简体   繁体   English

将usb标签与usbDevice usb4java关联

[英]associate usb label with usbDevice usb4java

i have to know the product id and vendor id of a specific usb device. 我必须知道特定USB设备的产品ID和供应商ID。
I can retrieve all usb devices id but i don t know how i can associate them with their own label ("F:"). 我可以检索所有USB设备ID,但是我不知道如何将它们与自己的标签(“ F:”)相关联。 This is my code for finding usb devices id: 这是我找到USB设备ID的代码:

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

It looks like you try to connect a USB stick to your Windows OS. 看来您尝试将USB记忆棒连接到Windows操作系统。 I suggest, iterating over all USB devices and check, if the "USB class" is a stick (mass storage, class 8) ( see here ). 我建议遍历所有USB设备,并检查“ USB类”是否为存储棒(大容量存储,类8)( 请参见此处 )。

Do you mind giving us further details on your project? 您介意为我们提供有关您的项目的更多详细信息吗?

Code snippet 程式码片段

This peace of code finds a attached mass storage device. 这种代码的安宁找到了一个附加的大容量存储设备。 It is not well tested nor commented. 它没有经过很好的测试或评论。

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;
}

} }

Useful links 有用的链接

USB4Java HighLevel API USB4Java高级API
USB4Java LowLevel API USB4Java 低层API
libusb-1.0 Project Homepage libusb-1.0 项目主页
Great Overview on Java.net Java.net概述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM