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 finish
es itself immediately. All of this occurs in LauncherActivity.onCreate
.
Any ideas?