Search code examples
android-studioconnectionusb

Detect connected USB device


I am working on an Android application that has to detect whether a USB converter (for example a USB-C to USB micro male to female converter) is connected to the device being used. From this link I thought this might be possible for USB micro and USB-C, since both have an ID pin for "Mode Detect".

I already checked this post on StackOverflow, which works for detecting whether the device is connected to a PC, but doesn't work for detecting a USB connection from (for example) a converter that is plugged in.

So what I would like to know is the following:

  1. Is it possible to detect whether a USB converter has been plugged into the device?
  2. If so, how could one detect this programatically?

Any help would be greatly appreciated.


Solution

  • You should use a USB_DEVICE_ATTACHED intent in your manifest. To get your app automatically started when the device is attached you should also specify a device_filter.xml

    <activity ...>
    ...
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
    
        <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>
    

    You can find more instructions here