Java and HID Communication

旧巷老猫 提交于 2019-12-30 07:40:39

问题


I'm looking to write a Linux/Windows/Mac Java HID controller for a simple wireless HID interface device. I've tinkered around with the USB4Java LibUsb library to no avail, and I've been steered in the direction of the JavaHIDAPI.

Unfortunately for me, I really have no idea what I'm doing beyond a pretty decent higher level programming skill set (C#/VB.Net), and switching over to this is killing me.

The directions that I found said that I would first need to compile the HIDAPI library found here. It said it would build something called hidapi-jni.dll (which it didn't).

Anyway, I think this is where I need to go since the HID does nothing but simply sends and receives signals to and from a wireless control (and responds to signals received).

Is there any step-by-step tutorial for using the JavaHIDAPI for this? Or is there a better library? (I noticed that this is a bit... dated).

I tried myself with the following:

static{ System.loadLibrary("hidapi"); }
.
.
.
public static void main(string[] Args) throws . . . {    
    ClassPathLibraryLoader.loadNativeHIDLibrary();
    HIDDevice dev = HIDManager.getInstance().openById(VEND_ID, PROD_ID, null);
    .
    .
    .
}

The issue into which I am running is that (besides the fact that the HIDAPI doesnt seem to be building into HIDAPI-jni.dll) is that the HIDManager.openById(. . .) always returns null. Also, I moved the hidapi.dll that was built into the C:...\Java...\bin directory... or something, I'm sure it's right because there wasn't any unsatisfied link error. It's really frustrating because there doesn't seem to be any newbies guide to Java and HID anywhere.

What am I doing wrong here?


回答1:


I'm a bit late to answer this question, but I've written a library called hid4java that might solve your problem.

It's based on Java Native Access (JNA) which is a lot simpler than faffing about with complex JNI incantations.

I had to write it because javahidapi is out of date and the underlying signal11/hidapi code has moved on considerably. The older versions of the hidapi library had problems on OS X with attach/detach events which have now been fixed so an upgrade is necessary.

Also I found it impossible to claim a USB HID device through usb4java on OS X so I ended up writing this library to solve that problem.

Hope it helps.




回答2:


So the issue, I discovered, was with native libraries. I was able to get the application to work by copying the .dll from the .jar file and referencing it, but more important, I'm going to rebuild the .java class file responsible for loading the library and add the

System.loadLibrary();

call. When the JavaHidApi ClassPathLibraryLoader.loadNativeHIDLibrary(); method is called it doesn't load the library upon successfully writing it out to the temp file which is mildly annoying. Doing this will eliminate the necessity for manually loading the library from a static location...

Thank you for pointing me in the correct direction.



来源:https://stackoverflow.com/questions/23796353/java-and-hid-communication

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!