建立蓝牙连接后,我调用了写入方法5次,发送消息btAdapter.write(“abc18”.getBytes());同样的方式我通过read()方法读取消息但问题是read方法有时一起读取多个消息。我怎么能阻止这个?请参阅下面的客户端和服务器代码。服务器发送相同的消息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());
答案 0 :(得分:0)
如果您继续像现在这样继续发送,则无法阻止。
更好的发送线路。并在接收方读取行。