Wandang
Wandang

Reputation: 952

Receiving bytearray gets splitted

I am developing an android app which receives an bytearray via Bluetooth from an Asuro which is a little robot.

This bytearray is a protocol for commands and therefore has a fixed size of 15 Furthermore the app is sending the bytearray to the Asuro and the Asuro sends it back.

However this bytearray gets split if I use the read(byte[],0,15) function and give this array to Log.d.

See the code below:

public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
    byte[] buffer = new byte[15];
int bytes = 0;

        // Keep listening to the InputStream while connected
while (true) {
    try {
    // Read from the InputStream
    bytes = mmInStream.read(buffer, 0, 15);
    if (buffer[0] != 0) {
        Log.d("Asuro-Android", "Input :" + Arrays.toString(buffer));
    }
    buffer = new byte[15];      
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}

I am sending to Asuro:

Output: [58, 48, 49, 48, 48, 48, 49, 48, 48, 48, 51, 48, 48, 48, 48]

Connections etc work. to simplify it i create an array and flush it into the outputstream.

But I am getting:

Input :[58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Input :[48, 49, 48, 48, 48, 49, 48, 48, 48, 51, 0, 0, 0, 0, 0]
Input :[48, 48, 48, 48, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Could this be an error on my part (app) or can this only be an error on asuro's part?

Upvotes: 0

Views: 120

Answers (1)

Dennis Mathews
Dennis Mathews

Reputation: 6975

This is the nature of the Bluetooth SPP profile , it doesn't provide frame boundaries. So it can get split by the sender SPP itself or the receiver depending on its buffer capacity etc. In your case since the packet is always fixed size you could do a simple re-assembly or for a more general case add your won header and reassemble a frame.

Upvotes: 1

Related Questions