android – 使用device_filter.xml资源文件过滤USB枚举结果

android – 使用device_filter.xml资源文件过滤USB枚举结果,第1张

概述按照 Android USB Host documentation中的说明,我设法通过USB_DEVICE_ATTACHED意图检测新的USB设备.要限制通知某些设备,可以指定资源文件: <activity ...>... <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVIC 按照 Android USB Host documentation中的说明,我设法通过USB_DEVICE_ATTACHED意图检测新的USB设备.要限制通知某些设备,可以指定资源文件:

<activity ...>...    <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>

device_filter.xml:

<?xml version="1.0" enCoding="utf-8"?><resources>    <usb-device vendor-ID="1234" product-ID="5678" /></resources>

问题是,如果在插入USB设备后服务启动,则不会收到任何意图.我可以使用getDeviceList获取设备列表,但希望避免从device_filter.xml文件中复制过滤条件.那可能吗?

解决方法 过滤功能在frameworks / base / services / java / com / androID / server / usb / UsbSettingsManager.java中实现,但不幸的是私有.我提取了部分实现,它可以像这样使用:

private voID scanDevices() {    ArrayList<UsbDevice> devices;    try {        devices = UsbDeviceFilter.getMatchingHostDevices(this,R.xml.wifi_devices);    } catch (Exception e) {        Log.w(TAG,"Failed to parse devices.xml: " + e.getMessage());        return;    }    for (UsbDevice device : devices) {        Log.d(TAG,"Matched device " + device);    }}

目前只接受主机设备,但添加对附件设备的支持是微不足道的.

UsbDeviceFilter.xml:

import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import androID.content.Context;import androID.content.res.XmlResourceParser;import androID.harDWare.usb.UsbDevice;import androID.harDWare.usb.UsbInterface;import androID.harDWare.usb.UsbManager;/** * Utility to test whether a USB device is accepted by a device filter. Heavily * based on com.androID.server.usb.UsbSettingsManager. * @author Peter Wu <lekensteyn@gmail.com> */public class UsbDeviceFilter {    private final List<DeviceFilter> hostDeviceFilters;    public UsbDeviceFilter(XmlPullParser parser) throws XmlPullParserException,IOException {        hostDeviceFilters = new ArrayList<UsbDeviceFilter.DeviceFilter>();        int eventType = parser.getEventType();        while (eventType != XmlPullParser.END_document) {            String tagname = parser.getname();            if ("usb-device".equals(tagname)                    && parser.getEventType() == XmlPullParser.START_TAG) {                hostDeviceFilters.add(DeviceFilter.read(parser));            }            eventType = parser.next();        }    }    public boolean matchesHostDevice(UsbDevice device) {        for (DeviceFilter filter : hostDeviceFilters) {            if (filter.matches(device)) {                return true;            }        }        return false;    }    /**     * Get a List of connected USB Host devices matching the devices filter.     * @param ctx A non-null application context.     * @param resourceID The resource ID pointing to a devices filter XML file.     * @return A List of connected host devices matching the filter.      * @throws XmlPullParserException     * @throws IOException     */    public static ArrayList<UsbDevice> getMatchingHostDevices(Context ctx,int resourceID) throws XmlPullParserException,IOException {        UsbManager usbManager = (UsbManager) ctx                .getSystemService(Context.USB_SERVICE);        XmlResourceParser parser = ctx.getResources().getXml(resourceID);        UsbDeviceFilter devFilter;        try {            devFilter = new UsbDeviceFilter(parser);        } finally {            parser.close();        }        ArrayList<UsbDevice> matchedDevices = new ArrayList<UsbDevice>();        for (UsbDevice device : usbManager.getDeviceList().values()) {            if (devFilter.matchesHostDevice(device)) {                matchedDevices.add(device);            }        }        return matchedDevices;    }    public static class DeviceFilter {        // USB vendor ID (or -1 for unspecifIEd)        public final int mvendorID;        // USB Product ID (or -1 for unspecifIEd)        public final int mProductID;        // USB device or interface class (or -1 for unspecifIEd)        public final int mClass;        // USB device subclass (or -1 for unspecifIEd)        public final int mSubclass;        // USB device protocol (or -1 for unspecifIEd)        public final int mProtocol;        private DeviceFilter(int vID,int pID,int clasz,int subclass,int protocol) {            mvendorID = vID;            mProductID = pID;            mClass = clasz;            mSubclass = subclass;            mProtocol = protocol;        }        private static DeviceFilter read(XmlPullParser parser) {            int vendorID = -1;            int productID = -1;            int deviceClass = -1;            int deviceSubclass = -1;            int deviceProtocol = -1;            int count = parser.getAttributeCount();            for (int i = 0; i < count; i++) {                String name = parser.getAttributename(i);                // All attribute values are ints                int value = Integer.parseInt(parser.getAttributeValue(i));                if ("vendor-ID".equals(name)) {                    vendorID = value;                } else if ("product-ID".equals(name)) {                    productID = value;                } else if ("class".equals(name)) {                    deviceClass = value;                } else if ("subclass".equals(name)) {                    deviceSubclass = value;                } else if ("protocol".equals(name)) {                    deviceProtocol = value;                }            }            return new DeviceFilter(vendorID,productID,deviceClass,deviceSubclass,deviceProtocol);        }        private boolean matches(int clasz,int protocol) {            return ((mClass == -1 || clasz == mClass)                    && (mSubclass == -1 || subclass == mSubclass)                    && (mProtocol == -1 || protocol == mProtocol));        }        public boolean matches(UsbDevice device) {            if (mvendorID != -1 && device.getvendorID() != mvendorID)                return false;            if (mProductID != -1 && device.getProductID() != mProductID)                return false;            // check device class/subclass/protocol            if (matches(device.getDeviceClass(),device.getDeviceSubclass(),device.getDeviceProtocol()))                return true;            // if device doesn't match,check the interfaces            int count = device.getInterfaceCount();            for (int i = 0; i < count; i++) {                UsbInterface intf = device.getInterface(i);                if (matches(intf.getInterfaceClass(),intf.getInterfaceSubclass(),intf.getInterfaceProtocol()))                    return true;            }            return false;        }    }}
总结

以上是内存溢出为你收集整理的android – 使用device_filter.xml资源文件过滤USB枚举结果全部内容,希望文章能够帮你解决android – 使用device_filter.xml资源文件过滤USB枚举结果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1125097.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存