我正在使用Google的Infamous BluetoothChat-Example来接收ByteArray。 我知道他的长度(55字节),他的起始字节(0x69)和他的结束字节(0x16)以及数组中数据的长度。
我很确定Sender在没有任何中断的情况下发送了55个字节,但在BluethootChat示例中看起来我收到了多个数据包。 第一个包由0x69后跟1023倍0x00组成。 然后我收到55bytes的其余部分。 这种情况在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;
}
}
}