我必须知道特定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);
}发布于 2014-04-02 17:06:47
看起来您正在尝试将U盘连接到Windows操作系统。我建议,遍历所有的USB设备,检查"USB class“是否是一个棒(海量存储,class 8) (see here)。
你介意给我们更多关于你的项目的细节吗?
代码片段
这段代码会找到一个附加的大容量存储设备。它没有经过良好的测试,也没有得到充分的评论。
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;
}}
有用的链接
USB4Java HighLevel API
USB4Java LowLevel API
libusb 1.0 Project Homepage
关于Java.net的伟大概述
https://stackoverflow.com/questions/21220548
复制相似问题