蓝牙输入流一起读取多条消息
Bluetooth Inputstream read multiple messages together
建立蓝牙连接后,我调用了 write 方法 5 次来发送消息 btAdapter.write("abc18".getBytes());同样,我通过 read() 方法读取消息,但问题是读取方法有时会一起读取多条消息。我怎样才能防止这种情况发生?看到我下面的客户端和服务器 code.Server 发送了相同的消息 5 次并且客户端读取相同但问题有时是客户端一起读取多条消息。
server side code:
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
mmOutStream.write(buffer);
String sendMsg = "testing";
for(int i=0;i<5;i++){//Sending same message 5 times.
mmOutStream.write(sendMsg.getBytes());
}
Client side:
private final InputStream mmInStream;
mmBuffer = new byte[1024];
numBytes = mmInStream.read(mmBuffer);
byte[] readBuf = mmBuffer;
String readMessage = new String(readBuf, 0, numBytes);
Log.d(TAG,"READING MESSAGES MESSAGES::"+readMessage.toString());
如果您像现在这样继续发送,就无法阻止这种情况。
更好的发送线路。并在接收端读取行。
建立蓝牙连接后,我调用了 write 方法 5 次来发送消息 btAdapter.write("abc18".getBytes());同样,我通过 read() 方法读取消息,但问题是读取方法有时会一起读取多条消息。我怎样才能防止这种情况发生?看到我下面的客户端和服务器 code.Server 发送了相同的消息 5 次并且客户端读取相同但问题有时是客户端一起读取多条消息。
server side code:
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
mmOutStream.write(buffer);
String sendMsg = "testing";
for(int i=0;i<5;i++){//Sending same message 5 times.
mmOutStream.write(sendMsg.getBytes());
}
Client side:
private final InputStream mmInStream;
mmBuffer = new byte[1024];
numBytes = mmInStream.read(mmBuffer);
byte[] readBuf = mmBuffer;
String readMessage = new String(readBuf, 0, numBytes);
Log.d(TAG,"READING MESSAGES MESSAGES::"+readMessage.toString());
如果您像现在这样继续发送,就无法阻止这种情况。
更好的发送线路。并在接收端读取行。