I am currently developing my program using sample BluetoothChat program on android device. This is the website that you can refer to: http://developer.android.com/resources/samples/BluetoothChat/src/com/example/android/BluetoothChat/index.html
Base on some reasons, I want to continue sending the message to the receiver. Hence, I do this on a method called sendMessage() in BluetoothChat class.
byte[] send = message.getBytes();
for(int i = 0; i < 5; i++) //simply add a while loop here
mChatService.write(send);
Sometimes, the messages will jam to each other. e.g. the message is 1234. So the receiver should receive five indivual messages. However, the receiver may receive 4 message and one message is 12341234. The pattern is not unique. Sometimes the receiver 3 message with two message is 12341234. Sometimes it runs perfectly which receive five indivual messages.
I don’t know which one(sender or receiver or both) having the problem. How to fix such problem? Thx a lot~
It appears that you are assuming a one-one correspondence between write and reads. Often it does work that way, but there is no guarantee – the odd behavior that you are seeing is actually typical.
As a result, you are responsible for breaking the data that you receive (via your socket.read) into messages.
Typically you would use messages of a fixed length or you would include the length of the message in the message header. Using fixed length messages really simplifies things, but is usually not flexible enough.