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

2

7

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,

Elwood answered 5/11, 2017 at 6:28 Comment(0)
J
2

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!!

Josephinejosephson answered 7/11, 2017 at 14:2 Comment(2)
Will try and reply to you ASAPElwood
I have tried this. this only triggers a dialog box to set the default usb accessory to my app , which switchs the communication from the third party app to me. I want both to be communicating at same timeElwood
C
1

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.

Crumble answered 30/9, 2021 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.