Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

USB_DEVICE_ATTACHED only startsActivity of Galaxy S3 ICS

Tags:

android

usb

Recently I've been trying to receive the intent android.hardware.usb.action.USB_DEVICE_ATTACHED using a broadcast receiver as per all the samples and examples that I've seen.

I've declared a reciever in the manifest;

<receiver android:name=".UsbDeviceReceiver">
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" 
                android:resource="@xml/device_filter"/>

I have also done similar in the activity code - onStart and OnStop register/unregister the receiver.

    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);

    registerReceiver(mUsbReceiver, filter);

However, I am finding that the intent is just caught. Observing logcat I can see that attaching a usb device looks for activities to start, while detaching broadcasts the detatch intent. According to the aforementioned samples, this should not be the case.

Am I missing something drastic concerning metadata? I have no problems at all with android.hardware.usb.action.USB_DEVICE_DETACHED. Perhaps this is a bug with the android version installed on the galaxy s3? Perhaps this is an ICS 'feature'.

Any relavent information is welcome!

like image 981
Gusdor Avatar asked Jun 28 '12 10:06

Gusdor


3 Answers

This is a known defect:

http://code.google.com/p/android/issues/detail?id=25701

and here:

http://code.google.com/p/android/issues/detail?id=25703

But don't hold your breath waiting for it to be fixed. While you're looking at the bug report, star it so you'll get notified of updates to the report. Likely that will be comments from other people asking for it to be fixed.

like image 135
Dave MacLean Avatar answered Nov 08 '22 20:11

Dave MacLean


The workaround that I have found for USB_ACCESSORY_ATTACHED is to just have a button that calls my OpenAccessory(accessory) method again and that seems to be working for me. Maybe that will work for USB_DEVICE_ATTACHED as well. Just have a device connnected/not connected status somewhere and if it is not connected the user can just hit that button.

Edit: I have found that putting android:launchMode="singleTask" in the manifest seems to "register" the intent of USB_ACCESSORY_ATTACHED, though it never shows up as such it does work as if thats what it is doing.

like image 45
TronicZomB Avatar answered Nov 08 '22 22:11

TronicZomB


you can declare intent filter in the manifest as explained here:http://developer.android.com/guide/topics/connectivity/usb/host.html

then you catch android.hardware.usb.action.USB_DEVICE_ATTACHED in onResume() or in onNewIntent().

when your activity is running and you attach your usb Device, main intent is thrown to onResume() but android.hardware.usb.action.USB_DEVICE_ATTACHED to the onNewIntent(). Adjust your code according to that.

you can declare onNewIntent() as follows:

@Override
    protected void onNewIntent(Intent intent) 
    {
        super.onNewIntent(intent);
        System.out.println("onNewIntent" + intent.getAction());
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
        mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if(mDevice!= null)
            mUsbManager.requestPermission(mDevice, mPermissionIntent);
        }
    }

and onResume() similarly.

tested on android 4.0.4 Device: Samsung Galaxy tab 750

like image 1
Abhishek Chandel Avatar answered Nov 08 '22 20:11

Abhishek Chandel