So I'm receiving byte arrays from USB device (some log messages) and I'm facing an issue, I don't know what is the best way to parse or read them...
So this is the receiver:
static class ReceiveLogsThread implements Runnable {
private static final String TAG = "IoTReceiveLogsThread";
Message msgRead;
ReceiveLogsThread() {
}
public void run() {
byte[] rbuf = new byte[4096];
while (!Thread.currentThread().isInterrupted()) {
try {
int len = mSerial.readLog(rbuf, mSerialPortLog);
if (len > 0) {
// Crashlytics.log(Log.DEBUG, TAG, "ReceiveLogsThread: " + printHex(rbuf));
// this.msgRead = receiveLogsHandler.obtainMessage(HANDLE_READ, printHex(rbuf));
this.msgRead = receiveLogsHandler.obtainMessage(HANDLE_READ, rbuf);
receiveLogsHandler.sendMessage(this.msgRead);
}
} catch (NullPointerException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Thread.currentThread().interrupt();
if (!mReadFlag) {
Crashlytics.log(Log.WARN, TAG, "Receive thread finished");
}
}
}
As you can see, printHex() method is commented because I think it caused me an issue with losing some of those messages due to real time parsing, as you can see from it's implementation
private static String printHex(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
String hex = formatter.toString();
return hex;
}
I don't thinks it's a good idea to do a printHex method as soon as I receive byte array, because bytes are comming so fast, so I want to try another way..
I want to send them as byte arrays and then parse them after I'm done with everything, so I'm not sure how to implement it right...
Here is a receive handler from my activity where I'm storing those arrays into List of byte arrays that could contain like 30000 byte arrays:
private List<byte[]> logs = new ArrayList<>();
Handler receiveLogsHandler = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msgRW) {
super.handleMessage(msgRW);
// logMessagesList.add(msgRW.obj.toString().toUpperCase());
// String message = msgRW.obj.toString().toUpperCase();
if(shouldCollectLogs) {
byte[] message = (byte[]) msgRW.obj;
logs.add(message);
}
....
So the problem I'm facing here is, how to combine all those byte arrays into one! And then do printHex on that one big array..