I am making an android application that can communicate with arduino serially over USB and used UsbSerial library from Github. I'm receiving data from arduino in UsbReadCallback which I have implemented as:
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] arg0) {
try {
String data = new String(arg0, Charset.forName("utf-8"));
if (mHandler != null)
{
mHandler.obtainMessage(0, data).sendToTarget();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
and my Handler as:
private static class MyHandler extends Handler {
private final WeakReference<SensorDisplayActivity> mActivity;
public MyHandler(SensorDisplayActivity activity) {
mActivity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
String data = ( String) msg.obj;
mActivity.get().mainValue_tx.setText(data);
}
}
But problem arriving when I'm displaying it in the TextView using setText()
its not showing the correct value i.e. sometimes showing 00 in place of 100 and 2 in place of 42 etc. but when I'm using append()
method of TextView the correct values are appending correctly. What might be the solution?