开发者

Android SDK: how to read a USB device's class?

开发者 https://www.devze.com 2023-03-25 23:02 出处:网络
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.getDeviceCl

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 t开发者_JAVA技巧hen?


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消