0

I am developing an android app where I want to read serial data via the phone's port(I use OGT cable). For this I have been using the following libray

https://github.com/mik3y/usb-serial-for-android

Library would read serial data as a byte array where I want to read it as an integer value between 0-255. Reason to do this is, I have been using "read()" method of the Inputstream in "java.io.InputStream". Here the read method would return an int evenn though it reads them as bytes.

Now I am trying to port this code to an android app, but I could not find a method to read integer from the serial input.

I tried converting the byte to int using the following code

ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default
short num = wrapped.getShort(); // 1

Where "arr" is the byte array read from the serial input. But this made the app crash in between and the numbers were not something in between 0-255.

I can not change my requirements of "integer values between 0-255" since there is a whole project built on this logic in the pc using Java.

Any suggestions to solve this?

1 Answers1

0

Use Byte(arr[0]).intValue() (using index 0 just as an example).

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74