如何判断USB设备的类型

    xiaoxiao2021-03-25  202

    1.USB的设备类型定义在UsbConstants.java 中,有以下几种

    2.那到底怎么知道usb外设的类型是属于那种呢,通过以下接口

    2.1若是mtk方案,外接设备后,从kernel log可以看得到

    <6>[ 9047.548616]  (2)[17:kworker/2:0]hid-generic0003:0C2E:0E4A.001D: hidraw0: USBHID v1.10Device [xxxxxxxx] on usb-musb-hdrc.0.auto-1.2.1/input2

    0003:0C2E:0E4A.001D对应设备的VID PID的16进制数,前面的0003是设备的类型

    2.2 但是有写有vendor自定义的类型则无法直观的看出

    从sys log可以看到以下打印信息,其中mClass=255就是我们想要知道的类型,此处为255及ffh,对应UsbConstants中的 USB_CLASS_VENDOR_SPEC

    UsbHostManager: Added device UsbDevice[mName=/dev/bus/usb/001/030,mVendorId=5246,mProductId=8214,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=UPEK,mProductName=Biometric Coprocessor,mVersion=1.16,mSerialNumber=null,mConfigurations=[ 01-01 23:45:56.345537   769  1052 D UsbHostManager: UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[ 01-01 23:45:56.345537   769  1052 D UsbHostManager: UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=255,mSubclass=0,mProtocol=0,mEndpoints=[ 01-01 23:45:56.345537   769  1052 D UsbHostManager: UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=64,mInterval=0] 01-01 23:45:56.345537   769  1052 D UsbHostManager: UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=64,mInterval=0] 01-01 23:45:56.345537   769  1052 D UsbHostManager: UsbEndpoint[mAddress=131,mAttributes=3,mMaxPacketSize=4,mInterval=20]]]]

    以上信息存在于UsbDevice.java中

    可以按照自己的格式显示usb设备插入时的相关信息

    private String listUsbDevices(boolean all)    {     UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);      HashMap deviceList = usbManager.getDeviceList();      if (deviceList.size() == 0)      {        return "no usb devices found";      }      Iterator deviceIterator = deviceList.values().iterator();      String returnValue = "";        while (deviceIterator.hasNext())      {        UsbDevice device = (UsbDevice)deviceIterator.next();        if ((device.getVendorId() == xxxx) && ((device.getProductId() == xxxx) )) //这里可以过滤你所关心的pid vid        {                    if (!all)          {            returnValue = returnValue + "Model: " + device.getDeviceName() + "\r\n";            returnValue = returnValue + "ID: " + device.getDeviceId() + "\r\n";            returnValue = returnValue + "Class: " + device.getDeviceClass() + "\r\n";            returnValue = returnValue + "Protocol: " + device.getDeviceProtocol() + "\r\n";            returnValue = returnValue + "Vendor ID " + device.getVendorId() + "\r\n";            returnValue = returnValue + "Product ID: " + device.getProductId() + "\r\n";            returnValue = returnValue + "Interface count: " + device.getInterfaceCount() + "\r\n";            returnValue = returnValue + "---------------------------------------\r\n";            for (int index = 0; index < device.getInterfaceCount(); index++)            {              UsbInterface mUsbInterface = device.getInterface(index);              returnValue = returnValue + "  *****     *****\r\n";              returnValue = returnValue + "  Interface index: " + index + "\r\n";              returnValue = returnValue + "  Interface ID: " + mUsbInterface.getId() + "\r\n";              returnValue = returnValue + "  Inteface class: " + mUsbInterface.getInterfaceClass() + "\r\n";              returnValue = returnValue + "  Interface protocol: " + mUsbInterface.getInterfaceProtocol() + "\r\n";              returnValue = returnValue + "  Endpoint count: " + mUsbInterface.getEndpointCount() + "\r\n";              for (int epi = 0; epi < mUsbInterface.getEndpointCount(); epi++)              {                UsbEndpoint mEndpoint = mUsbInterface.getEndpoint(epi);                returnValue = returnValue + "    ++++   ++++   ++++\r\n";                returnValue = returnValue + "    Endpoint index: " + epi + "\r\n";                returnValue = returnValue + "    Attributes: " + mEndpoint.getAttributes() + "\r\n";                returnValue = returnValue + "    Direction: " + mEndpoint.getDirection() + "\r\n";                returnValue = returnValue + "    Number: " + mEndpoint.getEndpointNumber() + "\r\n";                returnValue = returnValue + "    Interval: " + mEndpoint.getInterval() + "\r\n";                returnValue = returnValue + "    Packet size: " + mEndpoint.getMaxPacketSize() + "\r\n";                returnValue = returnValue + "    Type: " + mEndpoint.getType() + "\r\n";              }            }            returnValue = returnValue + "\n";            returnValue = returnValue + "\n";          }        }        else if (device.getVendorId() != 3118);        if (all == true)        {          returnValue = returnValue + "Model: " + device.getDeviceName() + "\r\n";          returnValue = returnValue + "ID: " + device.getDeviceId() + "\r\n";          returnValue = returnValue + "Class: " + device.getDeviceClass() + "\r\n";          returnValue = returnValue + "Protocol: " + device.getDeviceProtocol() + "\r\n";          returnValue = returnValue + "Vendor ID " + device.getVendorId() + "\r\n";          returnValue = returnValue + "Product ID: " + device.getProductId() + "\r\n";          returnValue = returnValue + "Interface count: " + device.getInterfaceCount() + "\r\n";          returnValue = returnValue + "---------------------------------------\r\n";          for (int index = 0; index < device.getInterfaceCount(); index++)          {            UsbInterface mUsbInterface = device.getInterface(index);            returnValue = returnValue + "  *****     *****\r\n";            returnValue = returnValue + "  Interface index: " + index + "\r\n";            returnValue = returnValue + "  Interface ID: " + mUsbInterface.getId() + "\r\n";            returnValue = returnValue + "  Inteface class: " + mUsbInterface.getInterfaceClass() + "\r\n";            returnValue = returnValue + "  Interface protocol: " + mUsbInterface.getInterfaceProtocol() + "\r\n";            returnValue = returnValue + "  Endpoint count: " + mUsbInterface.getEndpointCount() + "\r\n";            for (int epi = 0; epi < mUsbInterface.getEndpointCount(); epi++)            {              UsbEndpoint mEndpoint = mUsbInterface.getEndpoint(epi);              returnValue = returnValue + "    ++++   ++++   ++++\r\n";                                                                                                                                                                           returnValue = returnValue + "    Endpoint index: " + epi + "\r\n";              returnValue = returnValue + "    Attributes: " + mEndpoint.getAttributes() + "\r\n";              returnValue = returnValue + "    Direction: " + mEndpoint.getDirection() + "\r\n";              returnValue = returnValue + "    Number: " + mEndpoint.getEndpointNumber() + "\r\n";              returnValue = returnValue + "    Interval: " + mEndpoint.getInterval() + "\r\n";              returnValue = returnValue + "    Packet size: " + mEndpoint.getMaxPacketSize() + "\r\n";              returnValue = returnValue + "    Type: " + mEndpoint.getType() + "\r\n";            }          }        }      }      textView.setText(returnValue);        return returnValue;    }

    转载请注明原文地址: https://ju.6miu.com/read-12649.html

    最新回复(0)