1

I have Android <-> Arduino bluetooth communication. Sometimes Android gets two messages instead of one from Arduino. What i mean: if i send for ex. 5 bytes message "1234" from Arduino to Android over bluetooth, sometimes i get "1" 1 byte in one message and "234"+\n 4 bytes in second message. Sometimes i get full "1234"+\n 5 byte message and do not have any clue why. I need to split input message by delimiter and if i get separate message i get crash. So i need to append bytes to string till new line char comes.

  • How to add chars to string till "\n" new line char comes?

Case when data comes:

    case BLUETOOTH_RECEIVED:
        byte[] buffer = (byte[])msg.obj;
        int len = msg.arg1;
        if (len > 0 && buffer != null) {
            onBluetoothRead(buffer, len);
        }
        break;
    }

buffer to string:

private void onBluetoothRead(byte[] buffer, int len) {
    Log.i(LOGGER_TAG, String.format("Received: " +  output.replace("\n", "") + " message of " + "%d bytes", len));
    String output = new String(buffer, 0, len); // Add read buffer to new string
    m_deviceOutput.append(output); // Add (not replace) string to TextView
    StringTokenizer  splitStr = new StringTokenizer(output, ","); // split string by comma
    String numberOne = splitStr.nextToken(); // First split string
    String numberTwo = splitStr.nextToken(); // Second split string
    numberOne = numberOne.replaceAll("\\D+",""); // replace all chars, leave only numbers
    numberTwo = numberTwo.replaceAll("\\D+","");
}

LogCat:

07-22 14:06:15.099: I/DeviceActivity(20370): Received: 1234 message of 5 bytes
07-22 14:06:20.599: I/DeviceActivity(20370): Received: 1234 message of 5 bytes
07-22 14:06:27.349: I/DeviceActivity(20370): Received: 1 message of 1 bytes
07-22 14:06:27.469: I/DeviceActivity(20370): Received: 234 message of 4 bytes
07-22 14:06:37.219: I/DeviceActivity(20370): Received: 1 message of 1 bytes
07-22 14:06:37.349: I/DeviceActivity(20370): Received: 234 message of 4 bytes

in Arduino i can write like this and i want something similar here:

//Get data from RS485:
void READ01(){
  while (mySerial.available()){
    mySerial.read();
  }
    mySerial.println("01READ");
    momentas1="";
    delay(20);

    while (mySerial.available()) { 
      char c = mySerial.read();
      if (c == '\n'){
     break;
      }
      momentas1 += c; 
      }
}

This void READ01 adds chars to string until new line char comes.

1 Answer 1

2

You could use a buffer-like implementation by adding obtained string to another string until "\n" is received.

private String packet = "";
private void onBluetoothRead(byte[] buffer, int len) {
    Log.i(LOGGER_TAG, String.format("Received: " +  output.replace("\n", "") + " message of " + "%d bytes", len));
    String output = new String(buffer, 0, len); // Add read buffer to new string
    packet += output;
    if (packet.endsWith( "\n" ) {
        //do what you need to do
        m_deviceOutput.append(output); // Add (not replace) string to TextView
        StringTokenizer  splitStr = new StringTokenizer(packet, ","); // split string by comma
        String numberOne = splitStr.nextToken(); // First split string
        String numberTwo = splitStr.nextToken(); // Second split string
        numberOne = numberOne.replaceAll("\\D+",""); // replace all chars, leave only numbers
        numberTwo = numberTwo.replaceAll("\\D+","");
        packet = "";
    }

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

5 Comments

I didn't know string.endsWith()... I will try, but i believe 99.9% that this is an answer :)
hi there, I hope it works. But note that I have made an edit. There was a mistake in the code I wrote.
At first when wrote code to Eclipse i corrected it :)
Thank you Ruraj :) .startsWith() and .endsWith() are quite helpful :)
Glad to be of help :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.