Esempio n. 1
0
        private void ThreadRun()
        {
            // Always cancel discovery because it will slow down a connection
            _adapter.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();
                SAMLog.Debug("ABTA::Connect()=>true");
            }
            catch (Java.IO.IOException e)
            {
                SAMLog.Warning("ABTA::CONNFAILED", e);
                _adapter.ThreadMessage_ConnectionFailed();
                // Close the socket
                try
                {
                    mmSocket.Close();
                }
                catch (Java.IO.IOException e2)
                {
                    SAMLog.Error("ABTA::NOCLOSE2", e2.Message);
                }
                return;
            }

            // Reset the ConnectThread because we're done
            lock (this)
            {
                _adapter.SetConnectThreadCancelled();
            }

            // Start the connected thread
            _adapter.ThreadMessage_Connected(mmSocket, mmDevice);
        }
Esempio n. 2
0
        private void ThreadRun()
        {
            // Listen to the server socket if we're not connected
            while (_adapter.State != BluetoothAdapterState.Connected)
            {
                if (_adapter.State == BluetoothAdapterState.AdapterNotFound)
                {
                    return;
                }
                if (_adapter.State == BluetoothAdapterState.Error)
                {
                    return;
                }
                if (_adapter.State == BluetoothAdapterState.PermissionNotGranted)
                {
                    return;
                }
                if (_adapter.State == BluetoothAdapterState.ConnectionFailed)
                {
                    return;
                }
                if (_adapter.State == BluetoothAdapterState.ConnectionLost)
                {
                    return;
                }
                if (_adapter.State == BluetoothAdapterState.NotEnabledByUser)
                {
                    return;
                }

                BluetoothSocket socket = null;
                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)
                {
                    SAMLog.Warning("ABTA::AcceptFailed", e);
                    continue;
                }

                // If a connection was accepted
                if (socket != null)
                {
                    lock (this)
                    {
                        SAMLog.Debug("ABTA::Accept()=>" + _adapter.State);

                        switch (_adapter.State)
                        {
                        case BluetoothAdapterState.Listen:
                        case BluetoothAdapterState.Connecting:
                            // Situation normal. Start the connected thread.
                            _stopped = true;
                            _adapter.ThreadMessage_Connected(socket, socket.RemoteDevice);
                            return;

                        case BluetoothAdapterState.Active:
                        case BluetoothAdapterState.Connected:
                            // Either not ready or already connected. Terminate new socket.
                            try
                            {
                                socket.Close();
                            }
                            catch (Java.IO.IOException e)
                            {
                                SAMLog.Warning("ABTA::CNC", "Could not close unwanted socket", e.Message);
                            }
                            break;

                        case BluetoothAdapterState.AdapterNotFound:
                        case BluetoothAdapterState.PermissionNotGranted:
                        case BluetoothAdapterState.Created:
                        case BluetoothAdapterState.RequestingEnable:
                        case BluetoothAdapterState.NotEnabledByUser:
                        case BluetoothAdapterState.Scanning:
                        case BluetoothAdapterState.ConnectionLost:
                        case BluetoothAdapterState.ConnectionFailed:
                        case BluetoothAdapterState.Error:
                        default:
                            SAMLog.Error("ABTA::EnumSwitch_TR", "value: " + _adapter.State);
                            try
                            {
                                socket.Close();
                            }
                            catch (Java.IO.IOException e)
                            {
                                SAMLog.Warning("ABTA::CNC", "Could not close unwanted socket", e.Message);
                            }
                            return;
                        }
                    }
                }
            }
        }