1

我的 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();
}
4

0 回答 0