Example #1
0
        public override void Run()
        {
            Name = $"AcceptThread_{socketType}";
            BluetoothSocket socket = null;

            while (manager.GetState() != STATE_CONNECTED)
            {
                try
                {
                    socket = serverSocket.Accept();
                }
                catch (Java.IO.IOException e)
                {
                    Log.Error(BluetoothConnectionManager.TAG, "accept() failed", e);
                    break;
                }

                if (socket != null)
                {
                    lock (this)
                    {
                        switch (manager.GetState())
                        {
                        case STATE_LISTEN:
                        case STATE_CONNECTING:
                            // Situation normal. Start the connected thread.
                            manager.Connected(socket, socket.RemoteDevice, socketType);
                            break;

                        case STATE_NONE:
                        case STATE_CONNECTED:
                            try
                            {
                                socket.Close();
                            }
                            catch (Java.IO.IOException e)
                            {
                                Log.Error(BluetoothConnectionManager.TAG, "Could not close unwanted socket", e);
                            }
                            break;
                        }
                    }
                }
            }
        }
Example #2
0
        public override void Run()
        {
            Name = $"ConnectThread_{socketType}";

            // Always cancel discovery because it will slow down connection
            manager.btAdapter.CancelDiscovery();

            // Make a connection to the BluetoothSocket
            try
            {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                socket.Connect();
            }
            catch (Java.IO.IOException)
            {
                // Close the socket
                try
                {
                    socket.Close();
                }
                catch (Java.IO.IOException e2)
                {
                    Log.Error(BluetoothConnectionManager.TAG, $"unable to close() {socketType} socket during connection failure.", e2);
                }

                // Start the service over to restart listening mode
                manager.ConnectionFailed();
                return;
            }

            // Reset the ConnectThread because we're done
            lock (this)
            {
                manager.connectThread = null;
            }

            // Start the connected thread
            manager.Connected(socket, device, socketType);
        }