簡體   English   中英

將usb標簽與usbDevice usb4java關聯

[英]associate usb label with usbDevice usb4java

我必須知道特定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);  
}

看來您嘗試將USB記憶棒連接到Windows操作系統。 我建議遍歷所有USB設備,並檢查“ USB類”是否為存儲棒(大容量存儲,類8)( 請參見此處 )。

您介意為我們提供有關您的項目的更多詳細信息嗎?

程式碼片段

這種代碼的安寧找到了一個附加的大容量存儲設備。 它沒有經過很好的測試或評論。

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高級API
USB4Java 低層API
libusb-1.0 項目主頁
Java.net概述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM