4
votes

I have tried writing primitive activity for scanning USB port and displaying basic info for attached device. I am specifically interested in reading device class, which I believe UsbDevice.getDeviceClass () method is intended for. 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 doesn't work as expected, giving 0 for any device I connect. Many other fields of UsbDevice object, like subclass or protocol, are also 0. How can I get device class then?

1

1 Answers

4
votes

USB class is the attribute of interface, not device. Iterating interfaces works as expected:

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