我正在尝试从数字天平接收蓝牙消息,我是这样做的
private class ConnectedThread extends Thread
{
private final InputStream mmInStream;
private final OutputStream mmOutStream;
ConnectedThread(BluetoothSocketWrapper socket)
{
InputStream tmpIn = null;
OutputStream tmpOut = null;
try
{
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run()
{
try{
int bytes;
while (true) {
try {
int availableBytes = mmInStream.available();
byte[] buffer = new byte[availableBytes];
if (availableBytes > 0){
bytes = mmInStream.read(buffer);
String readMessage = new String(buffer, 0, bytes);
// Envia los datos obtenidos hacia el evento via handler
int finalBytes = bytes;
if (bytes>=3){
bluetoothIn.obtainMessage(handlerState, finalBytes, -1, readMessage).sendToTarget();
}else{
SystemClock.sleep(100);
}
}
} catch (IOException e) {
break;
}
}
}catch (Exception er){
er.printStackTrace();
}
// Se mantiene en modo escucha para determinar el ingreso de datos
}我的问题在于我收到的信息。enter image description here
在图像中,你会看到一个绿色的标记,这是我应该收到的整个消息,但有时只是重量或什么都没有,我将另一个应用程序连接到电子天平,消息就会完整。因此,我认为平衡不是问题所在。
感谢你的阅读和帮助。再见
发布于 2018-10-30 22:07:01
尝试使用“token”,以便让android端知道命令/字符串已结束。例如:向android myString发送命令时,将其作为myString发送\r\n并在您的android设备上进行解析。您遇到的问题似乎与蓝牙设备/模块缓冲区未刷新有关
https://stackoverflow.com/questions/53065923
复制相似问题