CodeFusionMobile CodeFusionMobile - 2 months ago 17
Android Question

Java threading issue with handler message data being overwritten by next message

I have a thread reading data from a bluetooth stream that sends the data to a handler on the main UIThread as it comes in (based on the Bluetooth Chat Sample).

I've discovered a threading problem that pops up quite frequently. First, some code for reference. (Just the part that reads the incomming data stream. It has been set up correctly before this code runs).

public void run() {
DebugLog.i("BluetoothService", "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes =;

DebugLog.d("BluetoothService", new String(buffer, 0, bytes));
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
} catch (IOException e) {
DebugLog.e(TAG, "disconnected", e);

Handler defined in my main activity (partial):

// The Handler that gets information back from the BluetoothService
private final Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case BluetoothService.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
DebugLog.d("BluetoothHandler", readMessage);
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);


My problem comes when a small, continuous stream of data comes into the
. For example, 'abcd'. If 'abcd' is read all at once, this code functions normally and the log reads:

BluetoothService: BEGIN mConnectedThread
BluetoothService: abcd
BluetoothHandler: abcd

But if that continous block of data gets read in 2 parts, the second set of data overwrites the first set of data by the time it gets to the handler. Here's some example logs that I've seen.

BluetoothService: BEGIN mConnectedThread
BluetoothService: a
BluetoothService: bcd
BluetoothHandler: b
BluetoothHandler: bcd


BluetoothService: BEGIN mConnectedThread
BluetoothService: abc
BluetoothService: d
BluetoothHandler: dbc
BluetoothHandler: d


BluetoothService: BEGIN mConnectedThread
BluetoothService: ab
BluetoothService: cde
BluetoothHandler: cd
BluetoothHandler: cde

Notice that the second message that gets sent always overwrites the first message's data and only up to the shortest message's length. Also, both messages are always sent before the first message has been processed by

I am guessing that I'm a common buffer somewhere is getting overwritten before the the first message is fully processed, but I don't see where. Any suggestions?


Is it possible that you have to use a second byte buffer when creating the message object?

mHandler.obtainMessage(MESSAGE_READ, bytes, -1, copyOfBuffer)

I have a suspicion that mHandler (although I don't know what it is) is keeping a reference to that byte array you send him.