Skip to content
Gary Rowe edited this page Oct 1, 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"

If you have a FIDO2 U2F authentication device (e.g. a HyperFIDO, Yubikey, Solokey, Trezor or Ledger) you may wish to explore its capabilities using this example. Simply plug it in and run the example to see the initial handshake to select a channel and basic device information.

You can see some of hid4java features in use such as:

  • device enumeration and selection
  • automatic data read events (new for 0.8.0)
  • low level HID traffic to System.out to assist early stage debugging
  • manual start to enable attach/detach events

Use the following command to try it out:

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

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"

UsbHidDeviceExample (removed in 0.8.0)

This example was removed as the Trezor devices were transitioned away from hidapi to libusb as part of a general firmware upgrade so only old devices with firmware below 1.7.1 are likely to respond to this code.

One of the earliest uses for hid4java was to interact with SatoshiLabs 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 a few seconds for it to finish.