Class 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 Summary
Constructors Constructor Description HidDevice(HidDeviceInfoStructure infoStructure, org.hid4java.HidDeviceManager hidDeviceManager)
-
Method Summary
Modifier and Type Method Description void
close()
Close this device freeing the HidApi resourcesboolean
equals(Object o)
int
getFeatureReport(byte[] data, byte reportId)
Get a feature report from a HID deviceString
getId()
The "path" is well-supported across Windows, Mac and Linux so makes a better choice for a unique ID See #8 for detailsString
getIndexedString(int index)
Get a string from a HID device, based on its string indexint
getInterfaceNumber()
String
getLastErrorMessage()
String
getManufacturer()
String
getPath()
String
getProduct()
int
getProductId()
int
getReleaseNumber()
String
getSerialNumber()
int
getUsage()
int
getUsagePage()
int
getVendorId()
int
hashCode()
boolean
isOpen()
boolean
isVidPidSerial(int vendorId, int productId, String serialNumber)
boolean
open()
Open this device and obtain a device structureByte[]
read()
Read an Input report from a HID deviceint
read(byte[] data)
Read an Input report from a HID deviceint
read(byte[] bytes, int timeoutMillis)
Read an Input report from a HID device with timeoutByte[]
read(int amountToRead)
Read an Input report from a HID deviceByte[]
read(int amountToRead, int timeoutMillis)
Read an Input report from a HID deviceint
sendFeatureReport(byte[] data, byte reportId)
Send a Feature report to the devicevoid
setNonBlocking(boolean nonBlocking)
Set the device handle to be non-blockingString
toString()
int
write(byte[] message, int packetLength, byte reportId)
-
Constructor Details
-
HidDevice
public HidDevice(HidDeviceInfoStructure infoStructure, org.hid4java.HidDeviceManager hidDeviceManager)- Parameters:
infoStructure
- The HID device info structure providing detailshidDeviceManager
- The HID device manager providing access to device enumeration for post IO scanning
-
-
Method Details
-
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
-
getVendorId
public int getVendorId()- Returns:
- Int version of vendor ID
-
getProductId
public int getProductId()- Returns:
- Int version of product ID
-
getSerialNumber
-
getReleaseNumber
public int getReleaseNumber() -
getManufacturer
-
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
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 readtimeoutMillis
- The number of milliseconds to wait before giving up- Returns:
- a Byte array of the read data
-
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
- Parameters:
amountToRead
- the number of bytes to read- Returns:
- a Byte array of the read data
-
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 intotimeoutMillis
- 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 reportreportId
- 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
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
-
isVidPidSerial
- Parameters:
vendorId
- The vendor IDproductId
- The product IDserialNumber
- The serial number- Returns:
- True if the device matches the given the combination with vendorId, productId being zero acting as a wildcard
-
equals
-
hashCode
public int hashCode() -
toString
-