示例#1
0
            public override void Run()
            {
                Log.Info(TAG, "BEGIN mConnectThread");
                Name = "ConnectThread";

                // Always cancel discovery because it will slow down a connection
                _service._adapter.CancelDiscovery();

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

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

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

                // Start the connected thread
                _service.Connected(mmSocket, mmDevice);
            }
示例#2
0
            public override void Run()
            {
                if (Debug)
                {
                    Log.Debug(TAG, "BEGIN mAcceptThread " + this.ToString());
                }

                Name = "AcceptThread";
                BluetoothSocket socket = null;

                // Listen to the server socket if we're not connected
                while (_service._state != BluetoothChatService.STATE_CONNECTED)
                {
                    try {
                        // This is a blocking call and will only return on a
                        // successful connection or an exception
                        socket = mmServerSocket.Accept();
                    } catch (Java.IO.IOException e) {
                        Log.Error(TAG, "accept() failed", e);
                        break;
                    }

                    // If a connection was accepted
                    if (socket != null)
                    {
                        lock (this) {
                            switch (_service._state)
                            {
                            case STATE_LISTEN:
                            case STATE_CONNECTING:
                                // Situation normal. Start the connected thread.
                                _service.Connected(socket, socket.RemoteDevice);
                                break;

                            case STATE_NONE:
                            case STATE_CONNECTED:
                                // Either not ready or already connected. Terminate new socket.
                                try {
                                    socket.Close();
                                } catch (Java.IO.IOException e) {
                                    Log.Error(TAG, "Could not close unwanted socket", e);
                                }
                                break;
                            }
                        }
                    }
                }

                if (Debug)
                {
                    Log.Info(TAG, "END mAcceptThread");
                }
            }