Package org.hid4java

Class HidDevice

java.lang.Object
org.hid4java.HidDevice

public class HidDevice
extends Object

High level wrapper to provide the following to API consumers:

  • Simplified access to the underlying JNA HidDeviceStructure
Since:
0.0.1  
  • Constructor Details

    • HidDevice

      public HidDevice​(HidDeviceInfoStructure infoStructure, org.hid4java.HidDeviceManager hidDeviceManager)
      Parameters:
      infoStructure - The HID device info structure providing details
      hidDeviceManager - The HID device manager providing access to device enumeration for post IO scanning
  • Method Details

    • getId

      public String getId()
      The "path" is well-supported across Windows, Mac and Linux so makes a better choice for a unique ID See #8 for details
      Returns:
      A unique device ID made up from vendor ID, product ID and serial number
    • getPath

      public String getPath()
    • getVendorId

      public int getVendorId()
      Returns:
      Int version of vendor ID
    • getProductId

      public int getProductId()
      Returns:
      Int version of product ID
    • getSerialNumber

      public String getSerialNumber()
    • getReleaseNumber

      public int getReleaseNumber()
    • getManufacturer

      public String getManufacturer()
    • getProduct

      public String getProduct()
    • getUsagePage

      public int getUsagePage()
    • getUsage

      public int getUsage()
    • getInterfaceNumber

      public int getInterfaceNumber()
    • open

      public boolean open()

      Open this device and obtain a device structure

      Returns:
      True if the device was successfully opened
    • isOpen

      public boolean isOpen()
      Returns:
      True if the device structure is present
    • close

      public void close()

      Close this device freeing the HidApi resources

    • setNonBlocking

      public void setNonBlocking​(boolean nonBlocking)

      Set the device handle to be non-blocking

      In non-blocking mode calls to hid_read() will return immediately with a value of 0 if there is no data to be read. In blocking mode, hid_read() will wait (block) until there is data to read before returning

      Non-blocking can be turned on and off at any time

      Parameters:
      nonBlocking - True if non-blocking mode is required
    • read

      public int read​(byte[] data)

      Read an Input report from a HID device

      Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports

      Parameters:
      data - The buffer to read into
      Returns:
      The actual number of bytes read and -1 on error. If no packet was available to be read and the handle is in non-blocking mode, this function returns 0.
    • read

      public Byte[] read​(int amountToRead, int timeoutMillis)

      Read an Input report from a HID device

      Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports

      Parameters:
      amountToRead - the number of bytes to read
      timeoutMillis - The number of milliseconds to wait before giving up
      Returns:
      a Byte array of the read data
    • read

      public Byte[] read​(int amountToRead)

      Read an Input report from a HID device

      Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports

      Parameters:
      amountToRead - the number of bytes to read
      Returns:
      a Byte array of the read data
    • read

      public Byte[] read()

      Read an Input report from a HID device

      Input reports are returned to the host through the INTERRUPT IN endpoint. The first byte will contain the Report number if the device uses numbered reports

      Returns:
      a Byte array of the read data
    • read

      public int read​(byte[] bytes, int timeoutMillis)

      Read an Input report from a HID device with timeout

      Parameters:
      bytes - The buffer to read into
      timeoutMillis - The number of milliseconds to wait before giving up
      Returns:
      The actual number of bytes read and -1 on error. If no packet was available to be read within the timeout period returns 0.
    • getFeatureReport

      public int getFeatureReport​(byte[] data, byte reportId)

      Get a feature report from a HID device

      Under the covers the HID library will set the first byte of data[] to the Report ID of the report to be read. Upon return, the first byte will still contain the Report ID, and the report data will start in data[1]

      This method handles all the wide string and array manipulation for you

      Parameters:
      data - The buffer to contain the report
      reportId - The report ID (or (byte) 0x00)
      Returns:
      The number of bytes read plus one for the report ID (which has been removed from the first byte), or -1 on error.
    • sendFeatureReport

      public int sendFeatureReport​(byte[] data, byte reportId)

      Send a Feature report to the device

      Under the covers, feature reports are sent over the Control endpoint as a Set_Report transfer. The first byte of data[] must contain the Report ID. For devices which only support a single report, this must be set to 0x0. The remaining bytes contain the report data

      Since the Report ID is mandatory, calls to hid_send_feature_report() will always contain one more byte than the report contains. For example, if a hid report is 16 bytes long, 17 bytes must be passed to hid_send_feature_report(): the Report ID (or 0x0, for devices which do not use numbered reports), followed by the report data (16 bytes). In this example, the length passed in would be 17

      This method handles all the array manipulation for you

      Parameters:
      data - The feature report data (will be widened and have the report ID pre-pended)
      reportId - The report ID (or (byte) 0x00)
      Returns:
      This function returns the actual number of bytes written and -1 on error.
    • getIndexedString

      public String getIndexedString​(int index)

      Get a string from a HID device, based on its string index

      Parameters:
      index - The index
      Returns:
      The string
    • write

      public int write​(byte[] message, int packetLength, byte reportId)
    • getLastErrorMessage

      public String getLastErrorMessage()
    • isVidPidSerial

      public boolean isVidPidSerial​(int vendorId, int productId, String serialNumber)
      Parameters:
      vendorId - The vendor ID
      productId - The product ID
      serialNumber - The serial number
      Returns:
      True if the device matches the given the combination with vendorId, productId being zero acting as a wildcard
    • equals

      public boolean equals​(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object