hid4java

repository·develop·Indexed 42 days ago

https://github.com/gary-rowe/hid4java

A Java library providing a cross-platform API for interacting with USB HID (Human Interface Device) devices. It leverages JNA and the native hidapi library to support feature reports, blocking reads with timeouts, and device attach/detach events.

Tokens
1.6K
Snippets
9
Records
10
Agent score
4%

What's inside hid4java

  1. Install hid4java via Gradle

    develop

    To use hid4java in a Gradle project, ensure mavenCentral() is in your repositories and add the implementation dependency.

    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation('org.hid4java:hid4java')
    }
  2. Build hid4java from source

    develop

    To build the project locally using Maven, clone the repository and run the mvn clean install command. The resulting JAR will be located in the target directory, and the install command will also place it in your local ~/.m2/repository for use in other local projects.

    cd <workspace>
    git clone https://github.com/gary-rowe/hid4java.git
    cd hid4java
    
    # Maven build
    mvn clean install
  3. Use Maven snapshots for latest development code

    develop

    If you need to use the latest development code (release candidates) from the Maven Central snapshot repository, you must add the Sonatype snapshot repository to your pom.xml.

      <!-- Enable Maven Central snapshots repository -->
    <repositories>
      <repository>
        <id>oss.sonatype.org-snapshot</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
          <enabled>false</enabled>
        </releases>
        <snapshots>
          <enabled>true</enabled>
        </snapshots>
      </repository>
    </repositories>
  4. Install hid4java via Maven

    develop

    To use hid4java in a Maven project, add the following dependency to your pom.xml. Note that the version in this example is 0.8.0; ensure you check for the latest stable version.

    <dependencies>
    
      <!-- hid4java for cross-platform HID USB -->
      <dependency>
        <groupId>org.hid4java</groupId>
        <artifactId>hid4java</artifactId>
        <version>0.8.0</version>
      </dependency>
    
    </dependencies>
  5. Manage HID devices with HidDeviceManager

    develop

    The HidDeviceManager is the core component responsible for managing HID services. It provides access to the underlying JNA and hidapi library, handles device discovery, and manages device attachment/detachment events.

    Key responsibilities include:

    • Device Discovery: Scanning for connected HID devices.
    • Event Handling: Detecting when devices are attached or detached (if configured).
    • Lifecycle Management: Starting and stopping the device scanning daemon.
    • Data Integration: Coordinating data read events and device write notifications.
  6. Start and stop the HID device scanning service

    develop

    Use start() to begin device discovery. If the scan interval in your HidServicesSpecification is greater than zero, start() will initiate a background daemon thread that periodically scans for changes.

    Use stop() to shut down the scanning thread and close all currently attached HidDevice instances. This is typically called during application shutdown to ensure resources are released and the attached devices map is cleared.

    // Assuming manager is already initialized
    manager.start();
    
    // ... perform operations ...
    
    manager.stop();
  7. Initialize and enumerate HID devices

    develop

    To use hid4java, you typically create a HidServicesSpecification, obtain HidServices via HidManager, and then start the services. You can add a listener to handle attach/detach events. Once started, you can retrieve a list of currently attached devices using getAttachedHidDevices().

    // Configure to use custom specification
    HidServicesSpecification hidServicesSpecification = new HidServicesSpecification();
    
    // Use the v0.7.0 manual start feature to get immediate attach events
    hidServicesSpecification.setAutoStart(false);
    
    // Get HID services using custom specification
    HidServices hidServices = HidManager.getHidServices(hidServicesSpecification);
    hidServices.addHidServicesListener(this);
    
    // Manually start the services to get attachment event
    hidServices.start();
    
    // Provide a list of attached devices
    for (HidDevice hidDevice : hidServices.getAttachedHidDevices()) {
      System.out.println(hidDevice);
    }
  8. Retrieve a list of all attached HID devices

    develop

    Call getAttachedHidDevices() to obtain a list of all currently connected HID devices. This method performs a fresh enumeration using the underlying hidapi library and wraps the results in HidDevice objects.

    List<HidDevice> devices = manager.getAttachedHidDevices();
    for (HidDevice device : devices) {
        System.out.println("Found device: " + device.getId());
    }