16

I've got an Android app that needs to detect when a USB peripheral is attached or detached. It works fine when the peripheral is first attached, but I don't receive any notification (i.e., I don't receive an Intent whose action is ACTION_USB_DEVICE_DETACHED) when it is subsequently detached.

Here's the relevant part of my AndroidManifest.xml:

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

It may also be worth noting that LauncherActivity only exists to start a Service when the device is attached, and to stop the service when it is detached. In either case, LauncherActivity always finishes itself immediately. All of this occurs in LauncherActivity.onCreate.

Any ideas?

1

5 Answers 5

18

USB_DEVICE_DETACHED is a broadcast intent, thus you may want to declare the BroadcastReceiver in manifest with the appropriate intent-filter for detached action, also with meta-data attached. Same goes for USB_ACCESSORY_DETACHED, for who is interested.

Summary:
USB_XXX_ATTACHED is an activity intent
USB_XXX_DETACHED is a broadcast intent

(where XXX = DEVICE | ACCESSORY)

See: http://developer.android.com/guide/components/intents-filters.html

"There is no overlap within these messaging systems: Broadcast intents are delivered only to broadcast receivers, never to activities or services"

2
  • 1
    This is incorrect. The DEVICE actions are both broadcasts. For ACCESSORY, the attached is an activity action, but the detached in a broadcast action. Aug 17, 2016 at 22:08
  • USB_XXX_ATTACHED intent can be used as broadcast intent also, I tried it and it's working fine for me. Oct 16, 2017 at 5:05
11

Try with USB_STATE as below.

It will fire both attached and detatched to same receiver and in receiver you can identify whether is was attached or detatched event.

IntentFilter filter = new IntentFilter();
filter.addAction("android.hardware.usb.action.USB_STATE");

Receiver:

public class USBReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if (intent.getExtras().getBoolean("connected")) { 
             //do your stuff
        }
    }  
}
3
  • Works like a charm ! Thank you Aug 27, 2015 at 13:27
  • 1
    Thank you. But I'm getting false always. I'll check it. Apr 12, 2016 at 14:57
  • @Hermandroid I imagine your situation would have been resolved by checking for host_connected instead (for future readers)
    – jskidd3
    Mar 26, 2021 at 15:16
9

So, I never got the ACTION_USB_DEVICE_DETACHED Intent to go to LauncherActivity; I don't know what the deal is there, probably something I don't properly understand about intent filters or the Activity lifecycle callbacks.

The solution I ended up using comes from the post linked by Pratik. I basically took everything about USB_DEVICE_DETACHED out of AndroidManifest.xml. Then, in the onCreate method of the Service, I registered a BroadcastReceiver like this:

@Override
public void onCreate() {
    detachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED))
                stopSelf();
        }
    };

    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(detachReceiver, filter);
}

A little clunky, and I'm still curious why just adding USB_DEVICE_DETACHED to the <intent-filter> of LauncherActivity wasn't working, but it does what I need.

1
  • 1
    Works great. The trick for me was removing the USB_DEVICE_DETACHED out of AndroidManifest.xml.
    – Frank
    Mar 18, 2016 at 23:33
1

The fix that worked for me was to unregister in onPause() and register again in onResume():

@Override
public void onPause()
{
    super.onPause();
    stopIOManager();
    if(m_UsbReceiver!=null) unregisterReceiver(m_UsbReceiver);
}

@Override
public void onResume()
{
    super.onResume();
    startIOManager();
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(m_UsbReceiver, filter);
}

However, although the app seems to always receive the DETACH event, once in a while it doesn't get the ATTACHED event. I then need to plug and and unplug the USB connector, and it usually works after one or two attempts. I've put the blame of this strange behaviour on the OS, without certainty.

0
0

For what it's worth, I had this same issue because the activity is paused then resumed when the device is disconnected.
Since the receiver is unregistered in the OnPause() method just prior to receiving the ACTION_USB_DEVICE_DETACHED, your app never gets notified.

1
  • It'd be awesome if you posted how you got around this issue. Sep 13, 2015 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.