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,
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!!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With