BluetoothHeadset
class BluetoothHeadset : BluetoothProfile
| kotlin.Any | |
| ↳ | android.bluetooth.BluetoothHeadset |
Public API for controlling the Bluetooth Headset Service. This includes both Bluetooth Headset and Handsfree (v1.5) profiles.
BluetoothHeadset is a proxy object for controlling the Bluetooth Headset Service via IPC.
Use BluetoothAdapter.getProfileProxy to get the BluetoothHeadset proxy object. Use BluetoothAdapter.closeProfileProxy to close the service connection.
Android only supports one connected Bluetooth Headset at a time. Each method is protected with its appropriate permission.
Summary
| Constants | |
|---|---|
| static String |
Intent used to broadcast the change in the Audio Connection state of the HFP profile. |
| static String |
Intent used to broadcast the change in connection state of the Headset profile. |
| static String |
Intent used to broadcast that the headset has posted a vendor-specific event. |
| static Int |
AT command type ACTION used with |
| static Int |
AT command type BASIC used with |
| static Int |
AT command type READ used with |
| static Int |
AT command type SET used with |
| static Int |
AT command type TEST used with |
| static String |
Used as an int extra field in |
| static String |
A Parcelable String array extra field in |
| static String |
A String extra field in |
| static String |
An int extra field in |
| static Int |
Headset state when SCO audio is connected. |
| static Int |
Headset state when SCO audio is connecting. |
| static Int |
Headset state when SCO audio is not connected. |
| static String |
A vendor-specific command for unsolicited result code. |
| static String |
The intent category to be used with |
| Inherited constants | |
|---|---|
| Public methods | |
|---|---|
| MutableList<BluetoothDevice!>! |
Get connected devices for this specific profile. |
| Int |
getConnectionState(device: BluetoothDevice!)Get the current connection state of the profile |
| MutableList<BluetoothDevice!>! |
getDevicesMatchingConnectionStates(states: IntArray!)Get a list of devices that match any of the given connection states. |
| Boolean |
isAudioConnected(device: BluetoothDevice!)Check if Bluetooth SCO audio is connected. |
| Boolean |
isNoiseReductionSupported(device: BluetoothDevice)Checks whether the headset supports some form of noise reduction. |
| Boolean |
Checks whether the headset supports voice recognition. |
| Boolean |
sendVendorSpecificResultCode(device: BluetoothDevice!, command: String!, arg: String!)Sends a vendor-specific unsolicited result code to the headset. |
| Boolean |
startVoiceRecognition(device: BluetoothDevice!)Start Bluetooth voice recognition. |
| Boolean |
stopVoiceRecognition(device: BluetoothDevice!)Stop Bluetooth Voice Recognition mode, and shut down the Bluetooth audio path. |
| Protected methods | |
|---|---|
| Unit |
finalize()Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
Constants
ACTION_AUDIO_STATE_CHANGED
static val ACTION_AUDIO_STATE_CHANGED: String
Intent used to broadcast the change in the Audio Connection state of the HFP profile.
This intent will have 3 extras:
EXTRA_STATE- The current state of the profile.EXTRA_PREVIOUS_STATE- The previous state of the profile.BluetoothDevice.EXTRA_DEVICE- The remote device.
EXTRA_STATE or EXTRA_PREVIOUS_STATE can be any of STATE_AUDIO_CONNECTED, STATE_AUDIO_DISCONNECTED,.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
Value: "android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED"ACTION_CONNECTION_STATE_CHANGED
static val ACTION_CONNECTION_STATE_CHANGED: String
Intent used to broadcast the change in connection state of the Headset profile.
This intent will have 3 extras:
EXTRA_STATE- The current state of the profile.EXTRA_PREVIOUS_STATE- The previous state of the profile.BluetoothDevice.EXTRA_DEVICE- The remote device.EXTRA_DISCONNECTED_REASON- A disconnection reason if moved fromSTATE_CONNECTINGtoSTATE_DISCONNECTED.
EXTRA_STATE or EXTRA_PREVIOUS_STATE can be any of STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED, STATE_DISCONNECTING.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
Value: "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
static val ACTION_VENDOR_SPECIFIC_HEADSET_EVENT: String
Intent used to broadcast that the headset has posted a vendor-specific event.
This intent will have 4 extras and 1 category.
BluetoothDevice.EXTRA_DEVICE- The remote Bluetooth DeviceEXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD- The vendor specific commandEXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE- The AT command type which can be one ofAT_CMD_TYPE_READ,AT_CMD_TYPE_TEST, orAT_CMD_TYPE_SET,AT_CMD_TYPE_BASIC,AT_CMD_TYPE_ACTION.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS- Command arguments.
The category is the Company ID of the vendor defining the vendor-specific command. BluetoothAssignedNumbers
For example, for Plantronics specific events Category will be VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY.55
For example, an AT+XEVENT=foo,3 will get translated into
- EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = +XEVENT
- EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = AT_CMD_TYPE_SET
- EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = foo, 3
For apps targeting
Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag. For apps targeting
Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int). Requires
android.Manifest.permission#BLUETOOTH_CONNECT
Value: "android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT"AT_CMD_TYPE_ACTION
static val AT_CMD_TYPE_ACTION: Int
AT command type ACTION used with EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE For example, AT+CHUP. There are no arguments for action commands.
Value: 4AT_CMD_TYPE_BASIC
static val AT_CMD_TYPE_BASIC: Int
AT command type BASIC used with EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE For example, ATD. Single character commands and everything following the character are arguments.
Value: 3AT_CMD_TYPE_READ
static val AT_CMD_TYPE_READ: Int
AT command type READ used with EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE For example, AT+VGM?. There are no arguments for this command type.
Value: 0AT_CMD_TYPE_SET
static val AT_CMD_TYPE_SET: Int
AT command type SET used with EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE For example, AT+VGM=.
Value: 2AT_CMD_TYPE_TEST
static val AT_CMD_TYPE_TEST: Int
AT command type TEST used with EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE For example, AT+VGM=?. There are no arguments for this command type.
Value: 1EXTRA_DISCONNECTED_REASON
static val EXTRA_DISCONNECTED_REASON: String
Used as an int extra field in ACTION_CONNECTION_STATE_CHANGED intents for connection failure reasons.
The value is one of the following:
BluetoothStatusCodes.SUCCESSBluetoothStatusCodes.ERROR_UNKNOWNBluetoothStatusCodes.ERROR_INSUFFICIENT_RESOURCESBluetoothStatusCodes.ERROR_SDP_DISCOVERY_FAILEDBluetoothStatusCodes.ERROR_RFCOMM_CONNECTION_FAILED
Value: "android.bluetooth.headset.extra.DISCONNECTED_REASON"EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS
static val EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS: String
A Parcelable String array extra field in ACTION_VENDOR_SPECIFIC_HEADSET_EVENT intents that contains the arguments to the vendor-specific command.
Value: "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_ARGS"EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD
static val EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD: String
A String extra field in ACTION_VENDOR_SPECIFIC_HEADSET_EVENT intents that contains the name of the vendor-specific command.
Value: "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD"EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
static val EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE: String
An int extra field in ACTION_VENDOR_SPECIFIC_HEADSET_EVENT intents that contains the AT command type of the vendor-specific command.
Value: "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE"STATE_AUDIO_CONNECTED
static val STATE_AUDIO_CONNECTED: Int
Headset state when SCO audio is connected. This state can be one of EXTRA_STATE or EXTRA_PREVIOUS_STATE of ACTION_AUDIO_STATE_CHANGED intent.
Value: 12STATE_AUDIO_CONNECTING
static val STATE_AUDIO_CONNECTING: Int
Headset state when SCO audio is connecting. This state can be one of EXTRA_STATE or EXTRA_PREVIOUS_STATE of ACTION_AUDIO_STATE_CHANGED intent.
Value: 11STATE_AUDIO_DISCONNECTED
static val STATE_AUDIO_DISCONNECTED: Int
Headset state when SCO audio is not connected. This state can be one of EXTRA_STATE or EXTRA_PREVIOUS_STATE of ACTION_AUDIO_STATE_CHANGED intent.
Value: 10VENDOR_RESULT_CODE_COMMAND_ANDROID
static val VENDOR_RESULT_CODE_COMMAND_ANDROID: String
A vendor-specific command for unsolicited result code.
Value: "+ANDROID"VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY
static val VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY: String
The intent category to be used with ACTION_VENDOR_SPECIFIC_HEADSET_EVENT for the companyId
Value: "android.bluetooth.headset.intent.category.companyid"Public methods
getConnectedDevices
fun getConnectedDevices(): MutableList<BluetoothDevice!>!
Get connected devices for this specific profile.
Return the set of devices which are in state STATE_CONNECTED
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Return | |
|---|---|
MutableList<BluetoothDevice!>! |
List of devices. The list will be empty on error. |
getConnectionState
fun getConnectionState(device: BluetoothDevice!): Int
Get the current connection state of the profile
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
device |
BluetoothDevice!: Remote bluetooth device. |
| Return | |
|---|---|
Int |
State of the profile connection. One of STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING Value is one of the following: |
getDevicesMatchingConnectionStates
fun getDevicesMatchingConnectionStates(states: IntArray!): MutableList<BluetoothDevice!>!
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.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
states |
IntArray!: Array of states. States can be one of STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING, |
| Return | |
|---|---|
MutableList<BluetoothDevice!>! |
List of devices. The list will be empty on error. |
isAudioConnected
fun isAudioConnected(device: BluetoothDevice!): Boolean
Check if Bluetooth SCO audio is connected.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
device |
BluetoothDevice!: Bluetooth headset |
| Return | |
|---|---|
Boolean |
true if SCO is connected, false otherwise or on error |
isNoiseReductionSupported
fun isNoiseReductionSupported(device: BluetoothDevice): Boolean
Checks whether the headset supports some form of noise reduction.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
device |
BluetoothDevice: Bluetooth device. This value cannot be null. |
| Return | |
|---|---|
Boolean |
true if echo cancellation and/or noise reduction is supported, false otherwise |
isVoiceRecognitionSupported
fun isVoiceRecognitionSupported(device: BluetoothDevice): Boolean
Checks whether the headset supports voice recognition.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
device |
BluetoothDevice: Bluetooth device. This value cannot be null. |
| Return | |
|---|---|
Boolean |
true if voice recognition is supported, false otherwise |
sendVendorSpecificResultCode
fun sendVendorSpecificResultCode(
device: BluetoothDevice!,
command: String!,
arg: String!
): Boolean
Sends a vendor-specific unsolicited result code to the headset.
The actual string to be sent is command + ": " + arg. For example, if command is VENDOR_RESULT_CODE_COMMAND_ANDROID and arg is "0", the string "+ANDROID: 0" will be sent.
Currently only VENDOR_RESULT_CODE_COMMAND_ANDROID is allowed as command.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
device |
BluetoothDevice!: Bluetooth headset. |
command |
String!: A vendor-specific command. |
arg |
String!: The argument that will be attached to the command. |
| Return | |
|---|---|
Boolean |
false if there is no headset connected, or if the command is not an allowed vendor-specific unsolicited result code, or on error. true otherwise. |
| Exceptions | |
|---|---|
java.lang.IllegalArgumentException |
if command is null. |
startVoiceRecognition
fun startVoiceRecognition(device: BluetoothDevice!): Boolean
Start Bluetooth voice recognition. This methods sends the voice recognition AT command to the headset and establishes the audio connection.
Users can listen to ACTION_AUDIO_STATE_CHANGED. If this function returns true, this intent will be broadcasted with EXTRA_STATE set to STATE_AUDIO_CONNECTING.
EXTRA_STATE will transition from STATE_AUDIO_CONNECTING to STATE_AUDIO_CONNECTED when audio connection is established and to STATE_AUDIO_DISCONNECTED in case of failure to establish the audio connection.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
device |
BluetoothDevice!: Bluetooth headset |
| Return | |
|---|---|
Boolean |
false if there is no headset connected, or the connected headset doesn't support voice recognition, or voice recognition is already started, or audio channel is occupied, or on error, true otherwise |
stopVoiceRecognition
fun stopVoiceRecognition(device: BluetoothDevice!): Boolean
Stop Bluetooth Voice Recognition mode, and shut down the Bluetooth audio path.
Users can listen to ACTION_AUDIO_STATE_CHANGED. If this function returns true, this intent will be broadcasted with EXTRA_STATE set to STATE_AUDIO_DISCONNECTED.
For apps targeting Build.VERSION_CODES.R or lower, this requires the Manifest.permission.BLUETOOTH permission which can be gained with a simple <uses-permission> manifest tag.
For apps targeting Build.VERSION_CODES.S or or higher, this requires the Manifest.permission.BLUETOOTH_CONNECT permission which can be gained with android.app.Activity#requestPermissions(String[], int).
Requires android.Manifest.permission#BLUETOOTH_CONNECT
| Parameters | |
|---|---|
device |
BluetoothDevice!: Bluetooth headset |
| Return | |
|---|---|
Boolean |
false if there is no headset connected, or voice recognition has not started, or voice recognition has ended on this headset, or on error, true otherwise |
Protected methods
finalize
protected fun finalize(): Unit
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.
The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.
The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.
After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.
The finalize method is never invoked more than once by a Java virtual machine for any given object.
Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.
| Exceptions | |
|---|---|
java.lang.Throwable |
the Exception raised by this method |