just get the names of USB devices attached to a system?

后端 未结 3 1892
广开言路
广开言路 2020-12-19 09:10

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

3条回答
  •  执念已碎
    2020-12-19 09:50

    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.(HIDManager.java:53)
            at com.codeminders.hidapi.HIDManager.getInstance(HIDManager.java:121)
            at Test.main(Test.java:14)
    

提交回复
热议问题