We just switched our boards from the RN4677 to the RN4678, and we are having an issue with high latency parsing data on the RN4678. Our Android devices are the Lenovo M10 FHD Plus 2nd Gen, and for some reason there is a delay when receiving data with the RN4678.
Here is my code:
public static class BluetoothSocketListener implements Runnable {
private final WeakReference<CollectingDetail> wrActivity;
private BluetoothSocket socket;
public BluetoothSocketListener(BluetoothSocket socket, CollectingDetail collectingDetail) {
this.socket = socket;
wrActivity = new WeakReference<CollectingDetail>(collectingDetail);
}
@Override
public void run() {
final CollectingDetail activity = wrActivity.get();
if (activity != null) {
activity.inStream = null;
if (!Thread.currentThread().isInterrupted()) {
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
Log.i("Bluetooth bytes", new String(buffer));
activity.inStream = BluetoothConnectionService.inputStream;
int availableBytes;
int bytesRead = -1;
String message = "";
while (activity.processThread) {
message = "";
try {
availableBytes = activity.inStream.available();
if (availableBytes > 0) {
bytesRead = activity.inStream.read(buffer);
if (bytesRead != -1) {
message = new String(buffer, 0, bytesRead);
activity.mainHandler.post(new MessagePoster(message, activity));
}
}
} catch (IOException e) {
Log.e("BLUETOOTH_COMMS", "Error reading bytes");
try {
socket.close();
} catch (IOException e1) {
Log.e("BLUETOOTH_COMMS", "Could not close socket");
}
activity.processThread = false;
}
}
}
}
}
}
private static class MessagePoster implements Runnable {
private String message;
private final WeakReference<CollectingDetail> wrActivity;
public MessagePoster(String message, CollectingDetail activity) {
this.message = message;
wrActivity = new WeakReference<CollectingDetail>(activity);
}
@Override
public void run() {
final CollectingDetail activity = wrActivity.get();
if (activity != null) {
activity.seprateData(message);
}
}
}
void seprateData(String message) {
try {
message = message.replaceAll("(\\r\\n|\\n|\\r)", ",");
String[] a = message.split(",");
for (String data : a) {
if (data.length() > 0 && !data.equals(" ")) {
if (data.length() >= 10 && data.startsWith("5A")) {
Log.d("Good Data", data + " " + SystemClock.elapsedRealtime() );
al_sepratedMessageList.add(data);
} else {
if (ConnectThrough.equalsIgnoreCase("bt")) {
if (data.length() <= 5) {
char a1 = data.charAt(data.length() - 1);
if (Character.isDigit(a1) || Character.isLetter(a1)) {
Log.d("Partial Data Valid <= 5", data + " " + SystemClock.elapsedRealtime() );
breakSignal.append(data);
} else {
Log.d("Partial Data End", "End" + " " + SystemClock.elapsedRealtime());
breakSignal.setLength(0);
breakSignal.setLength(10);
}
} else {
Log.d("Partial Data Valid > 5", data + " " + SystemClock.elapsedRealtime());
breakSignal.append(data);
}
String s = breakSignal.toString();
if (s.length() >= 10
&& s.startsWith("5A")) {
al_sepratedMessageList.add(s);
breakSignal.setLength(0);
breakSignal.setLength(10);
}
}
}
}
}
calculation();
} catch (Exception e) {
Log.e("CollectingDetail", "Error parsing data!");
e.printStackTrace();
mApp.isMovingActivities = true;
finish();
}
}
On the RN4678, I noticed I am getting a lot of incomplete packets, which could be the cause of our latency. However, I haven't figured out a solution on how to solve it.
EDIT: We tested on HyperTerminal on a computer, and there doesn't appear to be a visible delay. However, this could have something to do with Android's Bluetooth stack.
EDIT 2: I logged each data point with timestamps, and it appears the RN4678 sends packets much quicker than the RN4677, some less than 1ms apart. I wonder if this is overworking the UI thread.