Skip to content

🎮 HID API for Java (like hidapi, jna, currently for mac only)

License

Notifications You must be signed in to change notification settings

umjammer/hid4java

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Release Java CI CodeQL Java Parent

hid4java

logo

🎮 hid api for java

  • by jna directory instead of hidapi
  • with jinput spi

Install

Usage

References

LESSON

  • sigabrt __pthread_kill+0x8 ... suspect using java heap as native memory, like using byte[] for a parameter type that should be Pointer
  • the context object for callback become wierd after a while ... suspect data is garbage collected

TODO

  • sigabrt ... -Djbr.catch.SIGABRT=true (maybe only for jetbrain's jvm)
  • npe on close
  • windows/linux not tested yet
  • dig into the difference between static and instance method reference
  • spi attach/detach controller
  • gyro
  • reduce classes. compare with purejavahidpai, there are too many classes
  • deadzone

The hid4java project supports USB HID devices through a common API which is provided here under the MIT license. The API is very simple but provides great flexibility such as support for feature reports and blocking reads with timeouts. Attach/detach events are provided to allow applications to respond instantly to device availability.

Telegram group

If you want to discuss hid4java in general please use the Telegram chat. I can't guarantee an instant response but I'm usually active on Telegram during office hours in the GMT timezone.

Remember to check the Wiki first before asking questions to avoid causing frustration!

Technologies

  • hidapi - Native USB HID library for multiple platforms
  • JNA - to remove the need for Java Native Interface (JNI) and greatly simplify the project
  • dockcross - Cross-compilation environments for multiple platforms to create hidapi libraries
  • Java 17+ - to remove dependencies on JVMs that have reached end of life

Install

Code example

Taken from UsbHidEnumerationExample which provides more details.

// Configure to use custom specification
HidServicesSpecification hidSpecification = new HidServicesSpecification();

// Use the v0.7.0 manual start feature to get immediate attach events
hidSpecification.setAutoStart(false);

// Get HID services using custom specification
HidServices hidDevices = HidManager.getHidServices(hidSpecification);
hidDevices.addHidServicesListener(this);

// Manually start the services to get attachment event
hidDevices.start();

// Provide a list of attached devices
for (HidDevice hidDevice : hidDevices.getAttachedHidDevices()) {
  System.out.println(hidDevice);
}
    

More information

Much of the information previously in this README has been migrated to the project Wiki as it was getting rather long. Here are some useful jumping off points that should help:

  • Home - The wiki Home page with lots of useful launch points
  • FAQ - Frequently asked questions
  • Examples - Using the examples to kickstart your own project
  • Troubleshooting - A comprehensive troubleshooting guide

Closing notes

All trademarks and copyrights are acknowledged.

Many thanks to victorix who provided the basis for this library. Please see the inspiration on the mbed.org site.

Thanks also go to everyone who has contributed their knowledge and advice during the creation and subsequent improvement of this library.


image by pngwing.com