I have modified the Android Bluetooth Chat code so that bytes received from the BluetoothChatService will be sent directly to the BluetoothChat Activity.
The sending rate of the other Bluetooth device is 125Khz, each packet is 20bytes.
So there will be around 125*20 bytes per second which has to be received by the Service and then sent to the Activity.
However, I found out that there will be packet loss in the data received by the Activity. I have verified that the data received in the Service is indeed correct.
This is the part of code which sends a 20 byte object to the Activity.
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, 20, -1, buffer)
.sendToTarget();
This is the handler that gets message back from The BluetoothChatService
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
...}
}
Any idea how to solve this problem? Thanks.
It is sending a reference to your buffer. If you reuse that buffer when reading in more Bluetooth data, your old data may be overwritten before the activity has had a chance to use the data.