Just enter the names of the USB devices connected to the system?

Are there any system calls or specific OS functions that Java can call to get only the names of the connected USB devices?

I saw only 6-7 questions here, but everyone mentions C ++ GetRawInputDeviceList() functions, etc., and they are not cross-platform compatible. Either for Windows in C # or C ++, or only for Linux.

But I work in Java. In addition, it should be a cross platform. At least it should work on Windows, Linux, and Mac. I can also work with command line commands terminal / shell / command-prompt. I think I can run them with Java.

I tried getFileSystemView and listRoots . But they give the names of all drives [dvd, hdd, floppy, etc.].

I need to get only USB devices.

Please do not mention JUSB or JSR080. Why:

jUSB: accessing USB devices currently requires that they be connected to the GNU / Linux host system

javax.usb: The preliminary alpha version of Windows is not certified and requires a kernel driver.

usb4java: basically, it just implements JSR80 with a bit more abstraction, maybe

Although, to be honest, I have not tried libusb, since it is in C ++.

If you intend to mention the APIs, list the fully tested and verified ones that work on Linux, Windows, and Mac. If this were not so, I would not pose this question. I saw mention of jUSB, javax.usb, etc. On many other posts.

+6
source share
3 answers

You can use jUsb API for Linux.

Or you can run terminal on Linux using the Process class and run ls -la /dev/disk/by-id/usb-* and catch stdout to see the results.

On Windows, you can try the following: How to find the path to a USB drive using PowerShell

EDIT:

For Windows, another useful utility is devcon.exe .

See more details.

EDIT 2: For Mac, you can start terminal using the Process class and run system_profiler SPUSBDataType

+4
source

Yoy can try javahidapi . I think this is c / C ++ and JNI code. Support for linux, mac, and windows has been announced. I tried this with linux (ok), with clean windows in a virtual box (not ok, UnsatisfiedLinkError, I think some MSVS libraries were skipped). If you compile it from source, it should work, I believe.

here is an 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(); } } } 

CHANGE the output shows only one connected USB device, a brilliant 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 the new Windows XP this does not work (you can find only one Windows. I do not have Visual Studio to compile lib from the source code):

 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) 
+2
source

Maybe things have improved since you first asked this question. I recently started learning usb4java on Mac and it seems to work. Sample code is available for both the low-level (libusb-like) API and the high-level API (javax).

To list all USB devices, see examples .

I downloaded all the libraries directly from usb4java.org and examples from github. I was not able to get maven to work, but I could import the libraries and examples into Eclipse and run them all.

There is one and the same native code in usb4java, but the library wraps them pretty nicely and hides all the messy details, only extracts and deploys its own code on demand.

0
source

All Articles