简体   繁体   中英

Xamarin Bluetooth Chat with Arduino - Giving strange two line results

I am working with Xamarin on Visual Studio and the most updated C# Bluetooth Chat I could find online. I have some trouble getting the proper output. I have the Arduino send data to the bluetooth ~every 2 seconds as follows

Serial.Println("In Standby Mode");

The bluetooth chat app receives this using a thread separate from the UI

public override void Run ()
{
    Log.Info (TAG, "BEGIN mConnectedThread");
     //data from socket
     byte[] buffer = new byte[1024];
     //number of bytes read
      int bytes;
      string bufferString;

      // Keep listening to the InputStream while connected
       while (true) {
        try {

                 lock(buffer)
                    { 
                        bytes = mmInStream.Read (buffer, 0, buffer.Length);
                        bufferString = System.Text.Encoding.ASCII.GetString(buffer);
                        var bufferStringJava = new Java.Lang.String(buffer, 0, buffer.Length);


                           System.Console.WriteLine("Incoming Data from Bluetooth Is:" + bufferStringJava);

                        _service._handler.ObtainMessage (BluetoothChat.MESSAGE_READ, bytes, -1, bufferString) //Edited to send a string instead of an array
                        .SendToTarget ();

                    }

                } catch (Java.IO.IOException e) {
                    Log.Error (TAG, "disconnected", e);
                    _service.ConnectionLost ();
                    break;
                }
            }

You can see I have the output printed here. Also, I have the output printed on the Android Activity

readString2 = String.Copy((string)msg.Obj);
var readMessage = new Java.Lang.String(Encoding.ASCII.GetBytes(readString2), 0, msg.Arg1);                         
bluetoothChat.conversationArrayAdapter.Add(bluetoothChat.connectedDeviceName + ":  " + readMessage);

The console output is very strange, giving things as follows. Various letters are typically missing from the output.

05-24 22:25:29.286 I/mono-stdout( 4690): Incoming Data from Bluetooth Is:I Standby Mode Incoming Data from Bluetooth Is:I Standby Mode

05-24 22:25:29.306 I/mono-stdout( 4690): Incoming Data from Bluetooth Is:n Standby Mode Incoming Data from Bluetooth Is:n Standby Mode

05-24 22:25:30.316 I/mono-stdout( 4690): Incoming Data from Bluetooth Is:I Standby Mode Incoming Data from Bluetooth Is:I Standby Mode

The Activity display can be seen in the picture attached. Essentially, it displays everything properly, but typically splits the "In Standby Mode" text in half.

Bluetooth Chat Output

I am very confused why there are missing letters in my console output, but not in the app display. Also, why the app is displaying as two lines. Also, I am not sure why my console seems to output the same thing twice.

Thanks for the help!

I figured out what was happening. Essentially, the thread reading the bluetooth socket data repeats over and over without any delay. The look will read data from the stream (mmInStream.Read()), then send that data using a handler. But it simply reads whatever is available at that moment. So if only the letter "I" has sent, it won't pick up the other letters until the next time around the look. This explains why the message was split. I fixed this using the following code.

  if (mmInStream.IsDataAvailable())
    {
     await System.Threading.Tasks.Task.Delay(20);
     bytes = mmInStream.Read(buffer, 0, buffer.Length);

Next, each time around the look, the byte[] buffer data is NOT reset. Since I was taking all the buffer each time, some characters from previous reads remained, giving me strange outputs.

To fix this, I copied the buffer into a string of length each to the bytes read, giving by the integer variable named bytes.

for (int i = 0; i < bytes; i++)
{
    temp[i] = buffer[i];
}

bufferString = System.Text.Encoding.ASCII.GetString(temp);

I still haven't figured out how to get rid of double output from mono.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM