简体   繁体   中英

Android USB device filter

I'm trying to filter certain devices from displaying the USB Connection dialogue, and according to my understanding, this can be done by creating a device filter, specifiying the devices to allow, then processing the intent within my activity.

The issue that I'm having is that it prompts regardless of what device I connect (It does not restrict to the list within device_filter.xml).

How can I limit the connection prompts to the 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 wish 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>

And within 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();
            }
        }
    }

A little bit late, but your filter is not well formed:

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

You're missing a slash (/) to close the tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM