Android判断USB设备类型

相关要点

//获取所有的USB设备
UsbManager usbManager = (UsbManager) appContext.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
//查看一下USB设备信息
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            Log.e("fxlog", "UsbDevice[" + device.toString() + "]");
        }

测试时插入了两个设备,一个U盘一个阵列麦克风得到了以下信息

鼠标相关打印
UsbDevice[mName=/dev/bus/usb/004/002,mVendorId=6127,mProductId=24601,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=PixArt,
mProductName=Lenovo USB Optical Mouse,mVersion=2.0,mSerialNumber=null,
mConfigurations=[
    UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,
    mInterfaces=[
    UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=2,mEndpoints=[
    UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=8,mInterval=10]]]]

阵列麦克风相关打印
UsbDevice[mName=/dev/bus/usb/005/002,mVendorId=3190,mProductId=5659,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=null
,mProductName=MAONO AU-BM10,mVersion=1.16,mSerialNumber=null,
mConfigurations=[
    UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=50,
    mInterfaces=[
    UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=1,mSubclass=1,mProtocol=0,mEndpoints=[]
    UsbInterface[mId=1,mAlternateSetting=0,mName=null,mClass=1,mSubclass=2,mProtocol=0,mEndpoints=[]
    UsbInterface[mId=1,mAlternateSetting=1,mName=null,mClass=1,mSubclass=2,mProtocol=0,mEndpoints=[
    UsbEndpoint[mAddress=1,mAttributes=9,mMaxPacketSize=200,mInterval=1]]
    UsbInterface[mId=2,mAlternateSetting=0,mName=null,mClass=1,mSubclass=2,mProtocol=0,mEndpoints=[]
    UsbInterface[mId=2,mAlternateSetting=1,mName=null,mClass=1,mSubclass=2,mProtocol=0,mEndpoints=[
    UsbEndpoint[mAddress=130,mAttributes=5,mMaxPacketSize=200,mInterval=1]]
    UsbInterface[mId=3,mAlternateSetting=0,mName=null,mClass=3,mSubclass=0,mProtocol=0,mEndpoints=[
    UsbEndpoint[mAddress=131,mAttributes=3,mMaxPacketSize=4,mInterval=32]]]]

经分析每一个USB有多个Interface,而InterfaceClass则标记USB是属于什么设备,打印信息mClass就是标记了设备类型

InterfaceClass获取
Log.e("fxlog", "InterfaceClass:" + device.getInterface(0).getInterfaceClass());

类型判断相关分析

//相关静态常量
android.hardware.usb.UsbConstants
   /**
     * USB class indicating that the class is determined on a per-interface basis.
     */
    public static final int USB_CLASS_PER_INTERFACE = 0;
    /**
     * USB class for audio devices.
     */
    public static final int USB_CLASS_AUDIO = 1;
    /**
     * USB class for communication devices.
     */
    public static final int USB_CLASS_COMM = 2;
    /**
     * USB class for human interface devices (for example, mice and keyboards).
     */
    public static final int USB_CLASS_HID = 3;
    /**
     * USB class for physical devices.
     */
    public static final int USB_CLASS_PHYSICA = 5;
    /**
     * USB class for still image devices (digital cameras).
     */
    public static final int USB_CLASS_STILL_IMAGE = 6;
    /**
     * USB class for printers.
     */
    public static final int USB_CLASS_PRINTER = 7;
    /**
     * USB class for mass storage devices.
     */
    public static final int USB_CLASS_MASS_STORAGE = 8;
    /**
     * USB class for USB hubs.
     */
    public static final int USB_CLASS_HUB = 9;
    /**
     * USB class for CDC devices (communications device class).
     */
    public static final int USB_CLASS_CDC_DATA = 0x0a;
    /**
     * USB class for content smart card devices.
     */
    public static final int USB_CLASS_CSCID = 0x0b;
    /**
     * USB class for content security devices.
     */
    public static final int USB_CLASS_CONTENT_SEC = 0x0d;
    /**
     * USB class for video devices.
     */
    public static final int USB_CLASS_VIDEO = 0x0e;
    /**
     * USB class for wireless controller devices.
     */
    public static final int USB_CLASS_WIRELESS_CONTROLLER = 0xe0;
    /**
     * USB class for wireless miscellaneous devices.
     */
    public static final int USB_CLASS_MISC = 0xef;
    /**
     * Application specific USB class.
     */
    public static final int USB_CLASS_APP_SPEC = 0xfe;
    /**
     * Vendor specific USB class.
     */
    public static final int USB_CLASS_VENDOR_SPEC = 0xff;

    /**
     * Boot subclass for HID devices.
     */
    public static final int USB_INTERFACE_SUBCLASS_BOOT = 1;
    /**
     * Vendor specific USB subclass.
     */
    public static final int USB_SUBCLASS_VENDOR_SPEC = 0xff;

进行device.getInterface(0).getInterfaceClass()==UsbConstants.USB_CLASS_**判断即可

发表回复

CAPTCHAis initialing...