public class HidApi extends Object
JNA utility class to provide the following to low level operations:
Constructor and Description |
---|
HidApi() |
Modifier and Type | Method and Description |
---|---|
static void |
close(HidDeviceStructure device)
Close a HID device
|
static HidDeviceInfoStructure |
enumerateDevices(int vendor,
int product)
Enumerate the attached HID devices
|
static void |
exit()
Finalise the HID API library
|
static void |
freeEnumeration(HidDeviceInfoStructure list)
Free an enumeration linked list
|
static int |
getFeatureReport(HidDeviceStructure device,
byte[] data,
byte reportId)
Get a feature report from a HID device
|
static String |
getIndexedString(HidDeviceStructure device,
int idx)
Get a string from a HID device, based on its string index
|
static String |
getLastErrorMessage(HidDeviceStructure device) |
static String |
getManufacturer(HidDeviceStructure device) |
static String |
getProductId(HidDeviceStructure device) |
static String |
getSerialNumber(HidDeviceStructure device) |
static void |
init()
Initialise the HID API library
|
static HidDeviceStructure |
open(int vendor,
int product,
String serialNumber)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number
|
static HidDeviceStructure |
open(String path)
Open a HID device by its path name
|
static int |
read(HidDeviceStructure device,
byte[] buffer)
Read an Input report from a HID device
|
static int |
read(HidDeviceStructure device,
byte[] buffer,
int timeoutMillis)
Read an Input report from a HID device with timeout
|
static int |
sendFeatureReport(HidDeviceStructure device,
byte[] data,
byte reportId)
Send a Feature report to the device using a simplified interface
|
static boolean |
setNonBlocking(HidDeviceStructure device,
boolean nonBlocking)
Set the device handle to be non-blocking
|
static int |
write(HidDeviceStructure device,
byte[] data,
int len,
byte reportId)
Write an Output report to a HID device using a simplified interface
|
public static HidDeviceStructure open(int vendor, int product, String serialNumber)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number
vendor
- The vendor IDproduct
- The product IDserialNumber
- The serial numberpublic static void init()
Initialise the HID API library
Required if the consuming application is using multiple threads containing device handles.
public static void exit()
Finalise the HID API library
public static HidDeviceStructure open(String path)
Open a HID device by its path name
path
- The device path (e.g. "0003:0002:00")public static void close(HidDeviceStructure device)
Close a HID device
public static HidDeviceInfoStructure enumerateDevices(int vendor, int product)
Enumerate the attached HID devices
vendor
- The vendor IDproduct
- The product IDpublic static void freeEnumeration(HidDeviceInfoStructure list)
Free an enumeration linked list
list
- The list to freepublic static String getLastErrorMessage(HidDeviceStructure device)
public static String getManufacturer(HidDeviceStructure device)
device
- The HID devicepublic static String getProductId(HidDeviceStructure device)
device
- The HID devicepublic static String getSerialNumber(HidDeviceStructure device)
device
- The HID devicepublic static boolean setNonBlocking(HidDeviceStructure device, 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
device
- The HID devicenonBlocking
- True if non-blocking mode is requiredpublic static int read(HidDeviceStructure device, byte[] buffer)
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 ID if the device uses numbered reports.
device
- The HID devicebuffer
- The buffer to read into (allow an extra byte if device supports multiple report IDs)public static int read(HidDeviceStructure device, byte[] buffer, int timeoutMillis)
Read an Input report from a HID device with timeout
device
- The HID devicebuffer
- The buffer to read intotimeoutMillis
- The number of milliseconds to wait before giving uppublic static int getFeatureReport(HidDeviceStructure device, 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
device
- The HID devicedata
- The buffer to contain the reportreportId
- The report ID (or (byte) 0x00)public static int sendFeatureReport(HidDeviceStructure device, byte[] data, byte reportId)
Send a Feature report to the device using a simplified interface
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 0x00, for devices which do not use numbered reports), followed by the report data (16 bytes). In this example, the bytes written would be 17.
This method handles all the array manipulation for you
device
- The HID devicedata
- The feature report data (will be widened and have the report ID pre-pended)reportId
- The report ID (or (byte) 0x00)public static int write(HidDeviceStructure device, byte[] data, int len, byte reportId)
Write an Output report to a HID device using a simplified interface
In USB HID the first byte of the data packet must contain the Report ID.
For devices which only support a single report, this must be set to 0x00.
The remaining bytes contain the report data. Since the Report ID is mandatory,
calls to hid_write()
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_write()
,
the Report ID (or 0x00, for devices with a single report), followed by the report data (16 bytes).
In this example, the length passed in would be 17
hid_write()
will send the data on the first OUT endpoint, if one exists.
If it does not, it will send the data through the Control Endpoint (Endpoint 0)
device
- The devicedata
- The report data to write (should not include the Report ID)len
- The length of the report data (should not include the Report ID)reportId
- The report ID (or (byte) 0x00)public static String getIndexedString(HidDeviceStructure device, int idx)
Get a string from a HID device, based on its string index
device
- The HID deviceidx
- The indexCopyright © 2014–2015 Bitcoin Solutions Ltd. All rights reserved.