Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the same Usb accessory for two apps at same time?

I have an app that uses

<uses-feature
        android:name="android.hardware.usb.accessory"
        android:required="true" />

I want that USB to communicate with the App1 running in the foreground and App2 running in the background. The App2 running in background in mine. The App1 running in the foreground is by Third-party. I will not have any access to App1.
When i was trying to do, Whenever one app gains access the other loses the connection. Is there a way to make both app communicate with the USB simultaneously,

like image 819
jknair Avatar asked Nov 05 '17 06:11

jknair


2 Answers

Try this,

Create an my_accessory_filter.xml resource file in the res/xml/ directory with a element to identify your accessory.

  <resources>

<usb-accessory model="CoolAccessory" manufacturer="My App Company" version="1.0"/>

</resources>

Now, when you connect your accessory to the device android will send an intent to open an appropriate application. The best part is that more than one application can respond to a given intent so multiple apps could optionally respond for the same accessory.

Don't forget to call your my_accessory_filter.xml resource file in manifest like this:

<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
                android:resource="@xml/my_accessory_filter" />

Hope this helps!!

like image 169
PN10 Avatar answered Nov 18 '22 00:11

PN10


I am pretty sure this won't work. Let's assume that the system allows you to do this (which apparently is not the case, given your test). The way one communicates with a USB accessory is with a ParcelFileDescriptor.

To read from it, you will do something like this:

val buffer = ByteArray(16384)
val inStream = FileInputStream(fileDescriptor)

inStream.read(buffer)

If you have two apps reading at the same time, each of them will read part of the stream. For instance, if the stream is "A - B - C - D - E", the first app may read "A - B - E" and the second app "C - D". But that's most definitely not what you want.

like image 42
JonasVautherin Avatar answered Nov 18 '22 02:11

JonasVautherin