0

i have to know the product id and vendor id of a specific usb device.
I can retrieve all usb devices id but i don t know how i can associate them with their own label ("F:"). This is my code for finding usb devices id:

List perepheriques = hub.getAttachedUsbDevices();
Iterator iterator = perepheriques.iterator();
while (iterator.hasNext()) {
  UsbDevice perepherique = (UsbDevice) iterator.next();
  perepherique.getUsbDeviceDescriptor();
  System.out.println(perepherique);  
}
Sceik
  • 275
  • 1
  • 3
  • 13

1 Answers1

0

It looks like you try to connect a USB stick to your Windows OS. I suggest, iterating over all USB devices and check, if the "USB class" is a stick (mass storage, class 8) (see here).

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 LowLevel API
libusb-1.0 Project Homepage
Great Overview on Java.net

Markus
  • 763
  • 7
  • 24