Aren\'t there any system calls or OS specific functions that can be called by Java to get just the names of the USB devices attached?
I\'ve seen probably 6-7 questio
You can use the jUsb
API, for Linux.
Or you could launch the terminal
in Linux using the Process
class, and run
ls -la /dev/disk/by-id/usb-*
and catch the stdout
to know the results.
For Windows, you can try this : How to find my USB flash drive's path with PowerShell
EDIT:
For Windows, another helpful utility is the devcon.exe
.
For more info, check this.
EDIT 2:
For Mac, you could launch the terminal
using the Process
class, and run system_profiler SPUSBDataType
Maybe things have improved since you first asked this question. I recently began exploring usb4java on the Mac, and it seems to work. There is example code available, both for the low-level (libusb-like) API, and the high-level (javax) API.
To list all USB-devices, look the examples.
I downloaded all libraries directly from usb4java.org and the examples from github. I did not manage to get the maven build working, but I could import the libraries and examples in Eclipse and run them all.
There are same native code included in usb4java, but the library wraps them all quite beautifully and hides all the messy details, only extracting and deploying the native code at demand.
Yoy can try javahidapi. I think it some c/c++ code and JNI. Declarated linux, mac and windows support. I have tried it with linux (ok), with clean windows in virtual box (not ok, UnsatisfiedLinkError, i think some MSVS libs was missed). If you'll compile it from source, it should work, i belive.
here is example:
import com.codeminders.hidapi.HIDDeviceInfo;
import com.codeminders.hidapi.HIDManager;
public class TestHid {
public static void main(String[] args) throws Exception {
try {
com.codeminders.hidapi.ClassPathLibraryLoader.loadNativeHIDLibrary();
HIDManager hidManager = HIDManager.getInstance();
HIDDeviceInfo[] infos = hidManager.listDevices();
for (HIDDeviceInfo info : infos) {
System.out.println("info: " + info.toString());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
EDIT output shows only one plugged in usb device, genius laser mouse.
[grigory@gr testRSA]$ pwd
/home/grigory/testRSA/out/production/testRSA
[grigory@gr testRSA]$ whoami
grigory
[grigory@gr testRSA]$ java -cp ".:hidapi-1.1.jar" Test
libusb couldn't open USB device /dev/bus/usb/003/002: Permission denied.
libusb requires write access to USB device nodes.
info:HIDDeviceInfo [path=0003:0002:00, vendor_id=1112, product_id=58, serial_number=null, release_number=0, manufacturer_string=null, product_string=null, usage_page=0, usage=0, interface_number=0]
[grigory@gr testRSA]$ sudo java -cp ".:hidapi-1.1.jar" Test
[sudo] password for grigory:
info:HIDDeviceInfo [path=0003:0002:00, vendor_id=1112, product_id=58, serial_number=null, release_number=0, manufacturer_string=Genius, product_string=Laser Mouse, usage_page=0, usage=0, interface_number=0]
[grigory@gr testRSA]$
and for fresh Windows XP it isn't work (only one windows i can find. I haven't Visual Studio for compile lib from source):
E:\testRSA\out\production\testRSA>java -cp ".;hidapi-1.1.jar" -Djava.library.pat
h="e:\testRSA\out\production\testRSA" Test
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.codeminders.hidap
i.HIDManager.init()V
at com.codeminders.hidapi.HIDManager.init(Native Method)
at com.codeminders.hidapi.HIDManager.<init>(HIDManager.java:53)
at com.codeminders.hidapi.HIDManager.getInstance(HIDManager.java:121)
at Test.main(Test.java:14)