我的Android设备和WT-12蓝牙模块通过RFCOMM进行通信。我的数据速率约为180kbps左右。如果我只获取数据并且不在线处理数据,我的数据不会有任何问题。但是,当我尝试使用计数器对数据进行一些计算时,我可能会丢失数据。我认为这是因为缓冲区是在我读之前编写的。有办法避免这种情况吗? 我通过蓝牙捕获数据的代码如下:
void beginListenForData() {
stopWorker = false;
workerThread = new Thread(new Runnable() {
public void run() {
while ((!Thread.currentThread().isInterrupted() && !stopWorker)) {
try {
try {
bytesAvailable = myInputStream.available();
capturedSampleNumTwice += bytesAvailable;
} catch (NullPointerException e) {
Log.e("null pointer","null pointer");
}
if (bytesAvailable > 0) {
myInputStream.read(packetBytes, packetBytesAddress, bytesAvailable);
if(recordingActive){
packetBytesAddress += bytesAvailable;
Log.i("packetBytesAddress", String.valueOf(packetBytesAddress));
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
}