Java – Android USB device filter

Android USB device filter… here is a solution to the problem.

Android USB device filter

I’m trying to filter some devices so that they don’t show the USB connection dialog, which as far as I can understand can be done by creating a build filter, specifying allowed devices, and then handling intents in my activity.

The problem I’m having is that it prompts no matter what device I connect (it’s not limited to the list in device_filter.xml).

How do I limit connection prompts to devices listed in device_filter.xml?

device_filter.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1027" product-id="24597">
<usb-device vendor-id="1659" product-id="8963" >
</resources>

This file contains the devices I want to whitelist.

From AndroidManifest.xml:

        <activity
        android:name=". Player"
        android:clearTaskOnLaunch="true"
        android:configChanges="orientation|keyboardHidden"
        android:exported="true"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>

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

In My Activity:

if (intent.getAction() != null){
        if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
            Context context = getApplicationContext();
            try
            {
                PackageManager pm = context.getPackageManager();
                ApplicationInfo ai = pm.getApplicationInfo( "com.package.name", 0 );
                if( ai != null )
                {
                    UsbManager manager = (UsbManager) context.getSystemService( Context.USB_SERVICE );
                    IBinder b = ServiceManager.getService( Context.USB_SERVICE );
                    IUsbManager service = IUsbManager.Stub.asInterface( b );

HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
                    while( deviceIterator.hasNext() )
                    {
                        UsbDevice device = deviceIterator.next();
                        Log.d ("DERP", "The Vendor ID is: "+device.getVendorId());
                        Log.d ("DERP", "The interface is: "+device.getInterfaceCount());

service.grantDevicePermission( device, ai.uid );
                        service.setDevicePackage( device, "com.package.name");
                    }
                }
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    }

Solution

It’s a bit late, but your filter is not formatted correctly :

<usb-device vendor-id="1027" product-id="24597" />

You are missing a slash (/) for the closing tag.

Related Problems and Solutions