安卓USB设备过滤器

3

我试图过滤掉显示 USB 连接对话框的某些设备,并根据我的理解,可以通过创建设备筛选器、指定要允许的设备,然后在我的活动中处理意图来完成。

我遇到的问题是无论我连接什么设备,它都会提示(它不限制在 device_filter.xml 中的列表中)。

如何将连接提示限制为 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>

此文件包含我希望列入白名单的设备。

来自 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>

在我的活动中

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();
            }
        }
    }
1个回答

4
有点晚了,但是你的过滤器格式不正确:
<usb-device vendor-id="1027" product-id="24597" />

您缺少一个斜杠(/)来关闭标签。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接