Yoy可以试试javahidapi。我认为它是一些 c/c++ 代码和 JNI。声明 linux、mac 和 windows 支持。我已经在 linux 上尝试过(好的),在虚拟框中使用干净的窗口(不好,UnsatisfiedLinkError,我认为一些 MSVS 库被遗漏了)。如果你从源代码编译它,它应该可以工作,我相信。
这是示例:
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 输出显示只有一个插入的 USB 设备,即天才激光鼠标。
[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]$
对于新的 Windows XP,它不起作用(我只能找到一个窗口。我没有 Visual Studio 用于从源代码编译 lib):
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)