【问题标题】:Android USB device filterAndroid USB 设备过滤器
【发布时间】:2016-02-13 03:48:52
【问题描述】:

我正在尝试过滤某些设备以显示 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();
            }
        }
    }

【问题讨论】:

    标签: java android xml usb


    【解决方案1】:

    有点晚了,但您的过滤器格式不正确:

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

    您缺少用于关闭标签的斜杠 (/)。

    【讨论】:

      猜你喜欢
      • 2015-04-03
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多