コード例 #1
0
        public override void Run()
        {
            Log.Info(BluetoothConnectionManager.TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int    bytes;

            // Keep listening to the InputStream while connected
            while (manager.GetState() == STATE_CONNECTED)
            {
                try
                {
                    // Read from the InputStream
                    bytes = inStream.Read(buffer, 0, buffer.Length);

                    // Send the obtained bytes to the UI Activity
                    manager.handler
                    .ObtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                    .SendToTarget();
                }
                catch (Java.IO.IOException e)
                {
                    Log.Error(BluetoothConnectionManager.TAG, "disconnected", e);
                    manager.ConnectionLost();
                    break;
                }
            }
        }
コード例 #2
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;
                        }
                    }
                }
            }
        }