HidDevice


public final class HidDevice
extends Object implements AutoCloseable

java.lang.Object
   ↳ 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

int TRANSPORT_BLUETOOTH

Transport type is Bluetooth.

int TRANSPORT_I2C

Transport type is I2C.

int TRANSPORT_SPI

Transport type is SPI.

int TRANSPORT_UNKNOWN

Transport type is unknown.

int TRANSPORT_USB

Transport type is USB.

int TRANSPORT_VIRTUAL

Transport type is Virtual.

Public methods

void close()

Releases all system resources and closes the connection to the HID device.

boolean equals(Object o)

Indicates whether some other object is "equal to" this one.

void getFeatureReport(int reportId, Executor executor, OutcomeReceiver<ReportException> callback)

Get a feature report from the HID device.

String getName()

The human-readable name of the HID device.

String getPhysicalAddress()

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 getProductId()

The product ID for this device.

byte[] getReportDescriptor()

The raw HID report descriptor for the device.

int getTransport()

The transport type used by this device.

String getUniqueId()

A unique identifier for this device instance.

int getVendorId()

The vendor ID for this device.

int hashCode()

Returns a hash code value for the object.

boolean isOpen()

Returns whether the HID device is currently open and ready for communication.

void open(Executor executor, OutcomeReceiver<HidDeviceException> callback)

Opens the HID device so that it can be used to send and receive HID reports.

void sendFeatureReport(Report report, Executor executor, OutcomeReceiver<VoidException> callback)

Send a feature report to the HID device.

void sendOutputReport(Report report, Executor executor, OutcomeReceiver<VoidException> callback)

Send an output report to the HID device.

String toString()

Returns a string representation of the object.

Inherited methods

Constants

TRANSPORT_BLUETOOTH

public static final int TRANSPORT_BLUETOOTH

Transport type is Bluetooth.

Constant Value: 5 (0x00000005)

TRANSPORT_I2C

public static final int TRANSPORT_I2C

Transport type is I2C.

Constant Value: 24 (0x00000018)

TRANSPORT_SPI

public static final int TRANSPORT_SPI

Transport type is SPI.

Constant Value: 28 (0x0000001c)

TRANSPORT_UNKNOWN

public static final int TRANSPORT_UNKNOWN

Transport type is unknown.

Constant Value: 0 (0x00000000)

TRANSPORT_USB

public static final int TRANSPORT_USB

Transport type is USB.

Constant Value: 3 (0x00000003)

TRANSPORT_VIRTUAL

public static final int TRANSPORT_VIRTUAL

Transport type is Virtual.

Constant Value: 6 (0x00000006)

Public methods

close

public void close ()

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 Manifest.permission.ACCESS_HID

equals

public boolean equals (Object o)

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 return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

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
o Object: This value may be null.

Returns
boolean true if this object is the same as the obj argument; false otherwise.

getFeatureReport

public void getFeatureReport (int reportId, 
                Executor executor, 
                OutcomeReceiver<ReportException> callback)

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 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: the callback object to be used to notify of the report or an error.
This value cannot be null.

Throws
IllegalStateException if the device is not open.

getName

public String getName ()

The human-readable name of the HID device.

Returns
String This value cannot be null.

getPhysicalAddress

public String getPhysicalAddress ()

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.

Returns
String This value cannot be null.

getProductId

public int getProductId ()

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.

Returns
int

getReportDescriptor

public byte[] getReportDescriptor ()

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.

Returns
byte[] This value cannot be null.

getTransport

public int getTransport ()

The transport type used by this device.

Returns
int Value is one of the following:

getUniqueId

public String getUniqueId ()

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.

Returns
String This value cannot be null.

getVendorId

public int getVendorId ()

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.

Returns
int

hashCode

public int hashCode ()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Returns
int a hash code value for this object.

isOpen

public boolean isOpen ()

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(Executor, OutcomeReceiver) operation has successfully completed.

Returns
boolean true if the device is fully opened; false otherwise.

open

public void open (Executor executor, 
                OutcomeReceiver<HidDeviceException> callback)

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.onResult is invoked immediately.
  • If the device is currently opening, the request is queued and callback will 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 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: The object to be notified of success or failure.
This value cannot be null.

sendFeatureReport

public void sendFeatureReport (Report report, 
                Executor executor, 
                OutcomeReceiver<VoidException> callback)

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 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: the callback object to be used to notify of an error.
This value cannot be null.

Throws
IllegalStateException if the device is not open.

sendOutputReport

public void sendOutputReport (Report report, 
                Executor executor, 
                OutcomeReceiver<VoidException> callback)

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 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: the callback object to be used to notify of an error.
This value cannot be null.

Throws
IllegalStateException if the device is not open.

toString

public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.