public final class BluetoothHeadset extends Object implements BluetoothProfile
BluetoothHeadset is a proxy object for controlling the Bluetooth Headset Service via IPC.
Use BluetoothAdapter.getProfileProxy(android.content.Context, android.bluetooth.BluetoothProfile.ServiceListener, int)
to get
the BluetoothHeadset proxy object. Use
BluetoothAdapter.closeProfileProxy(int, android.bluetooth.BluetoothProfile)
to close the service connection.
Android only supports one connected Bluetooth Headset at a time. Each method is protected with its appropriate permission.
BluetoothProfile.ServiceListener
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_AUDIO_STATE_CHANGED
Intent used to broadcast the change in the Audio Connection state of the
A2DP profile.
|
static String |
ACTION_CONNECTION_STATE_CHANGED
Intent used to broadcast the change in connection state of the Headset
profile.
|
static String |
ACTION_HF_INDICATORS_VALUE_CHANGED
Intent used to broadcast the headset's indicator status
This intent will have 3 extras:
#EXTRA_IND_ID - The Assigned number of headset Indicator which is supported by
the headset ( as indicated by AT+BIND
command in the SLC sequence).or whose value
is changed (indicated by AT+BIEV command)
#EXTRA_IND_VALUE - The updated value of headset indicator. |
static String |
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
Intent used to broadcast that the headset has posted a
vendor-specific event.
|
static int |
AT_CMD_TYPE_ACTION
AT command type ACTION used with
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+CHUP. |
static int |
AT_CMD_TYPE_BASIC
AT command type BASIC used with
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, ATD. |
static int |
AT_CMD_TYPE_READ
AT command type READ used with
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+VGM?. |
static int |
AT_CMD_TYPE_SET
AT command type SET used with
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+VGM= |
static int |
AT_CMD_TYPE_TEST
AT command type TEST used with
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+VGM=?. |
static String |
EXTRA_HF_INDICATORS_IND_ID
A String extra field in
ACTION_HF_INDICATORS_VALUE_CHANGED
intents that contains the UUID of the headset indicator (as defined by Bluetooth SIG)
that is being sent. |
static String |
EXTRA_HF_INDICATORS_IND_VALUE
A int extra field in
ACTION_HF_INDICATORS_VALUE_CHANGED
intents that contains the value of the Headset indicator that is being sent. |
static String |
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS
A Parcelable String array extra field in
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT intents that contains
the arguments to the vendor-specific command. |
static String |
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD
A String extra field in
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
intents that contains the name of the vendor-specific command. |
static String |
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
An int extra field in
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
intents that contains the AT command type of the vendor-specific command. |
static int |
STATE_AUDIO_CONNECTED |
static int |
STATE_AUDIO_CONNECTING
Headset state when SCO audio is connecting.
|
static int |
STATE_AUDIO_DISCONNECTED
Headset state when SCO audio is not connected.
|
static String |
VENDOR_RESULT_CODE_COMMAND_ANDROID
A vendor-specific command for unsolicited result code.
|
static String |
VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY
The intent category to be used with
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
for the companyId |
A2DP, A2DP_SINK, AVRCP_CONTROLLER, EXTRA_PREVIOUS_STATE, EXTRA_STATE, GATT, GATT_SERVER, HEADSET, HEADSET_CLIENT, HEALTH, INPUT_DEVICE, MAP, PAN, PBAP, PBAP_CLIENT, PRIORITY_AUTO_CONNECT, PRIORITY_OFF, PRIORITY_ON, PRIORITY_UNDEFINED, SAP, STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING
Modifier and Type | Method and Description |
---|---|
boolean |
acceptIncomingConnect(BluetoothDevice device)
Accept the incoming connection.
|
void |
bindResponse(int ind_id,
boolean ind_status)
Send Headset the BIND response from AG to report change in the status of the
HF indicators to the headset
|
void |
clccResponse(int index,
int direction,
int status,
int mode,
boolean mpty,
String number,
int type)
Send Headset of CLCC response
|
boolean |
connect(BluetoothDevice device)
Initiate connection to a profile of the remote bluetooth device.
|
boolean |
connectAudio()
Initiates a connection of headset audio.
|
boolean |
disableWBS()
disable WBS codec settting.
|
boolean |
disconnect(BluetoothDevice device)
Initiate disconnection from a profile
This API will return false in scenarios like the profile on the
Bluetooth device is not in connected state etc.
|
boolean |
disconnectAudio()
Initiates a disconnection of headset audio.
|
boolean |
enableWBS()
enable WBS codec setting.
|
boolean |
getAudioRouteAllowed()
Returns whether audio routing is allowed. see
setAudioRouteAllowed(boolean) . |
int |
getAudioState(BluetoothDevice device)
Get the current audio state of the Headset.
|
int |
getBatteryUsageHint(BluetoothDevice device)
Get battery usage hint for Bluetooth Headset service.
|
List<BluetoothDevice> |
getConnectedDevices()
Get connected devices for this specific profile.
|
int |
getConnectionState(BluetoothDevice device)
Get the current connection state of the profile
Requires
android.Manifest.permission#BLUETOOTH permission. |
List<BluetoothDevice> |
getDevicesMatchingConnectionStates(int[] states)
Get a list of devices that match any of the given connection
states.
|
int |
getPriority(BluetoothDevice device)
Get the priority of the profile.
|
boolean |
isAudioConnected(BluetoothDevice device)
Check if Bluetooth SCO audio is connected.
|
boolean |
isAudioOn()
Check if Bluetooth SCO audio is connected.
|
static boolean |
isBluetoothVoiceDialingEnabled(Context context)
Indicates if current platform supports voice dialing over bluetooth SCO.
|
void |
phoneStateChanged(int numActive,
int numHeld,
int callState,
String number,
int type)
Notify Headset of phone state change.
|
boolean |
rejectIncomingConnect(BluetoothDevice device)
Reject the incoming connection.
|
boolean |
sendVendorSpecificResultCode(BluetoothDevice device,
String command,
String arg)
Sends a vendor-specific unsolicited result code to the headset.
|
void |
setAudioRouteAllowed(boolean allowed)
Sets whether audio routing is allowed.
|
boolean |
setPriority(BluetoothDevice device,
int priority)
Set priority of the profile
The device should already be paired.
|
boolean |
startScoUsingVirtualVoiceCall(BluetoothDevice device)
Initiates a SCO channel connection with the headset (if connected).
|
boolean |
startVoiceRecognition(BluetoothDevice device)
Start Bluetooth voice recognition.
|
boolean |
stopScoUsingVirtualVoiceCall(BluetoothDevice device)
Terminates an ongoing SCO connection and the associated virtual
call.
|
boolean |
stopVoiceRecognition(BluetoothDevice device)
Stop Bluetooth Voice Recognition mode, and shut down the
Bluetooth audio path.
|
public static final String ACTION_CONNECTION_STATE_CHANGED
This intent will have 3 extras:
BluetoothProfile.EXTRA_STATE
- The current state of the profile. BluetoothProfile.EXTRA_PREVIOUS_STATE
- The previous state of the profile. BluetoothDevice.EXTRA_DEVICE
- The remote device. 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 android.Manifest.permission#BLUETOOTH
permission to
receive.
public static final String ACTION_AUDIO_STATE_CHANGED
This intent will have 3 extras:
BluetoothProfile.EXTRA_STATE
- The current state of the profile. BluetoothProfile.EXTRA_PREVIOUS_STATE
- The previous state of the profile. BluetoothDevice.EXTRA_DEVICE
- The remote device. BluetoothProfile.EXTRA_STATE
or BluetoothProfile.EXTRA_PREVIOUS_STATE
can be any of
STATE_AUDIO_CONNECTED
, STATE_AUDIO_DISCONNECTED
,
Requires android.Manifest.permission#BLUETOOTH
permission
to receive.
public static final String ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
This intent will have 4 extras and 1 category.
BluetoothDevice.EXTRA_DEVICE
- The remote Bluetooth Device
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD
- The vendor
specific command EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
- The AT
command type which can be one of AT_CMD_TYPE_READ
,
AT_CMD_TYPE_TEST
, or AT_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
Requires android.Manifest.permission#BLUETOOTH
permission
to receive.
public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
intents that contains the name of the vendor-specific command.public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
intents that contains the AT command type of the vendor-specific command.public static final int AT_CMD_TYPE_READ
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+VGM?. There are no arguments for this command type.public static final int AT_CMD_TYPE_TEST
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+VGM=?. There are no arguments for this command type.public static final int AT_CMD_TYPE_SET
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+VGM=public static final int AT_CMD_TYPE_BASIC
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, ATD. Single character commands and everything following the
character are arguments.public static final int AT_CMD_TYPE_ACTION
EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE
For example, AT+CHUP. There are no arguments for action commands.public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
intents that contains
the arguments to the vendor-specific command.public static final String VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY
ACTION_VENDOR_SPECIFIC_HEADSET_EVENT
for the companyIdpublic static final String VENDOR_RESULT_CODE_COMMAND_ANDROID
public static final int STATE_AUDIO_DISCONNECTED
BluetoothProfile.EXTRA_STATE
or BluetoothProfile.EXTRA_PREVIOUS_STATE
of
ACTION_AUDIO_STATE_CHANGED
intent.public static final int STATE_AUDIO_CONNECTING
BluetoothProfile.EXTRA_STATE
or BluetoothProfile.EXTRA_PREVIOUS_STATE
of
ACTION_AUDIO_STATE_CHANGED
intent.public static final String ACTION_HF_INDICATORS_VALUE_CHANGED
This intent will have 3 extras:
#EXTRA_IND_ID
- The Assigned number of headset Indicator which is supported by
the headset ( as indicated by AT+BIND
command in the SLC sequence).or whose value
is changed (indicated by AT+BIEV command)#EXTRA_IND_VALUE
- The updated value of headset indicator. BluetoothDevice.EXTRA_DEVICE
- The remote device. #EXTRA_IND_ID
is defined by Bluetooth SIG and each of the indicators are
given an assigned number. Below shows the assigned number of Indicator added so far
- Enhanced Safety - 1
Requires android.Manifest.permission#BLUETOOTH
permission to
receive.
public static final String EXTRA_HF_INDICATORS_IND_ID
ACTION_HF_INDICATORS_VALUE_CHANGED
intents that contains the UUID of the headset indicator (as defined by Bluetooth SIG)
that is being sent.public static final String EXTRA_HF_INDICATORS_IND_VALUE
ACTION_HF_INDICATORS_VALUE_CHANGED
intents that contains the value of the Headset indicator that is being sent.public static final int STATE_AUDIO_CONNECTED
public boolean connect(BluetoothDevice device)
Currently, the system supports only 1 connection to the headset/handsfree profile. The API will automatically disconnect connected devices before connecting.
This API returns false in scenarios like the profile on the device is already connected or Bluetooth is not turned on. When this API returns true, it is guaranteed that connection state intent for the profile will be broadcasted with the state. Users can get the connection state of the profile from this intent.
Requires android.Manifest.permission#BLUETOOTH_ADMIN
permission.
device
- Remote Bluetooth Devicepublic boolean disconnect(BluetoothDevice device)
This API will return false in scenarios like the profile on the Bluetooth device is not in connected state etc. When this API returns, true, it is guaranteed that the connection state change intent will be broadcasted with the state. Users can get the disconnection state of the profile from this intent.
If the disconnection is initiated by a remote device, the state
will transition from BluetoothProfile.STATE_CONNECTED
to
BluetoothProfile.STATE_DISCONNECTED
. If the disconnect is initiated by the
host (local) device the state will transition from
BluetoothProfile.STATE_CONNECTED
to state BluetoothProfile.STATE_DISCONNECTING
to
state BluetoothProfile.STATE_DISCONNECTED
. The transition to
BluetoothProfile.STATE_DISCONNECTING
can be used to distinguish between the
two scenarios.
Requires android.Manifest.permission#BLUETOOTH_ADMIN
permission.
device
- Remote Bluetooth Devicepublic List<BluetoothDevice> getConnectedDevices()
Return the set of devices which are in state BluetoothProfile.STATE_CONNECTED
Requires android.Manifest.permission#BLUETOOTH
permission.
getConnectedDevices
in interface BluetoothProfile
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states)
If none of the devices match any of the given states, an empty list will be returned.
Requires android.Manifest.permission#BLUETOOTH
permission.
getDevicesMatchingConnectionStates
in interface BluetoothProfile
states
- Array of states. States can be one of
BluetoothProfile.STATE_CONNECTED
, BluetoothProfile.STATE_CONNECTING
,
BluetoothProfile.STATE_DISCONNECTED
, BluetoothProfile.STATE_DISCONNECTING
,public int getConnectionState(BluetoothDevice device)
Requires android.Manifest.permission#BLUETOOTH
permission.
getConnectionState
in interface BluetoothProfile
device
- Remote bluetooth device.BluetoothProfile.STATE_CONNECTED
, BluetoothProfile.STATE_CONNECTING
,
BluetoothProfile.STATE_DISCONNECTED
, BluetoothProfile.STATE_DISCONNECTING
public boolean setPriority(BluetoothDevice device, int priority)
The device should already be paired.
Priority can be one of BluetoothProfile.PRIORITY_ON
or
BluetoothProfile.PRIORITY_OFF
,
Requires android.Manifest.permission#BLUETOOTH_ADMIN
permission.
device
- Paired bluetooth devicepriority
- public int getPriority(BluetoothDevice device)
The priority can be any of:
BluetoothProfile.PRIORITY_AUTO_CONNECT
, BluetoothProfile.PRIORITY_OFF
,
BluetoothProfile.PRIORITY_ON
, BluetoothProfile.PRIORITY_UNDEFINED
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- Bluetooth devicepublic boolean startVoiceRecognition(BluetoothDevice device)
Users can listen to ACTION_AUDIO_STATE_CHANGED
.
If this function returns true, this intent will be broadcasted with
BluetoothProfile.EXTRA_STATE
set to STATE_AUDIO_CONNECTING
.
BluetoothProfile.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.
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- Bluetooth headsetpublic boolean stopVoiceRecognition(BluetoothDevice device)
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- Bluetooth headsetpublic boolean isAudioConnected(BluetoothDevice device)
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- Bluetooth headsetpublic int getBatteryUsageHint(BluetoothDevice device)
device
- the bluetooth headset.public static boolean isBluetoothVoiceDialingEnabled(Context context)
public boolean acceptIncomingConnect(BluetoothDevice device)
public boolean rejectIncomingConnect(BluetoothDevice device)
public int getAudioState(BluetoothDevice device)
public void setAudioRouteAllowed(boolean allowed)
false
, the AG will not route any
audio to the HF unless explicitly told to.
This method should be used in cases where the SCO channel is shared between multiple profiles
and must be delegated by a source knowledgeable
Note: This is an internal function and shouldn't be exposedallowed
- true
if the profile can reroute audio, false
otherwise.public boolean getAudioRouteAllowed()
setAudioRouteAllowed(boolean)
.
Note: This is an internal function and shouldn't be exposedpublic boolean isAudioOn()
Requires android.Manifest.permission#BLUETOOTH
permission.
public boolean connectAudio()
public boolean disconnectAudio()
public boolean startScoUsingVirtualVoiceCall(BluetoothDevice device)
device
- Remote Bluetooth Devicepublic boolean stopScoUsingVirtualVoiceCall(BluetoothDevice device)
device
- Remote Bluetooth Devicepublic void phoneStateChanged(int numActive, int numHeld, int callState, String number, int type)
public void clccResponse(int index, int direction, int status, int mode, boolean mpty, String number, int type)
public boolean sendVendorSpecificResultCode(BluetoothDevice device, String command, String arg)
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
.
Requires android.Manifest.permission#BLUETOOTH
permission.
device
- Bluetooth headset.command
- A vendor-specific command.arg
- The argument that will be attached to the command.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.IllegalArgumentException
- if command
is null
.public boolean enableWBS()
public boolean disableWBS()
public void bindResponse(int ind_id, boolean ind_status)
ind_id
- Assigned Number of the indicator (defined by SIG)ind_status
- possible values- false-Indicator is disabled, no value changes shall be sent for this indicator
true-Indicator is enabled, value changes may be sent for this indicator