Join us on the livestream at Android Dev Summit on 7-8 November 2018, starting at 10AM PDT!

BluetoothHidDevice

public final class BluetoothHidDevice
extends Object implements BluetoothProfile

java.lang.Object
   ↳ android.bluetooth.BluetoothHidDevice


Provides the public APIs to control the Bluetooth HID Device profile.

BluetoothHidDevice is a proxy object for controlling the Bluetooth HID Device Service via IPC. Use BluetoothAdapter.getProfileProxy(Context, BluetoothProfile.ServiceListener, int) to get the BluetoothHidDevice proxy object.

Summary

Nested classes

class BluetoothHidDevice.Callback

The template class that applications use to call callback functions on events from the HID host. 

Constants

String ACTION_CONNECTION_STATE_CHANGED

Intent used to broadcast the change in connection state of the Input Host profile.

byte ERROR_RSP_INVALID_PARAM

Constant representing error response for Set Report due to "invalid parameter".

byte ERROR_RSP_INVALID_RPT_ID

Constant representing error response for Set Report due to "invalid report ID".

byte ERROR_RSP_NOT_READY

Constant representing error response for Set Report due to "not ready".

byte ERROR_RSP_SUCCESS

Constant representing success response for Set Report.

byte ERROR_RSP_UNKNOWN

Constant representing error response for Set Report with unknown reason.

byte ERROR_RSP_UNSUPPORTED_REQ

Constant representing error response for Set Report due to "unsupported request".

byte PROTOCOL_BOOT_MODE

Constant representing boot protocol mode used set by host.

byte PROTOCOL_REPORT_MODE

Constant representing report protocol mode used set by host.

byte REPORT_TYPE_FEATURE

Constant representing HID Feature Report type.

byte REPORT_TYPE_INPUT

Constant representing HID Input Report type.

byte REPORT_TYPE_OUTPUT

Constant representing HID Output Report type.

byte SUBCLASS1_COMBO

Constant representing combo keyboard and mouse subclass.

byte SUBCLASS1_KEYBOARD

Constant representing keyboard subclass.

byte SUBCLASS1_MOUSE

Constant representing mouse subclass.

byte SUBCLASS1_NONE

Constant representing unspecified HID device subclass.

byte SUBCLASS2_CARD_READER

Constant representing card reader subclass.

byte SUBCLASS2_DIGITIZER_TABLET

Constant representing digitizer tablet subclass.

byte SUBCLASS2_GAMEPAD

Constant representing gamepad subclass.

byte SUBCLASS2_JOYSTICK

Constant representing joystick subclass.

byte SUBCLASS2_REMOTE_CONTROL

Constant representing remote control subclass.

byte SUBCLASS2_SENSING_DEVICE

Constant representing sensing device subclass.

byte SUBCLASS2_UNCATEGORIZED

Constant representing uncategorized HID device subclass.

Inherited constants

Public methods

boolean connect(BluetoothDevice device)

Initiates connection to host which is currently paired with this device.

boolean disconnect(BluetoothDevice device)

Disconnects from currently connected host.

List<BluetoothDevice> getConnectedDevices()

Get connected devices for this specific profile.

int getConnectionState(BluetoothDevice device)

Get the current connection state of the profile

List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states)

Get a list of devices that match any of the given connection states.

boolean registerApp(BluetoothHidDeviceAppSdpSettings sdp, BluetoothHidDeviceAppQosSettings inQos, BluetoothHidDeviceAppQosSettings outQos, Executor executor, BluetoothHidDevice.Callback callback)

Registers application to be used for HID device.

boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data)

Sends report to remote host as reply for GET_REPORT request from BluetoothHidDevice.Callback.onGetReport(BluetoothDevice, byte, byte, int).

boolean reportError(BluetoothDevice device, byte error)

Sends error handshake message as reply for invalid SET_REPORT request from BluetoothHidDevice.Callback.onSetReport(BluetoothDevice, byte, byte, byte[]).

boolean sendReport(BluetoothDevice device, int id, byte[] data)

Sends report to remote host using interrupt channel.

boolean unregisterApp()

Unregisters application.

Inherited methods

Constants

ACTION_CONNECTION_STATE_CHANGED

added in API level 28
public static final String ACTION_CONNECTION_STATE_CHANGED

Intent used to broadcast the change in connection state of the Input Host profile.

This intent will have 3 extras:

BluetoothProfile.EXTRA_STATE or BluetoothProfile.EXTRA_PREVIOUS_STATE can be any of BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTING.

Requires Manifest.permission.BLUETOOTH permission to receive.

Constant Value: "android.bluetooth.hiddevice.profile.action.CONNECTION_STATE_CHANGED"

ERROR_RSP_INVALID_PARAM

added in API level 28
public static final byte ERROR_RSP_INVALID_PARAM

Constant representing error response for Set Report due to "invalid parameter".

Constant Value: 4 (0x00000004)

ERROR_RSP_INVALID_RPT_ID

added in API level 28
public static final byte ERROR_RSP_INVALID_RPT_ID

Constant representing error response for Set Report due to "invalid report ID".

Constant Value: 2 (0x00000002)

ERROR_RSP_NOT_READY

added in API level 28
public static final byte ERROR_RSP_NOT_READY

Constant representing error response for Set Report due to "not ready".

Constant Value: 1 (0x00000001)

ERROR_RSP_SUCCESS

added in API level 28
public static final byte ERROR_RSP_SUCCESS

Constant representing success response for Set Report.

Constant Value: 0 (0x00000000)

ERROR_RSP_UNKNOWN

added in API level 28
public static final byte ERROR_RSP_UNKNOWN

Constant representing error response for Set Report with unknown reason.

Constant Value: 14 (0x0000000e)

ERROR_RSP_UNSUPPORTED_REQ

added in API level 28
public static final byte ERROR_RSP_UNSUPPORTED_REQ

Constant representing error response for Set Report due to "unsupported request".

Constant Value: 3 (0x00000003)

PROTOCOL_BOOT_MODE

added in API level 28
public static final byte PROTOCOL_BOOT_MODE

Constant representing boot protocol mode used set by host. Default is always PROTOCOL_REPORT_MODE unless notified otherwise.

Constant Value: 0 (0x00000000)

PROTOCOL_REPORT_MODE

added in API level 28
public static final byte PROTOCOL_REPORT_MODE

Constant representing report protocol mode used set by host. Default is always PROTOCOL_REPORT_MODE unless notified otherwise.

Constant Value: 1 (0x00000001)

REPORT_TYPE_FEATURE

added in API level 28
public static final byte REPORT_TYPE_FEATURE

Constant representing HID Feature Report type.

Constant Value: 3 (0x00000003)

REPORT_TYPE_INPUT

added in API level 28
public static final byte REPORT_TYPE_INPUT

Constant representing HID Input Report type.

Constant Value: 1 (0x00000001)

REPORT_TYPE_OUTPUT

added in API level 28
public static final byte REPORT_TYPE_OUTPUT

Constant representing HID Output Report type.

Constant Value: 2 (0x00000002)

SUBCLASS1_COMBO

added in API level 28
public static final byte SUBCLASS1_COMBO

Constant representing combo keyboard and mouse subclass.

Constant Value: -64 (0xffffffc0)

SUBCLASS1_KEYBOARD

added in API level 28
public static final byte SUBCLASS1_KEYBOARD

Constant representing keyboard subclass.

Constant Value: 64 (0x00000040)

SUBCLASS1_MOUSE

added in API level 28
public static final byte SUBCLASS1_MOUSE

Constant representing mouse subclass.

Constant Value: -128 (0xffffff80)

SUBCLASS1_NONE

added in API level 28
public static final byte SUBCLASS1_NONE

Constant representing unspecified HID device subclass.

Constant Value: 0 (0x00000000)

SUBCLASS2_CARD_READER

added in API level 28
public static final byte SUBCLASS2_CARD_READER

Constant representing card reader subclass.

Constant Value: 6 (0x00000006)

SUBCLASS2_DIGITIZER_TABLET

added in API level 28
public static final byte SUBCLASS2_DIGITIZER_TABLET

Constant representing digitizer tablet subclass.

Constant Value: 5 (0x00000005)

SUBCLASS2_GAMEPAD

added in API level 28
public static final byte SUBCLASS2_GAMEPAD

Constant representing gamepad subclass.

Constant Value: 2 (0x00000002)

SUBCLASS2_JOYSTICK

added in API level 28
public static final byte SUBCLASS2_JOYSTICK

Constant representing joystick subclass.

Constant Value: 1 (0x00000001)

SUBCLASS2_REMOTE_CONTROL

added in API level 28
public static final byte SUBCLASS2_REMOTE_CONTROL

Constant representing remote control subclass.

Constant Value: 3 (0x00000003)

SUBCLASS2_SENSING_DEVICE

added in API level 28
public static final byte SUBCLASS2_SENSING_DEVICE

Constant representing sensing device subclass.

Constant Value: 4 (0x00000004)

SUBCLASS2_UNCATEGORIZED

added in API level 28
public static final byte SUBCLASS2_UNCATEGORIZED

Constant representing uncategorized HID device subclass.

Constant Value: 0 (0x00000000)

Public methods

connect

added in API level 28
public boolean connect (BluetoothDevice device)

Initiates connection to host which is currently paired with this device. If the application is not registered, #connect(BluetoothDevice) will fail. The connection state should be tracked by the application by handling callback from Callback#onConnectionStateChanged. The connection state is not related to the return value of this method.

Parameters
device BluetoothDevice

Returns
boolean true if the command is successfully sent; otherwise false.

disconnect

added in API level 28
public boolean disconnect (BluetoothDevice device)

Disconnects from currently connected host. The connection state should be tracked by the application by handling callback from Callback#onConnectionStateChanged. The connection state is not related to the return value of this method.

Parameters
device BluetoothDevice

Returns
boolean true if the command is successfully sent; otherwise false.

getConnectedDevices

added in API level 28
public List<BluetoothDevice> getConnectedDevices ()

Get connected devices for this specific profile.

Return the set of devices which are in state STATE_CONNECTED

Returns
List<BluetoothDevice> List of devices. The list will be empty on error.

getConnectionState

added in API level 28
public int getConnectionState (BluetoothDevice device)

Get the current connection state of the profile

Parameters
device BluetoothDevice: Remote bluetooth device.

Returns
int State of the profile connection. One of STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING

getDevicesMatchingConnectionStates

added in API level 28
public List<BluetoothDevice> getDevicesMatchingConnectionStates (int[] states)

Get a list of devices that match any of the given connection states.

If none of the devices match any of the given states, an empty list will be returned.

Parameters
states int: Array of states. States can be one of BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_DISCONNECTING,

Returns
List<BluetoothDevice> List of devices. The list will be empty on error.

registerApp

added in API level 28
public boolean registerApp (BluetoothHidDeviceAppSdpSettings sdp, 
                BluetoothHidDeviceAppQosSettings inQos, 
                BluetoothHidDeviceAppQosSettings outQos, 
                Executor executor, 
                BluetoothHidDevice.Callback callback)

Registers application to be used for HID device. Connections to HID Device are only possible when application is registered. Only one application can be registered at one time. When an application is registered, the HID Host service will be disabled until it is unregistered. When no longer used, application should be unregistered using unregisterApp(). The app will be automatically unregistered if it is not foreground. The registration status should be tracked by the application by handling callback from Callback#onAppStatusChanged. The app registration status is not related to the return value of this method.

Parameters
sdp BluetoothHidDeviceAppSdpSettings: BluetoothHidDeviceAppSdpSettings object of HID Device SDP record. The HID Device SDP record is required.

inQos BluetoothHidDeviceAppQosSettings: BluetoothHidDeviceAppQosSettings object of Incoming QoS Settings. The Incoming QoS Settings is not required. Use null or default BluetoothHidDeviceAppQosSettings.Builder for default values.

outQos BluetoothHidDeviceAppQosSettings: BluetoothHidDeviceAppQosSettings object of Outgoing QoS Settings. The Outgoing QoS Settings is not required. Use null or default BluetoothHidDeviceAppQosSettings.Builder for default values.

executor Executor: Executor object on which callback will be executed. The Executor object is required.

callback BluetoothHidDevice.Callback: BluetoothHidDevice.Callback object to which callback messages will be sent. The Callback object is required.

Returns
boolean true if the command is successfully sent; otherwise false.

replyReport

added in API level 28
public boolean replyReport (BluetoothDevice device, 
                byte type, 
                byte id, 
                byte[] data)

Sends report to remote host as reply for GET_REPORT request from BluetoothHidDevice.Callback.onGetReport(BluetoothDevice, byte, byte, int).

Parameters
device BluetoothDevice

type byte: Report Type, as in request.

id byte: Report Id, as in request.

data byte: Report data, not including Report Id.

Returns
boolean true if the command is successfully sent; otherwise false.

reportError

added in API level 28
public boolean reportError (BluetoothDevice device, 
                byte error)

Sends error handshake message as reply for invalid SET_REPORT request from BluetoothHidDevice.Callback.onSetReport(BluetoothDevice, byte, byte, byte[]).

Parameters
device BluetoothDevice

error byte: Error to be sent for SET_REPORT via HANDSHAKE.

Returns
boolean true if the command is successfully sent; otherwise false.

sendReport

added in API level 28
public boolean sendReport (BluetoothDevice device, 
                int id, 
                byte[] data)

Sends report to remote host using interrupt channel.

Parameters
device BluetoothDevice

id int: Report Id, as defined in descriptor. Can be 0 in case Report Id are not defined in descriptor.

data byte: Report data, not including Report Id.

Returns
boolean true if the command is successfully sent; otherwise false.

unregisterApp

added in API level 28
public boolean unregisterApp ()

Unregisters application. Active connection will be disconnected and no new connections will be allowed until registered again using (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, Executor, Callback). The registration status should be tracked by the application by handling callback from Callback#onAppStatusChanged. The app registration status is not related to the return value of this method.

Returns
boolean true if the command is successfully sent; otherwise false.