首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android USB设备过滤器

Android USB设备过滤器
EN

Stack Overflow用户
提问于 2014-10-20 19:35:16
回答 1查看 3.6K关注 0票数 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

代码语言:javascript
复制
        <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>

和我的活动

代码语言:javascript
复制
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();
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-12 19:48:52

有点晚了,但是你的过滤器没有很好的结构:

代码语言:javascript
复制
<usb-device vendor-id="1027" product-id="24597" />

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26473529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档