Issue
Im new to java and am just wondering if you can receive a list of connected USB devices, for example:
If i created a java program to modify devices, i want the JFrame to display a list of devices for the user to select one... (That's not my idea, or my intention but that is the best example i can think of, sorry.)
Solution
Using the javax.usb
library, we can get information about the UsbHostManager
.
import java.util.*;
import javax.usb.*;
public class USBLister {
public static void main(String[] args) throws UsbException {
//Get UsbHub
UsbServices services = UsbHostManager.getUsbServices();
UsbHub root = services.getRootUsbHub();
listPeripherique(root);
}
public static void listPeripherique(UsbHub hub) {
//List all the USBs attached
List perepheriques = hub.getAttachedUsbDevices();
Iterator iterator = perepheriques.iterator();
while (iterator.hasNext()) {
UsbDevice perepherique = (UsbDevice) iterator.next();
System.out.println(perepherique);
if (perepherique.isUsbHub())
listPeripherique((UsbHub) perepherique);
}
}
}
Answered By - jado