【问题标题】:associate usb label with usbDevice usb4java将 usb 标签与 usbDevice usb4java 关联
【发布时间】:2014-01-19 18:06:23
【问题描述】:

我必须知道特定 USB 设备的产品 ID 和供应商 ID。
我可以检索所有 USB 设备 ID,但我不知道如何将它们与它们自己的标签(“F:”)相关联。 这是我查找 USB 设备 ID 的代码:

List perepheriques = hub.getAttachedUsbDevices();
Iterator iterator = perepheriques.iterator();
while (iterator.hasNext()) {
  UsbDevice perepherique = (UsbDevice) iterator.next();
  perepherique.getUsbDeviceDescriptor();
  System.out.println(perepherique);  
}

【问题讨论】:

    标签: java usb libusb


    【解决方案1】:

    您似乎尝试将 U 盘连接到您的 Windows 操作系统。我建议遍历所有 USB 设备并检查“USB 类”是否为记忆棒(大容量存储,8 类)(see here)。

    您介意向我们提供有关您项目的更多详细信息吗?

    代码 sn-p

    这个代码和平找到一个附加的大容量存储设备。它没有经过很好的测试也没有评论。

    import java.util.List;
    
    import javax.usb.UsbConfiguration;
    import javax.usb.UsbDevice;
    import javax.usb.UsbHostManager;
    import javax.usb.UsbHub;
    import javax.usb.UsbInterface;
    import javax.usb.UsbServices;
    
    public class USBHighLevel {
    
    public static void main(String[] args) throws Exception {
        UsbServices services = UsbHostManager.getUsbServices();
        UsbDevice usbDevice = findDevices(services.getRootUsbHub());
        System.out.println("Device=" + usbDevice);
    }
    
    private static UsbDevice findDevices(UsbHub node) {
        for (UsbDevice usbDevice: (List<UsbDevice>) node.getAttachedUsbDevices()) {
            if (usbDevice.isUsbHub()) {
                UsbDevice tmp =  findDevices((UsbHub) usbDevice);
                if(tmp != null) {
                    return tmp;
                }
            } else {
                if(matchesUSBClassType(usbDevice, (byte) 8)) {
                    return usbDevice;
                }
            }
        }
        return null;
    }
    
    private static boolean matchesUSBClassType(UsbDevice usbDevice, byte usbClassType) {
         boolean matchingType = false;
    
         UsbConfiguration config = usbDevice.getActiveUsbConfiguration();
         for (UsbInterface iface: (List<UsbInterface>) config.getUsbInterfaces()) {
             System.out.println(iface.getUsbInterfaceDescriptor().bInterfaceClass());
            if(iface.getUsbInterfaceDescriptor().bInterfaceClass() == usbClassType) {
                matchingType = true;
                break;
            }
         }
    
         return matchingType;
    }
    

    }

    有用的链接

    USB4JavaHighLevel API
    USB4JavaLowLevel API
    libusb-1.0 Project Homepage
    Java.net 的精彩概述

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2014-11-18
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      相关资源
      最近更新 更多