版权声明:本文为CSDN博主「pxw1992」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/pxw1992/article/details/115402870
android 串口通信 ,系统休眠后,有概率出现数据丢包,在网上找到一篇文章,将串口读数据部分代码采取下面的方式,解决了该问题。
/**
* 串口读数据线程
*/
public void run() {
byte[] buf = new byte[6];
while (!isInterrupted()) {
try {
if (null == mInputStream) {
return;
}
if (mInputStream.available() <= 0) {
continue;
}else {
try {
Thread.sleep(50L);
} catch (Exception e) {
e.printStackTrace();
}
}
int read = mInputStream.read(buf);
if (read > 0) {
byte[] readBytes = new byte[read];
System.arraycopy(buf, 0, readBytes, 0, read);
onDataReceived(readBytes);
Log.i(TAG, "run: readBytes = " + byte2hex(readBytes));
}
try {
Thread.sleep(10L);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Log.e(TAG, "read thread over");
}