Ok, this question has been asked a number of times on SO, but none of which has solved my issue. Typically most of the others have gone for BroadcastReceiver
as far as I understand.
Anyway, to the question. One of my Activities should read data from an USB device once it's attached to the phone. In order to perform this I created an intent-filter
in my AndroidManifest.xml
file according to the USB Host guide, like this:
<activity
android:name=".activity.GetMeterDataActivity"
android:label="@string/title_activity_get_meter_data" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
This works great in the emulator I use and my Activity
is started when I attach the device. However, on the real device nothing happens. If I instead register a BradcastReceiver
as suggested as an alternative solution on the guide the Intent is received and I ask for permission to use it and it works. I however find the later solution less neat since you have to ask permission yourself and would really prefer to just have the <intent-filter>
in the manifest.
Tried:
device_filter.xml
.<intent-filter>
between the intended Activity and the MAIN
Activity without success.onCreate()
and onResume()
, nothing or just android.intent.action.MAIN
Any pointers would be greatly appreciated.
EDIT1: This seem to be a Sony issue after upgrading to Lollipop, and devices all of a sudden is blacklisted. I can see this in my logs.
09-16 21:23:07.272 860-1279/? W/ContextImpl? Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1346 com.android.server.usb.UsbSettingsManager.blacklistedDeviceAttached:757 com.android.server.usb.UsbHostManager.endUsbDeviceAdded:242 com.android.server.usb.UsbHostManager.monitorUsbHostBus:-2 com.android.server.usb.UsbHostManager.access$000:49
If it is indeed blacklisted it's a bit odd that I can still use the device using a BraodcastReceiver
.
It was Sony that filtered the Intent I wanted to read. With the update to build number 23.4.A.1.232 of the software for Sony Xperia Z3 Compact this is no longer an issue and I receive the Intent.