androidandroid-usb

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,


Solution

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