我正在使用来自 Google 的臭名昭著的 BluetoothChat-Example 来接收 ByteArray。我知道他的长度(55 字节)、他的起始字节(0x69)和他的结束字节(0x16)以及数组中数据的长度。
我很确定发件人在没有任何中断的情况下发送了 55 个字节,但在 BluethootChat 示例中,它看起来像我收到了多个数据包。第一个包由 0x69 后跟 1023 乘以 0x00 组成。然后我收到剩下的 55 个字节。70% 的时间都会发生这种情况,有时数组会在中间分裂,有时整个数组会完整接收。
这是正常的 Android 蓝牙行为吗
提前致谢...
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}