Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android USB Accessory Multi Thread

I have a problem caused by multi-threading and Android Open Accessory.

I need to communicate with a USB Accessory, but I need to do it from 2 threads. One thread generates and sends data the other one reads data.

  • Why I don't use a single thread? Because there can be 1 or more writes before a read and reads are blocking, so that is not an option.

  • If using multiple threads, I do run into "I/O Error (No such device)" sooner or later, because I will have a collision between read & write being executed at the same time.

  • Locking will more or less put me back in single-thread situation, so not good.

  • .available() method on the input-stream returns is not supported, so I cannot check if anything is available before doing a read

  • Since it's not a socket-based stream I cannot set timeout either.

  • I have tried getting the FileDescriptor from the USBAccessory and passing to JNI to handle it there, but after the first read/write the device becomes inaccessible.

Question/Suggestion needed:
What will be a suggested/best-practice approach to this? I do not expect written code, I just need some guidance on how to approach this problem.

To clarify:
The software at the other end might or might NOT respond with any data. There are some so called silent sends were the data sent it's just received but there is no ACK. Since the app I'm working on is only a proxy, I do not have a clear picture if the data will or will not produce an answer. That will require analysis of the data as well, which isn't on the books at the moment.

like image 836
Emil Borconi Avatar asked Jun 14 '18 19:06

Emil Borconi


People also ask

What is USB accessory Android?

USB accessory mode allows users to connect USB host hardware specifically designed for Android-powered devices. The accessories must adhere to the Android accessory protocol outlined in the Android Accessory Development Kit documentation.

What is multi threading in Android?

Working on multiple tasks at the same time is Multitasking. In the same way, multiple threads running at the same time in a machine is called Multi-Threading. Technically, a thread is a unit of a process. Multiple such threads combine to form a process.

What is accessory framework android?

Open Accessory API or Open Accessory Framework - this is the API/framework in the Android development environment that allows the Android applications to transmit data in and out of the available USB port. This is provided by Google through the Android SDK.


1 Answers

If using multiple threads, I do run into I/O Error (No such device) sooner or later, because I will have a collision between read & write being executed at the same time.

This says it all. Since you are doing read and write on the same channel that does not support concurrent access, you are required to have your thread wait until the other thread is done doing read/write.

Your two-thread approach is what I would do, more or less. Good luck and trust in yourself.

like image 177
user1506104 Avatar answered Sep 28 '22 09:09

user1506104