I have my Android device and WT-12 bluetooth module communicating over RFCOMM. My data rate is about ~180kbps roughly. I don't have any problem in my data if I only get the data and don't process it online. However, when I try to use a counter to d some calculations with the data, it is possible I lost data. I think it is happening since the buffer is written before I read it. Is there a way to avoid this? My code to capture data over bluetooth is as following:
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();
}