Android SDK: how to read USB device class?

I tried to write primitive activity to scan a USB port and display basic information for a connected device. I am particularly interested in reading the device class that I rely on the UsbDevice.getDeviceClass () method . Here's what it looks like:

HashMap<String, UsbDevice> deviceList = findUsbDevices(); Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); if (deviceIterator.hasNext()) { UsbDevice device = deviceIterator.next(); String name = device.toString(); String cls = Integer.toString(device.getDeviceClass()); displayDeviceInfo(name, cls); } 

However, it does not work as expected, giving 0 for any connected device. Many other fields of the UsbDevice object, such as a subclass or protocol, are also equal to 0. How can I get a device class?

+4
source share
1 answer

The USB class is an attribute of the interface, not a device. Iterative interfaces work as expected:

 int count = device.getInterfaceCount(); for (int i = 0; i < count; i++) { UsbInterface iface = device.getInterface(i); int cls = iface.getInterfaceClass(); } 
+3
source

All Articles