HidDevice
class HidDevice : AutoCloseable
| kotlin.Any | |
| ↳ | android.hardware.hid.HidDevice |
Represents a Human Interface Device (HID) connected to the system.
HIDs are devices like keyboards, mice, game controllers, and other peripherals that follow the HID specification for communication over various transports (USB, Bluetooth, etc.).
Summary
| Constants | |
|---|---|
| static Int |
Transport type is Bluetooth. |
| static Int |
Transport type is I2C. |
| static Int |
Transport type is SPI. |
| static Int |
Transport type is unknown. |
| static Int |
Transport type is USB. |
| static Int |
Transport type is Virtual. |
| Public methods | |
|---|---|
| Unit |
close()Releases all system resources and closes the connection to the HID device. |
| Boolean |
Indicates whether some other object is "equal to" this one. |
| Unit |
getFeatureReport(reportId: Int, executor: Executor, callback: OutcomeReceiver<Report!, Exception!>)Get a feature report from the HID device. |
| String |
getName()The human-readable name of the HID device. |
| String |
The physical address of the device, representing its connection path through the system's device tree (e.g., the USB hub and port path). |
| Int |
The product ID for this device. |
| ByteArray |
The raw HID report descriptor for the device. |
| Int |
The transport type used by this device. |
| String |
A unique identifier for this device instance. |
| Int |
The vendor ID for this device. |
| Int |
hashCode() |
| Boolean |
isOpen()Returns whether the HID device is currently open and ready for communication. |
| Unit |
open(executor: Executor, callback: OutcomeReceiver<HidDevice!, Exception!>)Opens the HID device so that it can be used to send and receive HID reports. |
| Unit |
sendFeatureReport(report: Report, executor: Executor, callback: OutcomeReceiver<Void!, Exception!>)Send a feature report to the HID device. |
| Unit |
sendOutputReport(report: Report, executor: Executor, callback: OutcomeReceiver<Void!, Exception!>)Send an output report to the HID device. |
| String |
toString() |
Constants
Public methods
close
fun close(): Unit
Releases all system resources and closes the connection to the HID device.
This method is synchronous and idempotent; calling it on an already closed device has no effect.
Requires android.Manifest.permission#ACCESS_HID
| Exceptions | |
|---|---|
java.lang.Exception |
if this resource cannot be closed |
equals
fun equals(other: Any?): Boolean
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value
x,x.equals(x)should returntrue. - It is symmetric: for any non-null reference values
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| Parameters | |
|---|---|
obj |
the reference object with which to compare. |
o |
This value may be null. |
| Return | |
|---|---|
Boolean |
true if this object is the same as the obj argument; false otherwise. |
getFeatureReport
fun getFeatureReport(
reportId: Int,
executor: Executor,
callback: OutcomeReceiver<Report!, Exception!>
): Unit
Get a feature report from the HID device.
The device must be opened first before calling this method.
On success, callback.onResult is called with the retrieved Report. On failure, callback.onError is called with one of the following:
IOException: If the underlying hardware device could not be accessed or the system service returned a failure.SecurityException: If the caller lacks the required permission.
Requires
android.Manifest.permission#ACCESS_HID
| Parameters | |
|---|---|
reportId |
Int: the feature report to get. |
executor |
Executor: the executor on which the callback will be invoked. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread. |
callback |
OutcomeReceiver<Report!, Exception!>: the callback object to be used to notify of the report or an error. This value cannot be null. |
| Exceptions | |
|---|---|
java.lang.IllegalStateException |
if the device is not open. |
getName
fun getName(): String
The human-readable name of the HID device.
| Return | |
|---|---|
String |
This value cannot be null. |
getPhysicalAddress
fun getPhysicalAddress(): String
The physical address of the device, representing its connection path through the system's device tree (e.g., the USB hub and port path).
This value is unique to the device's physical connection point on the current system.
| Return | |
|---|---|
String |
This value cannot be null. |
getProductId
fun getProductId(): Int
The product ID for this device.
A product ID uniquely identifies which product within the address space of a given vendor, identified by the device's vendor ID. A value of 0 will be assigned where a product ID is not available.
getReportDescriptor
fun getReportDescriptor(): ByteArray
The raw HID report descriptor for the device.
This descriptor defines the format and meaning of the reports sent and received by the device, following the HID specification.
| Return | |
|---|---|
ByteArray |
This value cannot be null. |
getTransport
fun getTransport(): Int
The transport type used by this device.
| Return | |
|---|---|
Int |
Value is one of the following: |
getUniqueId
fun getUniqueId(): String
A unique identifier for this device instance.
This value is typically the device's serial number if available, or a system-generated unique ID. This ID is unique among all currently connected HID devices.
| Return | |
|---|---|
String |
This value cannot be null. |
getVendorId
fun getVendorId(): Int
The vendor ID for this device.
A vendor ID uniquely identifies the company who manufactured the device. A value of 0 will be assigned where a vendor ID is not available.
hashCode
fun hashCode(): Int
isOpen
fun isOpen(): Boolean
Returns whether the HID device is currently open and ready for communication.
Note: This method returns false while the device is in the process of opening. It only returns true after the open operation has successfully completed.
| Return | |
|---|---|
Boolean |
true if the device is fully opened; false otherwise. |
open
fun open(
executor: Executor,
callback: OutcomeReceiver<HidDevice!, Exception!>
): Unit
Opens the HID device so that it can be used to send and receive HID reports.
This is an asynchronous operation. If an open operation is already in progress, the provided callback is queued and will be notified once the underlying operation completes. The state transitions are as follows:
- If the device is already opened,
callback.onResultis invoked immediately. - If the device is currently opening, the request is queued and
callbackwill be notified when the existing operation finishes. - If the device is closed, it transitions to an opening state and attempts to establish a connection via the system service.
On success, callback.onResult is called with this HidDevice instance. On failure, callback.onError is called with one of the following:
IOException: If the underlying hardware device (e.g., hidraw node) could not be accessed or the system service returned a failure.SecurityException: If the caller lacks the required permission, or the user has rejected access to the device.RemoteException: If the HID system service is unreachable.
Requires
android.Manifest.permission#ACCESS_HID
| Parameters | |
|---|---|
executor |
Executor: The executor on which the callback will be invoked. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread. |
callback |
OutcomeReceiver<HidDevice!, Exception!>: The object to be notified of success or failure. This value cannot be null. |
sendFeatureReport
fun sendFeatureReport(
report: Report,
executor: Executor,
callback: OutcomeReceiver<Void!, Exception!>
): Unit
Send a feature report to the HID device.
The device must be opened first before calling this method.
On success, callback.onResult is called with null. On failure, callback.onError is called with one of the following:
IOException: If the underlying hardware device could not be accessed or the system service returned a failure.SecurityException: If the caller lacks the required permission.
Requires
android.Manifest.permission#ACCESS_HID
| Parameters | |
|---|---|
report |
Report: the feature report to send. This value cannot be null. |
executor |
Executor: the executor on which the callback will be invoked. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread. |
callback |
OutcomeReceiver<Void!, Exception!>: the callback object to be used to notify of an error. This value cannot be null. |
| Exceptions | |
|---|---|
java.lang.IllegalStateException |
if the device is not open. |
sendOutputReport
fun sendOutputReport(
report: Report,
executor: Executor,
callback: OutcomeReceiver<Void!, Exception!>
): Unit
Send an output report to the HID device.
The device must be opened first before calling this method.
On success, callback.onResult is called with null. On failure, callback.onError is called with one of the following:
IOException: If the underlying hardware device could not be accessed or the system service returned a failure.SecurityException: If the caller lacks the required permission.
Requires
android.Manifest.permission#ACCESS_HID
| Parameters | |
|---|---|
report |
Report: the output report to send. This value cannot be null. |
executor |
Executor: the executor on which the callback will be invoked. This value cannot be null. Callback and listener events are dispatched through this Executor, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use Context.getMainExecutor(). Otherwise, provide an Executor that dispatches to an appropriate thread. |
callback |
OutcomeReceiver<Void!, Exception!>: the callback object to be used to notify of an error. This value cannot be null. |
| Exceptions | |
|---|---|
java.lang.IllegalStateException |
if the device is not open. |
toString
fun toString(): String