Skip to content
Gary Rowe edited this page Sep 20, 2020 · 3 revisions

The examples are able to be run from within an IDE and directly from the command line using Maven. This gives maximum flexibility when first discovering the project API and seeing it work within your own environment.

I'll assume you can execute standalone Java classes with their main() methods from your IDE and only provide the command line entries to avoid duplication.

The easiest way into the examples is through device enumeration.

mvn clean test exec:java -Dexec.classpathScope="test" -Dexec.mainClass="org.hid4java.examples.UsbHidEnumerationExample"

Use CTRL+C to quit the example or wait 30s.

One of the earliest uses for hid4java was to interact with Satoshi Labs Trezor devices and these have become fairly popular over time. If you happen to have one just plug it in and try out the following:

mvn clean test exec:java -Dexec.classpathScope="test" -Dexec.mainClass="org.hid4java.examples.UsbHidDeviceExample"

If you have a Trezor device attached you'll see a Features response message appear as a big block of hex otherwise it will be just a simple enumeration of attached USB devices. There is no data collection from the device, just the initial handshake via Initialise -> Features to get the basic device capabilities.

You can plug various devices in and out to see messages.

Use CTRL+C to quit the example or wait 30s.

If you are using an older version of Linux, you may find the need to access the older libusb interface. This was introduced in version 0.6.0 and is activated in the HidSpecification. If hid4java is executing on a Linux variant with this mode enable it will load the libusb version of the native hidapi library instead of the usual hidraw one.

mvn clean test exec:java -Dexec.classpathScope="test" -Dexec.mainClass="org.hid4java.examples.LibusbEnumerationExample"

Clone this wiki locally