1

I'm in ICS and am attempting to get a receiver attached that will be called when a USB device and/or accessory is plugged in/out of the tablet.

Based on the documentation and other Stack questions, it *looks like all I should need is the code beneath, however when I run this code nothing happens... it's hard to tell, cause my device has the USB accessory being plugged into/out of it, so my debugging is just me writing stuff to file, rather than actual debugging, but it doesn't *appear to do anything at all.

  IntentFilter attachFilter = new IntentFilter();
  attachFilter.addAction(ACTION_USB_PERMISSION);
  attachFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);     
  registerReceiver(mAttachReceiver, attachFilter);

  IntentFilter detachFilter = new IntentFilter();
  detachFilter.addAction(ACTION_USB_PERMISSION);
  detachFilter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED);
  registerReceiver(mDetachedReceiver, detachFilter);

the receivers look like this (I've cut them down for brevity but they're valid receivers)...

private final BroadcastReceiver mAttachReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        logMsg("ACTION = " + action);
        ... }

}

And my manifest contains this entry:

<uses-feature android:name="android.hardware.usb.host" />

Is there something I'm missing? I've tried ACTION_USB_DEVICE_ATTACHED instead of ACCESSORY but that doesn't seem to produce any different results. Also, I CAN talk to the device in the app once its attached, so, it's being recognized and the subsequent code that sends/receives messages from the attached device (it's a NFC card reader) works as expected. The only thing that is persistently failing is the notification of the initial cable connection.

TIA

4

1 回答 1

8

ACTION_USB_DEVICE_ATTACHED can only be a intent filter for an Activity and not a BroadCastReciever.

The UsbManager only resolves activities(neither service not BroadcastReciever) and launches them.

So you are not recieving any broadcast.

Edit: Android Developer site does say its a broadcast, but Android Source is only resolving activities. Though you can use ACTION_USB_ACCESSORY_DETACHED for detach updates in reciever

Edit2: There is nothing wrong in your code as far as Receiver is concerned. Your code is not working due to the way in which USB_ACCESSORY_ATTACHED is broadcast. The USB_ACCESSORY_ATTACHED is not a broadcast in true sense, Android Frameworks only launches the intent for activities, so you are not receiving the intent in broadcast Receiver.

You can only use Activity for ATTACHED Intent and not receiver

<activity android:name=".YourActivity" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
    </intent-filter>

    <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
               android:resource="@xml/accessory_filter" />
</activity>
于 2012-12-06T07:58:16.793 回答