Esempio n. 1
0
            public override void Run()
            {
                Log.Info(TAG, "BEGIN mConnectedThread");
                byte[] buffer = new byte[1024];
                int    bytes;

                LogLines.Clear();

                // Keep listening to the InputStream while connected
                while (true)
                {
                    try {
                        // Read from the InputStream
                        bytes = mmInStream.Read(buffer, 0, buffer.Length);
                        string command = Encoding.UTF8.GetString(buffer, 0, bytes).TrimEnd(" \r\n".ToCharArray());

                        byte[] toSend = _service._reader.SendReceive(command);
                        string result = LoyaltyCardReader.ByteArrayToHexString(toSend) + "\r\n";
                        _service.Write(Encoding.UTF8.GetBytes(result));

                        // Send the obtained bytes to the UI Activity
                        _service._handler.ObtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer)
                        .SendToTarget();

                        LogLines.Add(string.Format("{0},{1},{2}", DateTime.UtcNow.Ticks, command, result));
                    } catch (Java.IO.IOException e) {
                        Log.Error(TAG, "disconnected", e);
                        _service.ConnectionLost();

                        File.AppendAllLines(@"/sdcard/log.txt", LogLines);
                        break;
                    }
                }
            }
Esempio n. 2
0
/// <summary>
/// Sends a message.
/// </summary>
/// <param name='message'>
/// A string of text to send.
/// </param>
        private void SendMessage(Java.Lang.String message)
        {
            // Check that we're actually connected before trying anything
            if (chatService.GetState() != BluetoothChatService.STATE_CONNECTED)
            {
                Toast.MakeText(this, Resource.String.not_connected, ToastLength.Short).Show();
                return;
            }

            // Check that there's actually something to send
            if (message.Length() > 0)
            {
                // Get the message bytes and tell the BluetoothChatService to write
                byte[] send = message.GetBytes();
                chatService.Write(send);

                // Reset out string buffer to zero and clear the edit text field
                outStringBuffer.SetLength(0);
            }
        }