Package org.hid4java

Class HidDevice


  • public class HidDevice
    extends java.lang.Object

    High level wrapper to provide the following to API consumers:

    • Simplified access to the underlying JNA HidDeviceStructure
    Since:
    0.0.1  
    • Field Detail

      • path

        private java.lang.String path
      • vendorId

        private short vendorId
      • productId

        private short productId
      • serialNumber

        private java.lang.String serialNumber
      • releaseNumber

        private int releaseNumber
      • manufacturer

        private java.lang.String manufacturer
      • product

        private java.lang.String product
      • usagePage

        private int usagePage
      • usage

        private int usage
      • interfaceNumber

        private int interfaceNumber
    • Constructor Detail

      • HidDevice

        public HidDevice​(HidDeviceInfoStructure infoStructure,
                         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 Detail

      • getId

        public java.lang.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 java.lang.String getPath()
      • getVendorId

        public short getVendorId()
      • getProductId

        public short getProductId()
      • getSerialNumber

        public java.lang.String getSerialNumber()
      • getReleaseNumber

        public int getReleaseNumber()
      • getManufacturer

        public java.lang.String getManufacturer()
      • getProduct

        public java.lang.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 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 java.lang.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 java.lang.String getLastErrorMessage()
      • isVidPidSerial

        public boolean isVidPidSerial​(int vendorId,
                                      int productId,
                                      java.lang.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
      • equals

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

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object