/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.bluetooth; import static android.bluetooth.BluetoothUtils.getSyncTimeout; import android.annotation.IntRange; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SuppressLint; import android.annotation.SystemApi; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; import android.compat.annotation.UnsupportedAppUsage; import android.content.AttributionSource; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.os.Parcel; import android.os.Parcelable; import android.os.RemoteException; import android.util.CloseGuard; import android.util.Log; import com.android.modules.utils.SynchronousResultReceiver; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeoutException; /** * This class provides the System APIs to interact with the Hands-Free Client profile. * *
BluetoothHeadsetClient is a proxy object for controlling the Bluetooth HFP Client * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get * the BluetoothHeadsetClient proxy object. * * @hide */ @SystemApi public final class BluetoothHeadsetClient implements BluetoothProfile, AutoCloseable { private static final String TAG = "BluetoothHeadsetClient"; private static final boolean DBG = true; private static final boolean VDBG = false; private final CloseGuard mCloseGuard; /** * Intent used to broadcast the change in connection state of the HFP Client profile. * *
This intent will have 3 extras: *
{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. * * @hide */ @SuppressLint("ActionValue") @SystemApi @RequiresBluetoothConnectPermission @RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.BLUETOOTH_PRIVILEGED, }) @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String ACTION_CONNECTION_STATE_CHANGED = "android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED"; /** * Intent sent whenever audio state changes. * *
It includes two mandatory extras: * {@link BluetoothProfile#EXTRA_STATE}, * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE}, * with possible values: * {@link #STATE_AUDIO_CONNECTING}, * {@link #STATE_AUDIO_CONNECTED}, * {@link #STATE_AUDIO_DISCONNECTED}
*When EXTRA_STATE
is set
* to STATE_AUDIO_CONNECTED,
* it also includes {@link #EXTRA_AUDIO_WBS}
* indicating wide band speech support.
It can contain one or more of the following extras: * {@link #EXTRA_NETWORK_STATUS}, * {@link #EXTRA_NETWORK_SIGNAL_STRENGTH}, * {@link #EXTRA_NETWORK_ROAMING}, * {@link #EXTRA_BATTERY_LEVEL}, * {@link #EXTRA_OPERATOR_NAME}, * {@link #EXTRA_VOICE_RECOGNITION}, * {@link #EXTRA_IN_BAND_RING}
* * @hide */ @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String ACTION_AG_EVENT = "android.bluetooth.headsetclient.profile.action.AG_EVENT"; /** * Intent sent whenever state of a call changes. * *It includes: * {@link #EXTRA_CALL}, * with value of {@link BluetoothHeadsetClientCall} instance, * representing actual call state.
* * @hide */ @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String ACTION_CALL_CHANGED = "android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED"; /** * Intent that notifies about the result of the last issued action. * Please note that not every action results in explicit action result code being sent. * Instead other notifications about new Audio Gateway state might be sent, * likeACTION_AG_EVENT
with EXTRA_VOICE_RECOGNITION
value
* when for example user started voice recognition from HF unit.
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_RESULT =
"android.bluetooth.headsetclient.profile.action.RESULT";
/**
* Intent that notifies about vendor specific event arrival. Events not defined in
* HFP spec will be matched with supported vendor event list and this intent will
* be broadcasted upon a match. Supported vendor events are of format of
* of "+eventCode" or "+eventCode=xxxx" or "+eventCode:=xxxx".
* Vendor event can be a response to an vendor specific command or unsolicited.
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_VENDOR_SPECIFIC_HEADSETCLIENT_EVENT =
"android.bluetooth.headsetclient.profile.action.VENDOR_SPECIFIC_EVENT";
/**
* Intent that notifies about the number attached to the last voice tag
* recorded on AG.
*
* It contains:
* {@link #EXTRA_NUMBER},
* with a String
value representing phone number.
Possible values: true
,
* false
.
Value: 0 - network unavailable, * 1 - network available
* * @hide */ public static final String EXTRA_NETWORK_STATUS = "android.bluetooth.headsetclient.extra.NETWORK_STATUS"; /** * Extra for AG_EVENT intent indicates network signal strength. *Value: Integer
representing signal strength.
Value: 0 - no roaming * 1 - active roaming
* * @hide */ public static final String EXTRA_NETWORK_ROAMING = "android.bluetooth.headsetclient.extra.NETWORK_ROAMING"; /** * Extra for AG_EVENT intent indicates the battery level. *Value: Integer
representing signal strength.
Value: String
representing operator name.
Value: * 0 - voice recognition stopped, * 1 - voice recognition started.
* * @hide */ public static final String EXTRA_VOICE_RECOGNITION = "android.bluetooth.headsetclient.extra.VOICE_RECOGNITION"; /** * Extra for AG_EVENT intent indicates in band ring state. *Value: * 0 - in band ring tone not supported, or * 1 - in band ring tone supported.
* * @hide */ public static final String EXTRA_IN_BAND_RING = "android.bluetooth.headsetclient.extra.IN_BAND_RING"; /** * Extra for AG_EVENT intent indicates subscriber info. *Value: String
containing subscriber information.
Value: String
representing phone number
* corresponding to last voice tag recorded on AG
Possible results: * {@link #ACTION_RESULT_OK}, * {@link #ACTION_RESULT_ERROR}, * {@link #ACTION_RESULT_ERROR_NO_CARRIER}, * {@link #ACTION_RESULT_ERROR_BUSY}, * {@link #ACTION_RESULT_ERROR_NO_ANSWER}, * {@link #ACTION_RESULT_ERROR_DELAYED}, * {@link #ACTION_RESULT_ERROR_BLACKLISTED}, * {@link #ACTION_RESULT_ERROR_CME}
* * @hide */ public static final String EXTRA_RESULT_CODE = "android.bluetooth.headsetclient.extra.RESULT_CODE"; /** * Extra for ACTION_RESULT intent that shows the extended result code of * last issued action. *Value: Integer
- error code.
true
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
*
* @hide
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceivertrue
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
*
* @hide
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiverThe device should already be paired. * Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF} * * @param device Paired bluetooth device * @param priority * @return true if priority is set, false on error * @hide */ @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) log("setPriority(" + device + ", " + priority + ")"); return setConnectionPolicy(device, BluetoothAdapter.priorityToConnectionPolicy(priority)); } /** * Set connection policy of the profile * *
The device should already be paired.
* Connection policy can be one of {@link #CONNECTION_POLICY_ALLOWED},
* {@link #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN}
*
* @param device Paired bluetooth device
* @param connectionPolicy is the connection policy to set to for this profile
* @return true if connectionPolicy is set, false on error
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public boolean setConnectionPolicy(@NonNull BluetoothDevice device,
@ConnectionPolicy int connectionPolicy) {
if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)
&& (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
|| connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
try {
final SynchronousResultReceiver The priority can be any of:
* {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
*
* @param device Bluetooth device
* @return priority of the device
* @hide
*/
@RequiresLegacyBluetoothPermission
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public int getPriority(BluetoothDevice device) {
if (VDBG) log("getPriority(" + device + ")");
return BluetoothAdapter.connectionPolicyToPriority(getConnectionPolicy(device));
}
/**
* Get the connection policy of the profile.
*
* The connection policy can be any of:
* {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN},
* {@link #CONNECTION_POLICY_UNKNOWN}
*
* @param device Bluetooth device
* @return connection policy of the device
* @hide
*/
@SystemApi
@RequiresLegacyBluetoothPermission
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public @ConnectionPolicy int getConnectionPolicy(@NonNull BluetoothDevice device) {
if (VDBG) log("getConnectionPolicy(" + device + ")");
final IBluetoothHeadsetClient service = getService();
final @ConnectionPolicy int defaultValue = BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver Feature required for successful execution is being reported by: {@link
* #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
* is not supported. Feature required for successful execution is being reported by: {@link
* #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
* is not supported. Feature required for successful execution is being reported by: {@link
* #EXTRA_AG_FEATURE_REJECT_CALL}. This method invocation will fail silently when feature is not
* supported. Feature required for successful execution is being reported by: {@link
* #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
* supported. Feature required for successful execution is being reported by: {@link
* #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
* supported. Feature required for successful execution is being reported by: {@link
* #EXTRA_AG_FEATURE_MERGE_AND_DETACH}. This method invocation will fail silently when feature
* is not supported. Feature required for successful execution is being reported by: {@link
* #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}. This method invocation will fail silently when
* feature is not supported. This intent will have 2 extras:
* true
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
*
* true
if command has been issued successfully; false
* otherwise.
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean sendVendorAtCommand(BluetoothDevice device, int vendorId, String atCommand) {
if (DBG) log("sendVendorSpecificCommand()");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceivertrue
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
*
* > recv =
SynchronousResultReceiver.get();
service.getCurrentCalls(device, mAttributionSource, recv);
return Attributable.setAttributionSource(
recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue),
mAttributionSource);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Returns list of current values of AG indicators.
*
* @param device remote device
* @return bundle of AG indicators; null if device is not in CONNECTED state
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public Bundle getCurrentAgEvents(BluetoothDevice device) {
if (DBG) log("getCurrentAgEvents()");
final IBluetoothHeadsetClient service = getService();
final Bundle defaultValue = null;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver
true
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
*
* @hide
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean acceptCall(BluetoothDevice device, int flag) {
if (DBG) log("acceptCall()");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceivertrue
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean holdCall(BluetoothDevice device) {
if (DBG) log("holdCall()");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceivertrue
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
*
* true
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
*
* true
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
*
* true
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
*
* {@link BluetoothHeadsetClientCall} call
if command has been issued
* successfully; {@link null}
otherwise; upon completion HFP sends {@link
* #ACTION_CALL_CHANGED} intent in case of success; {@link #ACTION_RESULT} is sent otherwise;
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) {
if (DBG) log("dial()");
final IBluetoothHeadsetClient service = getService();
final BluetoothHeadsetClientCall defaultValue = null;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceivertrue
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_RESULT} intent;
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean sendDTMF(BluetoothDevice device, byte code) {
if (DBG) log("sendDTMF()");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceivertrue
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_LAST_VTAG} or {@link #ACTION_RESULT}
* intent;
*
* true
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean connectAudio(BluetoothDevice device) {
if (VDBG) log("connectAudio");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceivertrue
if command has been issued successfully; false
* otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
*
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean disconnectAudio(BluetoothDevice device) {
if (VDBG) log("disconnectAudio");
final IBluetoothHeadsetClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver
*
*
* @hide
*/
@SuppressLint("ActionValue")
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_NETWORK_SERVICE_STATE_CHANGED =
"android.bluetooth.headsetclient.profile.action.NETWORK_SERVICE_STATE_CHANGED";
/**
* Extra for the network service state changed intent.
*
* This extra represents the current network service state of a connected Bluetooth device.
*
* @hide
*/
@SuppressLint("ActionValue")
@SystemApi
public static final String EXTRA_NETWORK_SERVICE_STATE =
"android.bluetooth.headsetclient.extra.EXTRA_NETWORK_SERVICE_STATE";
/**
* Get the network service state for a device
*
* @param device The {@link BluetoothDevice} you want the network service state for
* @return A {@link NetworkServiceState} representing the network service state of the device,
* or null if the device is not connected
* @hide
*/
@SystemApi
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public @Nullable NetworkServiceState getNetworkServiceState(@NonNull BluetoothDevice device) {
if (device == null) {
return null;
}
Bundle agEvents = getCurrentAgEvents(device);
if (agEvents == null) {
return null;
}
boolean isServiceAvailable = (agEvents.getInt(EXTRA_NETWORK_STATUS, 0) == 1);
int signalStrength = agEvents.getInt(EXTRA_NETWORK_SIGNAL_STRENGTH, 0);
String operatorName = agEvents.getString(EXTRA_OPERATOR_NAME, null);
boolean isRoaming = (agEvents.getInt(EXTRA_NETWORK_ROAMING, 0) == 1);
return new NetworkServiceState(device, isServiceAvailable, operatorName, signalStrength,
isRoaming);
}
private boolean isEnabled() {
return mAdapter.getState() == BluetoothAdapter.STATE_ON;
}
private static boolean isValidDevice(BluetoothDevice device) {
return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
}
private static void log(String msg) {
Log.d(TAG, msg);
}
}