Android Question USBserial with prefix or other alternatives

naveenpn

Member
Licensed User
Longtime User
Hi,
We have an application already written and tested using Bluetooth serial and we use the Asyncstreams with prefix for receiving data. We are now making a USB version of the hardware, but having a hard time to use the existing code as the USBserial lib doesn't support prefix mode. Is there anything we can do to continue using the earlier code with minimal changes? I've looked around but haven't found any alternatives and it looks like we have to write new code to receive data & buffer it ourselves which is proving too much for us. Any help would be highly appreciated.
 

naveenpn

Member
Licensed User
Longtime User
Its a custom hardware, reading sensor data and streaming it over bluetooth at the rate of about 2000 bytes per second. The data is then plotted in realtime on the android tablet. The data packet sent is prefixed with the data length (to satisfy the Asyncstream with prefix mode) with 208 bytes of data per packet. Unfortunately, we cannot change the firmware at this point of time as its heavily tested and no change is allowed.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It shouldn't be too difficult to implement the prefix protocol in your code. You need to create a large buffer and use ByteConverter to copy the data arriving to the buffer.
Use RandomAccessFile to read the length header. If there is enough data in the buffer than get the message payload (raf.ReadBytes) and trim the array.
See this example: https://www.b4x.com/android/forum/threads/b4x-class-mjpeg-decoder.73702/#content
 
Upvote 0

naveenpn

Member
Licensed User
Longtime User
Ok, thanks, if there is no other easy way, I guess we have to write our own code. Out of curiosity, is there any particular reason this is not available for the USBserial off the box?
 
Upvote 0
Top