HidManager


class HidManager
kotlin.Any
   ↳ android.hardware.hid.HidManager

Provides access to Human Interface Device (HID) nodes.

This manager allows applications to enumerate connected HID devices and query their information (such as vendor/product IDs and report descriptors).

HID devices include peripherals such as keyboards, mice, game controllers, and other interactive devices that follow the HID specification.

Access to HID devices requires the android.Manifest.permission#ACCESS_HID permission. Apps are typically granted access to specific devices via a system-provided chooser or permission dialog.

Device access may be restricted when the application is in the background or in a cached state to ensure system security and resource efficiency.

Summary

Public methods
Boolean

Checks if the calling app has the necessary permissions to enumerate connected HID devices.

MutableList<HidDevice!>

Returns a list of all currently connected HID devices.

Unit

Registers a listener to receive notifications about HID device lifecycle events.

Unit

Unregisters a previously registered listener for HID device lifecycle events.

Public methods

canEnumerateDevices

fun canEnumerateDevices(): Boolean

Checks if the calling app has the necessary permissions to enumerate connected HID devices.

Return
Boolean true if the calling app can enumerate HIDs, false otherwise.

getDevices

fun getDevices(): MutableList<HidDevice!>

Returns a list of all currently connected HID devices.

The returned list contains HidDevice objects representing each connected HID node that the system has detected and made available.

It is recommended to call canEnumerateDevices() before calling this method.
This method may take several seconds to complete, so it should only be called from a worker thread.
Requires android.Manifest.permission#ACCESS_HID

Return
MutableList<HidDevice!> a list of connected HID devices, or an empty list if no devices are connected.
This value cannot be null.
Exceptions
java.lang.IllegalStateException if called on the main thread.
java.lang.SecurityException if the caller does not have the android.Manifest.permission#ACCESS_HID permission or if canEnumerateDevices() returns false.

registerListener

fun registerListener(
    listener: HidDeviceListener,
    executor: Executor
): Unit

Registers a listener to receive notifications about HID device lifecycle events.

When a HID device is physically connected to or disconnected from the system, the provided HidDeviceListener will be notified. If the listener is already registered, this call does nothing.

All callback methods on the listener will be invoked using the provided Executor.

Upon registration, HidDeviceListener.onHidDeviceAdded(HidDevice) will be immediately invoked for all HID devices currently connected to the system.
Requires android.Manifest.permission#ACCESS_HID

Parameters
listener HidDeviceListener: The listener to receive device addition and removal events. Must not be null.
executor Executor: The executor on which the listener methods will be invoked. Must not 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.

unregisterListener

fun unregisterListener(listener: HidDeviceListener): Unit

Unregisters a previously registered listener for HID device lifecycle events.

Once unregistered, the listener will no longer receive notifications when HID devices are added to or removed from the system. If the provided listener was not previously registered via registerListener, this call does nothing.

After this method returns, no further callbacks will be dispatched to the executor associated with this listener.
Requires android.Manifest.permission#ACCESS_HID

Parameters
listener HidDeviceListener: The listener to unregister. Must not be null.