Hos*_*ari 2 java android buffer bluetooth
我正在使用本教程构建一个使用蓝牙进行连接的多人游戏:
https://developer.android.com/samples/BluetoothChat/index.html
由于我发送的是JSON格式的长串(如长度为5500+),因此消息处理程序方法不返回我发送的整个字符串,而是将它们拆分为989个字符长的字符串(其长度始终为989个字符).问题是,当我尝试解析JSON字符串时,显然会抛出错误,因为文本不完整.当我在控制台中打印出字符串时,它会将它们打印成多行,分割为989个字符.
另请注意,缓冲区长度设置为1024,但是当我增加限制时,结果将不会有所不同,并且它总是989个字符.
byte[] buffer = new byte[1024];
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?这是消息处理程序方法.请注意,从主机设备发送的消息看起来很好(它没有拆分),但是当客户端收到它时,它会分成多个字符串.
private static final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BluetoothService.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case BluetoothService.STATE_CONNECTED:
case BluetoothService.STATE_CONNECTING:
case BluetoothService.STATE_LISTEN:
case BluetoothService.STATE_NONE:
break;
}
break;
case BluetoothService.MESSAGE_WRITE:
// BYTE LENGTH IS OK IN HERE!
byte[] writeBuf = (byte[]) msg.obj;
String writeMessage = new String(writeBuf);
break;
case BluetoothService.MESSAGE_READ:
// BYTE LENGTH IS 1024 IN HERE!
byte[] readBuf = (byte[]) msg.obj;
message = new String(readBuf, 0, msg.arg1);
break;
}
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
990 次 |
| 最近记录: |