0

I am using a bluetooth serial connection to send data from Arduino to Android. This is the code that I use in my arduino to send data:

char toSend = (char)Serial.read();
bluetooth.print(toSend);

and this code on my Android application to read this data:

 void inputthread() {

final Handler handler = new Handler(); 
stopWorker = false;
workerThread = new Thread(new Runnable()
{
  public void run() 
  {
     while(!Thread.currentThread().isInterrupted() && !stopWorker)
     {
          try
          { 
            int bytesAvailable = mmInputStream.available();     
            if(bytesAvailable > 0) 
            {
              byte[] packetBytes = new byte[bytesAvailable];
              mmInputStream.read(packetBytes);
              final String s = new String(packetBytes);
              final String t = new String("s");
              if (s==t)
                  {
                      handler.post(new Runnable() 
                      {

                          public void run()
                          {
                              try {
                                  closeBT();
                                  }
                                catch (IOException ex) { }
                          }
                      });
                  }

              handler.post(new Runnable() 
              {
                  public void run()
                  {
                   // txtaggiorna.setText(s);
                  }
              }); 

            }
          } catch (IOException ex) 
          {
            stopWorker = true;
          }
     }
  }
});

When I send "Ciao" from Arduino serial monitor, Android shows "C" first and then "iao", but I don't know why.

1
  • "// txtaggiorna.setText(s);" is not commented in the original Commented Mar 25, 2014 at 18:15

1 Answer 1

1

This is an expected result that you must allow for. Any time a stream of data is transitioned between character-by-character transports and batched or packetized transports, without the use of logic intended to preserve grouping, there is a possibility for a message to be split across multiple batches/packets.

Your project involves both character-by-character serial transports and packetized bluetooth transport, so this type of split is quite likely to happen.

A relatively poor way to reassemble messages is to assume anything which arrives with small time gaps to be a single message, and then assume a new message starts when there is a longer time gap.

A much more sound mechanism is to declare that messages will be terminated, for example with a newline character. In that case, you re-assemble everything obtained between newlines into a single message. Note that it's entirely possible for a packet / read call to give you the tail end of one message, a newline, and then the start of a new message.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.