我尝试编写原始活动来扫描 USB 端口并显示连接设备的基本信息。我对读取设备类特别感兴趣,我相信UsbDevice.getDeviceClass ()方法就是用于读取设备类的。它看起来是这样的:
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);
}
Run Code Online (Sandbox Code Playgroud)
但是,它没有按预期工作,对于我连接的任何设备都给出 0。UsbDevice 对象的许多其他字段,如子类或协议,也都是 0。那么我怎样才能获取设备类呢?
USB类是接口的属性,而不是设备的属性。迭代接口按预期工作:
int count = device.getInterfaceCount();
for (int i = 0; i < count; i++)
{
UsbInterface iface = device.getInterface(i);
int cls = iface.getInterfaceClass();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3171 次 |
最近记录: |