| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 19 | |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
| Rahul Sabnis | e8bac9b | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 21 | import android.annotation.NonNull; |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
| Selim Gurun | a117cb37 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 23 | import android.annotation.RequiresPermission; |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 24 | import android.annotation.SdkConstant; |
| 25 | import android.annotation.SdkConstant.SdkConstantType; |
| Roopa Sattiraju | ffab995 | 2021-08-20 15:06:57 -0700 | [diff] [blame] | 26 | import android.annotation.SuppressLint; |
| 27 | import android.annotation.SystemApi; |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 28 | import android.bluetooth.annotations.RequiresBluetoothConnectPermission; |
| 29 | import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission; |
| 30 | import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; |
| Artur Satayev | 3625be4 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 31 | import android.compat.annotation.UnsupportedAppUsage; |
| Jeff Sharkey | f9e176c | 2021-04-22 16:01:29 -0600 | [diff] [blame] | 32 | import android.content.AttributionSource; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Context; |
| Mathew Inwood | 049f0f5 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 34 | import android.os.Build; |
| David Duarte | f5b3bc5 | 2023-10-17 00:55:06 +0000 | [diff] [blame] | 35 | import android.os.IBinder; |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
| William Escande | 5e8e153 | 2024-03-27 18:02:23 +0000 | [diff] [blame] | 41 | import java.util.Collections; |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 42 | import java.util.List; |
| Rahul Sabnis | 9a5e351 | 2022-03-17 23:37:38 -0700 | [diff] [blame] | 43 | import java.util.Objects; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 44 | |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 46 | * Public API for controlling the Bluetooth Headset Service. This includes both Bluetooth Headset |
| 47 | * and Handsfree (v1.5) profiles. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 49 | * <p>BluetoothHeadset is a proxy object for controlling the Bluetooth Headset Service via IPC. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 51 | * <p>Use {@link BluetoothAdapter#getProfileProxy} to get the BluetoothHeadset proxy object. Use |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 52 | * {@link BluetoothAdapter#closeProfileProxy} to close the service connection. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 54 | * <p>Android only supports one connected Bluetooth Headset at a time. Each method is protected with |
| 55 | * its appropriate permission. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | */ |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 57 | public final class BluetoothHeadset implements BluetoothProfile { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | private static final String TAG = "BluetoothHeadset"; |
| William Escande | 60874f8 | 2022-10-27 22:51:52 -0700 | [diff] [blame] | 59 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 60 | private static final boolean VDBG = false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 62 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 63 | * Intent used to broadcast the change in connection state of the Headset profile. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 64 | * |
| 65 | * <p>This intent will have 3 extras: |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 66 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 67 | * <ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 68 | * <li>{@link #EXTRA_STATE} - The current state of the profile. |
| 69 | * <li>{@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. |
| 70 | * <li>{@link BluetoothDevice#EXTRA_DEVICE} - The remote device. |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 71 | * </ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 72 | * |
| 73 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of {@link |
| 74 | * #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, {@link #STATE_CONNECTED}, {@link |
| 75 | * #STATE_DISCONNECTING}. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 76 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 77 | @RequiresLegacyBluetoothPermission |
| 78 | @RequiresBluetoothConnectPermission |
| 79 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 80 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 81 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 82 | "android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 85 | * Intent used to broadcast the change in the Audio Connection state of the HFP profile. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 86 | * |
| 87 | * <p>This intent will have 3 extras: |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 88 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 89 | * <ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 90 | * <li>{@link #EXTRA_STATE} - The current state of the profile. |
| 91 | * <li>{@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. |
| 92 | * <li>{@link BluetoothDevice#EXTRA_DEVICE} - The remote device. |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 93 | * </ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 94 | * |
| 95 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of {@link |
| 96 | * #STATE_AUDIO_CONNECTED}, {@link #STATE_AUDIO_DISCONNECTED}, |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 97 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 98 | @RequiresLegacyBluetoothPermission |
| 99 | @RequiresBluetoothConnectPermission |
| 100 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 101 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 102 | public static final String ACTION_AUDIO_STATE_CHANGED = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 103 | "android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED"; |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 104 | |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 105 | /** |
| 106 | * Intent used to broadcast the selection of a connected device as active. |
| 107 | * |
| 108 | * <p>This intent will have one extra: |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 109 | * |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 110 | * <ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 111 | * <li>{@link BluetoothDevice#EXTRA_DEVICE} - The remote device. It can be null if no device |
| 112 | * is active. |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 113 | * </ul> |
| 114 | * |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 115 | * @hide |
| 116 | */ |
| William Escande | 5d864a4 | 2022-01-27 13:42:05 +0100 | [diff] [blame] | 117 | @SystemApi |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 118 | @RequiresLegacyBluetoothPermission |
| 119 | @RequiresBluetoothConnectPermission |
| 120 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 121 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| William Escande | 5d864a4 | 2022-01-27 13:42:05 +0100 | [diff] [blame] | 122 | @SuppressLint("ActionValue") |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 123 | public static final String ACTION_ACTIVE_DEVICE_CHANGED = |
| 124 | "android.bluetooth.headset.profile.action.ACTIVE_DEVICE_CHANGED"; |
| Jaikumar Ganesh | f48cda5 | 2010-04-02 14:44:43 -0700 | [diff] [blame] | 125 | |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 126 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 127 | * Intent used to broadcast that the headset has posted a vendor-specific event. |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 128 | * |
| 129 | * <p>This intent will have 4 extras and 1 category. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 130 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 131 | * <ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 132 | * <li>{@link BluetoothDevice#EXTRA_DEVICE} - The remote Bluetooth Device |
| 133 | * <li>{@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD} - The vendor specific command |
| 134 | * <li>{@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} - The AT command type which can |
| 135 | * be one of {@link #AT_CMD_TYPE_READ}, {@link #AT_CMD_TYPE_TEST}, or {@link |
| 136 | * #AT_CMD_TYPE_SET}, {@link #AT_CMD_TYPE_BASIC},{@link #AT_CMD_TYPE_ACTION}. |
| 137 | * <li>{@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS} - Command arguments. |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 138 | * </ul> |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 139 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 140 | * <p>The category is the Company ID of the vendor defining the vendor-specific command. {@link |
| 141 | * BluetoothAssignedNumbers} |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 142 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 143 | * <p>For example, for Plantronics specific events Category will be {@link |
| 144 | * #VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY}.55 |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 145 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 146 | * <p>For example, an AT+XEVENT=foo,3 will get translated into |
| 147 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 148 | * <ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 149 | * <li>EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = +XEVENT |
| 150 | * <li>EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = AT_CMD_TYPE_SET |
| 151 | * <li>EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = foo, 3 |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 152 | * </ul> |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 153 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 154 | @RequiresLegacyBluetoothPermission |
| 155 | @RequiresBluetoothConnectPermission |
| 156 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 157 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 158 | public static final String ACTION_VENDOR_SPECIFIC_HEADSET_EVENT = |
| 159 | "android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT"; |
| 160 | |
| 161 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 162 | * A String extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} intents that contains |
| 163 | * the name of the vendor-specific command. |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 164 | */ |
| 165 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD = |
| 166 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD"; |
| 167 | |
| 168 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 169 | * An int extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} intents that contains the |
| 170 | * AT command type of the vendor-specific command. |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 171 | */ |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 172 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE = |
| 173 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE"; |
| 174 | |
| 175 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 176 | * AT command type READ used with {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} For |
| 177 | * example, AT+VGM?. There are no arguments for this command type. |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 178 | */ |
| 179 | public static final int AT_CMD_TYPE_READ = 0; |
| 180 | |
| 181 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 182 | * AT command type TEST used with {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} For |
| 183 | * example, AT+VGM=?. There are no arguments for this command type. |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 184 | */ |
| 185 | public static final int AT_CMD_TYPE_TEST = 1; |
| 186 | |
| 187 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 188 | * AT command type SET used with {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} For |
| 189 | * example, AT+VGM=<args>. |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 190 | */ |
| 191 | public static final int AT_CMD_TYPE_SET = 2; |
| 192 | |
| 193 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 194 | * AT command type BASIC used with {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} For |
| 195 | * example, ATD. Single character commands and everything following the character are arguments. |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 196 | */ |
| 197 | public static final int AT_CMD_TYPE_BASIC = 3; |
| 198 | |
| 199 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 200 | * AT command type ACTION used with {@link #EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD_TYPE} For |
| 201 | * example, AT+CHUP. There are no arguments for action commands. |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 202 | */ |
| 203 | public static final int AT_CMD_TYPE_ACTION = 4; |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 206 | * A Parcelable String array extra field in {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} |
| 207 | * intents that contains the arguments to the vendor-specific command. |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 208 | */ |
| 209 | public static final String EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS = |
| 210 | "android.bluetooth.headset.extra.VENDOR_SPECIFIC_HEADSET_EVENT_ARGS"; |
| 211 | |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 212 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 213 | * The intent category to be used with {@link #ACTION_VENDOR_SPECIFIC_HEADSET_EVENT} for the |
| 214 | * companyId |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 215 | */ |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 216 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY = |
| Jaikumar Ganesh | ee628ed | 2010-09-29 11:34:59 -0700 | [diff] [blame] | 217 | "android.bluetooth.headset.intent.category.companyid"; |
| 218 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 219 | /** A vendor-specific command for unsolicited result code. */ |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 220 | public static final String VENDOR_RESULT_CODE_COMMAND_ANDROID = "+ANDROID"; |
| 221 | |
| 222 | /** |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 223 | * A vendor-specific AT command |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 224 | * |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 225 | * @hide |
| 226 | */ |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 227 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_XAPL = "+XAPL"; |
| 228 | |
| 229 | /** |
| 230 | * A vendor-specific AT command |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 231 | * |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 232 | * @hide |
| 233 | */ |
| 234 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV = "+IPHONEACCEV"; |
| 235 | |
| 236 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 237 | * Battery level indicator associated with {@link #VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV} |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 238 | * |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 239 | * @hide |
| 240 | */ |
| 241 | public static final int VENDOR_SPECIFIC_HEADSET_EVENT_IPHONEACCEV_BATTERY_LEVEL = 1; |
| 242 | |
| 243 | /** |
| 244 | * A vendor-specific AT command |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 245 | * |
| Jack He | 0dfd69b | 2017-06-20 17:09:47 -0700 | [diff] [blame] | 246 | * @hide |
| 247 | */ |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 248 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT = "+XEVENT"; |
| 249 | |
| 250 | /** |
| 251 | * Battery level indicator associated with {@link #VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT} |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 252 | * |
| Jack He | 7f9c70b | 2017-06-20 17:07:40 -0700 | [diff] [blame] | 253 | * @hide |
| 254 | */ |
| 255 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT_BATTERY_LEVEL = "BATTERY"; |
| 256 | |
| 257 | /** |
| Poompatai Puntitpong | 0e4d90a | 2023-11-03 08:58:09 +0000 | [diff] [blame] | 258 | * A vendor-specific AT command that asks for the information about device manufacturer. |
| 259 | * |
| 260 | * @hide |
| 261 | */ |
| 262 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_CGMI = "+CGMI"; |
| 263 | |
| 264 | /** |
| 265 | * A vendor-specific AT command that asks for the information about the model of the device. |
| 266 | * |
| 267 | * @hide |
| 268 | */ |
| 269 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_CGMM = "+CGMM"; |
| 270 | |
| 271 | /** |
| 272 | * A vendor-specific AT command that asks for the revision information, for Android we will |
| 273 | * return the OS version and build number. |
| 274 | * |
| 275 | * @hide |
| 276 | */ |
| 277 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_CGMR = "+CGMR"; |
| 278 | |
| 279 | /** |
| 280 | * A vendor-specific AT command that asks for the device's serial number. |
| 281 | * |
| 282 | * @hide |
| 283 | */ |
| 284 | public static final String VENDOR_SPECIFIC_HEADSET_EVENT_CGSN = "+CGSN"; |
| 285 | |
| 286 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 287 | * Headset state when SCO audio is not connected. This state can be one of {@link #EXTRA_STATE} |
| 288 | * or {@link #EXTRA_PREVIOUS_STATE} of {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 289 | */ |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 290 | public static final int STATE_AUDIO_DISCONNECTED = 10; |
| Herb Jellinek | aad41c5 | 2010-08-10 13:17:43 -0700 | [diff] [blame] | 291 | |
| 292 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 293 | * Headset state when SCO audio is connecting. This state can be one of {@link #EXTRA_STATE} or |
| 294 | * {@link #EXTRA_PREVIOUS_STATE} of {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 295 | */ |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 296 | public static final int STATE_AUDIO_CONNECTING = 11; |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 297 | |
| 298 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 299 | * Headset state when SCO audio is connected. This state can be one of {@link #EXTRA_STATE} or |
| 300 | * {@link #EXTRA_PREVIOUS_STATE} of {@link #ACTION_AUDIO_STATE_CHANGED} intent. |
| Nick Pelly | dac4c0d | 2009-09-10 10:21:56 -0700 | [diff] [blame] | 301 | */ |
| Chienyuan | 0d3bada | 2019-05-29 10:29:30 +0800 | [diff] [blame] | 302 | public static final int STATE_AUDIO_CONNECTED = 12; |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Intent used to broadcast the headset's indicator status |
| 306 | * |
| 307 | * <p>This intent will have 3 extras: |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 308 | * |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 309 | * <ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 310 | * <li>{@link #EXTRA_HF_INDICATORS_IND_ID} - The Assigned number of headset Indicator which is |
| 311 | * supported by the headset ( as indicated by AT+BIND command in the SLC sequence) or |
| 312 | * whose value is changed (indicated by AT+BIEV command) |
| 313 | * <li>{@link #EXTRA_HF_INDICATORS_IND_VALUE} - Updated value of headset indicator. |
| 314 | * <li>{@link BluetoothDevice#EXTRA_DEVICE} - Remote device. |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 315 | * </ul> |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 316 | * |
| Jack He | 48f6a8a | 2017-06-22 12:56:54 -0700 | [diff] [blame] | 317 | * <p>{@link #EXTRA_HF_INDICATORS_IND_ID} is defined by Bluetooth SIG and each of the indicators |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 318 | * are given an assigned number. Below shows the assigned number of Indicator added so far - |
| 319 | * Enhanced Safety - 1, Valid Values: 0 - Disabled, 1 - Enabled - Battery Level - 2, Valid |
| 320 | * Values: 0~100 - Remaining level of Battery |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 321 | * |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 322 | * @hide |
| 323 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 324 | @RequiresLegacyBluetoothPermission |
| 325 | @RequiresBluetoothConnectPermission |
| 326 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jeff Sharkey | d7c5566 | 2021-04-20 12:30:37 -0600 | [diff] [blame] | 327 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 328 | public static final String ACTION_HF_INDICATORS_VALUE_CHANGED = |
| 329 | "android.bluetooth.headset.action.HF_INDICATORS_VALUE_CHANGED"; |
| 330 | |
| 331 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 332 | * A int extra field in {@link #ACTION_HF_INDICATORS_VALUE_CHANGED} intents that contains the |
| 333 | * assigned number of the headset indicator as defined by Bluetooth SIG that is being sent. |
| 334 | * Value range is 0-65535 as defined in HFP 1.7 |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 335 | * |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 336 | * @hide |
| 337 | */ |
| 338 | public static final String EXTRA_HF_INDICATORS_IND_ID = |
| 339 | "android.bluetooth.headset.extra.HF_INDICATORS_IND_ID"; |
| 340 | |
| 341 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 342 | * A int extra field in {@link #ACTION_HF_INDICATORS_VALUE_CHANGED} intents that contains the |
| 343 | * value of the Headset indicator that is being sent. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 344 | * |
| Mudumba Ananth | 3246de6 | 2016-02-29 02:14:36 -0800 | [diff] [blame] | 345 | * @hide |
| 346 | */ |
| 347 | public static final String EXTRA_HF_INDICATORS_IND_VALUE = |
| 348 | "android.bluetooth.headset.extra.HF_INDICATORS_IND_VALUE"; |
| 349 | |
| Jeff Sharkey | f9e176c | 2021-04-22 16:01:29 -0600 | [diff] [blame] | 350 | private final BluetoothAdapter mAdapter; |
| 351 | private final AttributionSource mAttributionSource; |
| David Duarte | f5b3bc5 | 2023-10-17 00:55:06 +0000 | [diff] [blame] | 352 | |
| 353 | private IBluetoothHeadset mService; |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 354 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 355 | /** Create a BluetoothHeadset proxy object. */ |
| David Duarte | f5b3bc5 | 2023-10-17 00:55:06 +0000 | [diff] [blame] | 356 | /* package */ BluetoothHeadset(Context context, BluetoothAdapter adapter) { |
| Jeff Sharkey | f9e176c | 2021-04-22 16:01:29 -0600 | [diff] [blame] | 357 | mAdapter = adapter; |
| 358 | mAttributionSource = adapter.getAttributionSource(); |
| David Duarte | f5b3bc5 | 2023-10-17 00:55:06 +0000 | [diff] [blame] | 359 | mService = null; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | /** |
| Santiago Seifert | efe4071 | 2023-01-06 13:54:17 +0000 | [diff] [blame] | 363 | * Close the connection to the backing service. Other public functions of BluetoothHeadset will |
| 364 | * return default error results once close() has been called. Multiple invocations of close() |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | * are ok. |
| Santiago Seifert | efe4071 | 2023-01-06 13:54:17 +0000 | [diff] [blame] | 366 | * |
| 367 | * @hide |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | */ |
| Mathew Inwood | 7d54389 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 369 | @UnsupportedAppUsage |
| Santiago Seifert | efe4071 | 2023-01-06 13:54:17 +0000 | [diff] [blame] | 370 | public void close() { |
| David Duarte | f5b3bc5 | 2023-10-17 00:55:06 +0000 | [diff] [blame] | 371 | mAdapter.closeProfileProxy(this); |
| 372 | } |
| 373 | |
| 374 | /** @hide */ |
| 375 | @Override |
| 376 | public void onServiceConnected(IBinder service) { |
| 377 | mService = IBluetoothHeadset.Stub.asInterface(service); |
| 378 | } |
| 379 | |
| 380 | /** @hide */ |
| 381 | @Override |
| 382 | public void onServiceDisconnected() { |
| 383 | mService = null; |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 384 | } |
| fredc | 3c71964 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 385 | |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 386 | private IBluetoothHeadset getService() { |
| David Duarte | f5b3bc5 | 2023-10-17 00:55:06 +0000 | [diff] [blame] | 387 | return mService; |
| 388 | } |
| 389 | |
| 390 | /** @hide */ |
| 391 | @Override |
| 392 | public BluetoothAdapter getAdapter() { |
| 393 | return mAdapter; |
| Jeff Sharkey | db38eb7 | 2021-05-24 10:00:23 -0600 | [diff] [blame] | 394 | } |
| 395 | |
| David Duarte | 5a02bb4 | 2023-12-04 23:07:42 +0000 | [diff] [blame] | 396 | /** @hide */ |
| Jeff Sharkey | db38eb7 | 2021-05-24 10:00:23 -0600 | [diff] [blame] | 397 | @Override |
| David Duarte | 5a02bb4 | 2023-12-04 23:07:42 +0000 | [diff] [blame] | 398 | @SuppressWarnings("Finalize") // empty finalize for api signature |
| Jeff Sharkey | db38eb7 | 2021-05-24 10:00:23 -0600 | [diff] [blame] | 399 | protected void finalize() throws Throwable { |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 400 | // The empty finalize needs to be kept or the |
| 401 | // cts signature tests would fail. |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 405 | * Initiate connection to a profile of the remote bluetooth device. |
| 406 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 407 | * <p>Currently, the system supports only 1 connection to the headset/handsfree profile. The API |
| 408 | * will automatically disconnect connected devices before connecting. |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 409 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 410 | * <p>This API returns false in scenarios like the profile on the device is already connected or |
| 411 | * Bluetooth is not turned on. When this API returns true, it is guaranteed that connection |
| 412 | * state intent for the profile will be broadcasted with the state. Users can get the connection |
| 413 | * state of the profile from this intent. |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 414 | * |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 415 | * @param device Remote Bluetooth Device |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 416 | * @return false on immediate error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 417 | * @hide |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | */ |
| Selim Gurun | a117cb37 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 419 | @SystemApi |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 420 | @RequiresLegacyBluetoothAdminPermission |
| 421 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 422 | @RequiresPermission( |
| 423 | allOf = { |
| 424 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 425 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 426 | }) |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 427 | public boolean connect(BluetoothDevice device) { |
| 428 | if (DBG) log("connect(" + device + ")"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 429 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 430 | if (service == null) { |
| 431 | Log.w(TAG, "Proxy not attached to service"); |
| 432 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 433 | } else if (isEnabled() && isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 435 | return service.connect(device, mAttributionSource); |
| 436 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 437 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 438 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 440 | return false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 444 | * Initiate disconnection from a profile |
| 445 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 446 | * <p>This API will return false in scenarios like the profile on the Bluetooth device is not in |
| 447 | * connected state etc. When this API returns, true, it is guaranteed that the connection state |
| 448 | * change intent will be broadcasted with the state. Users can get the disconnection state of |
| 449 | * the profile from this intent. |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 450 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 451 | * <p>If the disconnection is initiated by a remote device, the state will transition from |
| 452 | * {@link #STATE_CONNECTED} to {@link #STATE_DISCONNECTED}. If the disconnect is initiated by |
| 453 | * the host (local) device the state will transition from {@link #STATE_CONNECTED} to state |
| 454 | * {@link #STATE_DISCONNECTING} to state {@link #STATE_DISCONNECTED}. The transition to {@link |
| 455 | * #STATE_DISCONNECTING} can be used to distinguish between the two scenarios. |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 456 | * |
| Jaikumar Ganesh | a7266d3 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 457 | * @param device Remote Bluetooth Device |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 458 | * @return false on immediate error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 459 | * @hide |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | */ |
| Selim Gurun | a117cb37 | 2017-10-17 17:01:38 -0700 | [diff] [blame] | 461 | @SystemApi |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 462 | @RequiresLegacyBluetoothAdminPermission |
| 463 | @RequiresBluetoothConnectPermission |
| 464 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 465 | public boolean disconnect(BluetoothDevice device) { |
| 466 | if (DBG) log("disconnect(" + device + ")"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 467 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 468 | if (service == null) { |
| 469 | Log.w(TAG, "Proxy not attached to service"); |
| 470 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 471 | } else if (isEnabled() && isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 473 | return service.disconnect(device, mAttributionSource); |
| 474 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 475 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 476 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 478 | return false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 481 | /** {@inheritDoc} */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 482 | @Override |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 483 | @RequiresBluetoothConnectPermission |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 484 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 485 | public List<BluetoothDevice> getConnectedDevices() { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 486 | if (VDBG) log("getConnectedDevices()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 487 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 488 | if (service == null) { |
| 489 | Log.w(TAG, "Proxy not attached to service"); |
| 490 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 491 | } else if (isEnabled()) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | try { |
| Jeff Sharkey | 98f3044 | 2021-06-03 09:26:53 -0600 | [diff] [blame] | 493 | return Attributable.setAttributionSource( |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 494 | service.getConnectedDevices(mAttributionSource), mAttributionSource); |
| 495 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 496 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 497 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | } |
| William Escande | 5e8e153 | 2024-03-27 18:02:23 +0000 | [diff] [blame] | 499 | return Collections.emptyList(); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 502 | /** {@inheritDoc} */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 503 | @Override |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 504 | @RequiresBluetoothConnectPermission |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 505 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jaikumar Ganesh | d8fc4dd | 2010-10-18 16:41:53 -0700 | [diff] [blame] | 506 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 507 | if (VDBG) log("getDevicesMatchingStates()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 508 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 509 | if (service == null) { |
| 510 | Log.w(TAG, "Proxy not attached to service"); |
| 511 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 512 | } else if (isEnabled()) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | try { |
| Jeff Sharkey | 98f3044 | 2021-06-03 09:26:53 -0600 | [diff] [blame] | 514 | return Attributable.setAttributionSource( |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 515 | service.getDevicesMatchingConnectionStates(states, mAttributionSource), |
| Jeff Sharkey | 43ee69e | 2021-04-23 14:13:57 -0600 | [diff] [blame] | 516 | mAttributionSource); |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 517 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 518 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 519 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | } |
| William Escande | 5e8e153 | 2024-03-27 18:02:23 +0000 | [diff] [blame] | 521 | return Collections.emptyList(); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 524 | /** {@inheritDoc} */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 525 | @Override |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 526 | @RequiresBluetoothConnectPermission |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 527 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 528 | public int getConnectionState(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 529 | if (VDBG) log("getConnectionState(" + device + ")"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 530 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 531 | if (service == null) { |
| 532 | Log.w(TAG, "Proxy not attached to service"); |
| 533 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 534 | } else if (isEnabled() && isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 536 | return service.getConnectionState(device, mAttributionSource); |
| 537 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 538 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 539 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 541 | return BluetoothProfile.STATE_DISCONNECTED; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| Rahul Sabnis | e8bac9b | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 545 | * Set connection policy of the profile |
| 546 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 547 | * <p>The device should already be paired. Connection policy can be one of {@link |
| 548 | * #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, {@link |
| 549 | * #CONNECTION_POLICY_UNKNOWN} |
| Rahul Sabnis | e8bac9b | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 550 | * |
| 551 | * @param device Paired bluetooth device |
| 552 | * @param connectionPolicy is the connection policy to set to for this profile |
| 553 | * @return true if connectionPolicy is set, false on error |
| 554 | * @hide |
| 555 | */ |
| 556 | @SystemApi |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 557 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 558 | @RequiresPermission( |
| 559 | allOf = { |
| 560 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 561 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 562 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 563 | }) |
| 564 | public boolean setConnectionPolicy( |
| 565 | @NonNull BluetoothDevice device, @ConnectionPolicy int connectionPolicy) { |
| Rahul Sabnis | e8bac9b | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 566 | if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 567 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 568 | if (service == null) { |
| 569 | Log.w(TAG, "Proxy not attached to service"); |
| 570 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 571 | } else if (isEnabled() |
| 572 | && isValidDevice(device) |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 573 | && (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 574 | || connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 576 | return service.setConnectionPolicy(device, connectionPolicy, mAttributionSource); |
| 577 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 578 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 579 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 581 | return false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /** |
| Felix Stern | 692d36d | 2023-09-08 09:55:56 +0000 | [diff] [blame] | 585 | * Get the priority of the profile. |
| 586 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 587 | * <p>The priority can be any of: {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, {@link |
| 588 | * #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| Felix Stern | 692d36d | 2023-09-08 09:55:56 +0000 | [diff] [blame] | 589 | * |
| 590 | * @param device Bluetooth device |
| 591 | * @return priority of the device |
| 592 | * @hide |
| 593 | */ |
| 594 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 595 | @RequiresLegacyBluetoothPermission |
| 596 | @RequiresBluetoothConnectPermission |
| 597 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| 598 | public int getPriority(BluetoothDevice device) { |
| 599 | if (VDBG) log("getPriority(" + device + ")"); |
| 600 | return BluetoothAdapter.connectionPolicyToPriority(getConnectionPolicy(device)); |
| 601 | } |
| 602 | |
| 603 | /** |
| Rahul Sabnis | e8bac9b | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 604 | * Get the connection policy of the profile. |
| 605 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 606 | * <p>The connection policy can be any of: {@link #CONNECTION_POLICY_ALLOWED}, {@link |
| 607 | * #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN} |
| Rahul Sabnis | e8bac9b | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 608 | * |
| 609 | * @param device Bluetooth device |
| 610 | * @return connection policy of the device |
| 611 | * @hide |
| 612 | */ |
| 613 | @SystemApi |
| Jeff Sharkey | 43ee69e | 2021-04-23 14:13:57 -0600 | [diff] [blame] | 614 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 615 | @RequiresPermission( |
| 616 | allOf = { |
| 617 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 618 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 619 | }) |
| Rahul Sabnis | e8bac9b | 2019-11-27 18:09:33 -0800 | [diff] [blame] | 620 | public @ConnectionPolicy int getConnectionPolicy(@NonNull BluetoothDevice device) { |
| 621 | if (VDBG) log("getConnectionPolicy(" + device + ")"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 622 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 623 | if (service == null) { |
| 624 | Log.w(TAG, "Proxy not attached to service"); |
| 625 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 626 | } else if (isEnabled() && isValidDevice(device)) { |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 628 | return service.getConnectionPolicy(device, mAttributionSource); |
| 629 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 630 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 631 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 633 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /** |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 637 | * Checks whether the headset supports some form of noise reduction |
| 638 | * |
| 639 | * @param device Bluetooth device |
| 640 | * @return true if echo cancellation and/or noise reduction is supported, false otherwise |
| 641 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 642 | @RequiresLegacyBluetoothPermission |
| 643 | @RequiresBluetoothConnectPermission |
| 644 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 645 | public boolean isNoiseReductionSupported(@NonNull BluetoothDevice device) { |
| 646 | if (DBG) log("isNoiseReductionSupported()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 647 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 648 | if (service == null) { |
| 649 | Log.w(TAG, "Proxy not attached to service"); |
| 650 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 651 | } else if (isEnabled() && isValidDevice(device)) { |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 652 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 653 | return service.isNoiseReductionSupported(device, mAttributionSource); |
| 654 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 655 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 656 | } |
| 657 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 658 | return false; |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Checks whether the headset supports voice recognition |
| 663 | * |
| 664 | * @param device Bluetooth device |
| 665 | * @return true if voice recognition is supported, false otherwise |
| 666 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 667 | @RequiresLegacyBluetoothPermission |
| 668 | @RequiresBluetoothConnectPermission |
| 669 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 670 | public boolean isVoiceRecognitionSupported(@NonNull BluetoothDevice device) { |
| 671 | if (DBG) log("isVoiceRecognitionSupported()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 672 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 673 | if (service == null) { |
| 674 | Log.w(TAG, "Proxy not attached to service"); |
| 675 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 676 | } else if (isEnabled() && isValidDevice(device)) { |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 677 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 678 | return service.isVoiceRecognitionSupported(device, mAttributionSource); |
| 679 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 680 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 681 | } |
| 682 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 683 | return false; |
| Rahul Sabnis | c611e73 | 2020-12-14 10:54:45 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 687 | * Start Bluetooth voice recognition. This methods sends the voice recognition AT command to the |
| 688 | * headset and establishes the audio connection. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 689 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 690 | * <p>Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. If this function returns true, |
| 691 | * this intent will be broadcasted with {@link #EXTRA_STATE} set to {@link |
| 692 | * #STATE_AUDIO_CONNECTING}. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 693 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 694 | * <p>{@link #EXTRA_STATE} will transition from {@link #STATE_AUDIO_CONNECTING} to {@link |
| 695 | * #STATE_AUDIO_CONNECTED} when audio connection is established and to {@link |
| 696 | * #STATE_AUDIO_DISCONNECTED} in case of failure to establish the audio connection. |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 697 | * |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 698 | * @param device Bluetooth headset |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 699 | * @return false if there is no headset connected, or the connected headset doesn't support |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 700 | * voice recognition, or voice recognition is already started, or audio channel is occupied, |
| 701 | * or on error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 702 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 703 | @RequiresLegacyBluetoothPermission |
| 704 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 705 | @RequiresPermission( |
| 706 | allOf = { |
| 707 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 708 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 709 | }) |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 710 | public boolean startVoiceRecognition(BluetoothDevice device) { |
| 711 | if (DBG) log("startVoiceRecognition()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 712 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 713 | if (service == null) { |
| 714 | Log.w(TAG, "Proxy not attached to service"); |
| 715 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 716 | } else if (isEnabled() && isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 717 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 718 | return service.startVoiceRecognition(device, mAttributionSource); |
| 719 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 720 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 723 | return false; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 727 | * Stop Bluetooth Voice Recognition mode, and shut down the Bluetooth audio path. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 728 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 729 | * <p>Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. If this function returns true, |
| 730 | * this intent will be broadcasted with {@link #EXTRA_STATE} set to {@link |
| 731 | * #STATE_AUDIO_DISCONNECTED}. |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 732 | * |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 733 | * @param device Bluetooth headset |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 734 | * @return false if there is no headset connected, or voice recognition has not started, or |
| 735 | * voice recognition has ended on this headset, or on error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 736 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 737 | @RequiresLegacyBluetoothPermission |
| 738 | @RequiresBluetoothConnectPermission |
| 739 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 740 | public boolean stopVoiceRecognition(BluetoothDevice device) { |
| 741 | if (DBG) log("stopVoiceRecognition()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 742 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 743 | if (service == null) { |
| 744 | Log.w(TAG, "Proxy not attached to service"); |
| 745 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 746 | } else if (isEnabled() && isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 747 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 748 | return service.stopVoiceRecognition(device, mAttributionSource); |
| 749 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 750 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 753 | return false; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Check if Bluetooth SCO audio is connected. |
| 758 | * |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 759 | * @param device Bluetooth headset |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 760 | * @return true if SCO is connected, false otherwise or on error |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 761 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 762 | @RequiresLegacyBluetoothPermission |
| 763 | @RequiresBluetoothConnectPermission |
| 764 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 765 | public boolean isAudioConnected(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 766 | if (VDBG) log("isAudioConnected()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 767 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 768 | if (service == null) { |
| 769 | Log.w(TAG, "Proxy not attached to service"); |
| 770 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 771 | } else if (isEnabled() && isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 772 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 773 | return service.isAudioConnected(device, mAttributionSource); |
| 774 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 775 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 778 | return false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | } |
| 780 | |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 781 | /** @hide */ |
| 782 | @Retention(RetentionPolicy.SOURCE) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 783 | @IntDef( |
| 784 | value = { |
| 785 | BluetoothHeadset.STATE_AUDIO_DISCONNECTED, |
| 786 | BluetoothHeadset.STATE_AUDIO_CONNECTING, |
| 787 | BluetoothHeadset.STATE_AUDIO_CONNECTED, |
| 788 | BluetoothStatusCodes.ERROR_TIMEOUT |
| 789 | }) |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 790 | public @interface GetAudioStateReturnValues {} |
| 791 | |
| Jaikumar Ganesh | b84bbd9 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 792 | /** |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 793 | * Get the current audio state of the Headset. |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 794 | * |
| 795 | * @param device is the Bluetooth device for which the audio state is being queried |
| 796 | * @return the audio state of the device or an error code |
| Rahul Sabnis | 9a5e351 | 2022-03-17 23:37:38 -0700 | [diff] [blame] | 797 | * @throws NullPointerException if the device is null |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 798 | * @hide |
| 799 | */ |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 800 | @SystemApi |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 801 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 802 | @RequiresPermission( |
| 803 | allOf = { |
| 804 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 805 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 806 | }) |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 807 | public @GetAudioStateReturnValues int getAudioState(@NonNull BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 808 | if (VDBG) log("getAudioState"); |
| Rahul Sabnis | 9a5e351 | 2022-03-17 23:37:38 -0700 | [diff] [blame] | 809 | Objects.requireNonNull(device); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 810 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 811 | if (service == null) { |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 812 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 813 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 814 | } else if (!isDisabled()) { |
| 815 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 816 | return service.getAudioState(device, mAttributionSource); |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 817 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 818 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| William Escande | 17a262f | 2023-09-06 21:48:54 -0700 | [diff] [blame] | 819 | throw e.rethrowAsRuntimeException(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 820 | } |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 821 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 822 | return BluetoothHeadset.STATE_AUDIO_DISCONNECTED; |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 825 | /** @hide */ |
| 826 | @Retention(RetentionPolicy.SOURCE) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 827 | @IntDef( |
| 828 | value = { |
| 829 | BluetoothStatusCodes.SUCCESS, |
| 830 | BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND, |
| 831 | BluetoothStatusCodes.ERROR_TIMEOUT, |
| 832 | BluetoothStatusCodes.ERROR_UNKNOWN, |
| 833 | }) |
| Md Shahriar Hossain Sajib | 8b4df2f | 2022-02-18 13:56:27 +0800 | [diff] [blame] | 834 | public @interface SetAudioRouteAllowedReturnValues {} |
| 835 | |
| 836 | /** @hide */ |
| 837 | @Retention(RetentionPolicy.SOURCE) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 838 | @IntDef( |
| 839 | value = { |
| 840 | BluetoothStatusCodes.ALLOWED, |
| 841 | BluetoothStatusCodes.NOT_ALLOWED, |
| 842 | BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND, |
| 843 | BluetoothStatusCodes.ERROR_TIMEOUT, |
| 844 | BluetoothStatusCodes.ERROR_UNKNOWN, |
| 845 | }) |
| Md Shahriar Hossain Sajib | 8b4df2f | 2022-02-18 13:56:27 +0800 | [diff] [blame] | 846 | public @interface GetAudioRouteAllowedReturnValues {} |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 847 | |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 848 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 849 | * Sets whether audio routing is allowed. When set to {@code false}, the AG will not route any |
| 850 | * audio to the HF unless explicitly told to. This method should be used in cases where the SCO |
| 851 | * channel is shared between multiple profiles and must be delegated by a source knowledgeable. |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 852 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 853 | * @param allowed {@code true} if the profile can reroute audio, {@code false} otherwise. |
| 854 | * @return {@link BluetoothStatusCodes#SUCCESS} upon successful setting, otherwise an error |
| 855 | * code. |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 856 | * @hide |
| 857 | */ |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 858 | @SystemApi |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 859 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 860 | @RequiresPermission( |
| 861 | allOf = { |
| 862 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 863 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 864 | }) |
| Md Shahriar Hossain Sajib | 8b4df2f | 2022-02-18 13:56:27 +0800 | [diff] [blame] | 865 | public @SetAudioRouteAllowedReturnValues int setAudioRouteAllowed(boolean allowed) { |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 866 | if (VDBG) log("setAudioRouteAllowed"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 867 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 868 | if (service == null) { |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 869 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 870 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 871 | return BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND; |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 872 | } else if (isEnabled()) { |
| 873 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 874 | service.setAudioRouteAllowed(allowed, mAttributionSource); |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 875 | return BluetoothStatusCodes.SUCCESS; |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 876 | } catch (RemoteException e) { |
| 877 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| William Escande | 17a262f | 2023-09-06 21:48:54 -0700 | [diff] [blame] | 878 | throw e.rethrowAsRuntimeException(); |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 879 | } |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 880 | } |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 881 | |
| 882 | Log.e(TAG, "setAudioRouteAllowed: Bluetooth disabled, but profile service still bound"); |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 883 | return BluetoothStatusCodes.ERROR_UNKNOWN; |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 887 | * @return {@link BluetoothStatusCodes#ALLOWED} if audio routing is allowed, {@link |
| 888 | * BluetoothStatusCodes#NOT_ALLOWED} if audio routing is not allowed, or an error code if an |
| 889 | * error occurs. see {@link #setAudioRouteAllowed(boolean)}. |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 890 | * @hide |
| 891 | */ |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 892 | @SystemApi |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 893 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 894 | @RequiresPermission( |
| 895 | allOf = { |
| 896 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 897 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 898 | }) |
| Md Shahriar Hossain Sajib | 8b4df2f | 2022-02-18 13:56:27 +0800 | [diff] [blame] | 899 | public @GetAudioRouteAllowedReturnValues int getAudioRouteAllowed() { |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 900 | if (VDBG) log("getAudioRouteAllowed"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 901 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 902 | if (service == null) { |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 903 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 904 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 905 | return BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND; |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 906 | } else if (isEnabled()) { |
| 907 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 908 | return service.getAudioRouteAllowed(mAttributionSource) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 909 | ? BluetoothStatusCodes.ALLOWED |
| 910 | : BluetoothStatusCodes.NOT_ALLOWED; |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 911 | } catch (RemoteException e) { |
| 912 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| William Escande | 17a262f | 2023-09-06 21:48:54 -0700 | [diff] [blame] | 913 | throw e.rethrowAsRuntimeException(); |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 914 | } |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 915 | } |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 916 | |
| 917 | Log.e(TAG, "getAudioRouteAllowed: Bluetooth disabled, but profile service still bound"); |
| Md Shahriar Hossain Sajib | 7901c00 | 2022-01-19 15:37:26 +0800 | [diff] [blame] | 918 | return BluetoothStatusCodes.ERROR_UNKNOWN; |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /** |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 922 | * Force SCO audio to be opened regardless any other restrictions |
| 923 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 924 | * @param forced Whether or not SCO audio connection should be forced: True to force SCO audio |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 925 | * False to use SCO audio in normal manner |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 926 | * @hide |
| 927 | */ |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 928 | @RequiresBluetoothConnectPermission |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 929 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 930 | public void setForceScoAudio(boolean forced) { |
| 931 | if (VDBG) log("setForceScoAudio " + String.valueOf(forced)); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 932 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 933 | if (service == null) { |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 934 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 935 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 936 | } else if (isEnabled()) { |
| 937 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 938 | service.setForceScoAudio(forced, mAttributionSource); |
| 939 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 940 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 941 | } |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 945 | /** @hide */ |
| 946 | @Retention(RetentionPolicy.SOURCE) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 947 | @IntDef( |
| 948 | value = { |
| 949 | BluetoothStatusCodes.SUCCESS, |
| 950 | BluetoothStatusCodes.ERROR_UNKNOWN, |
| 951 | BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND, |
| 952 | BluetoothStatusCodes.ERROR_TIMEOUT, |
| 953 | BluetoothStatusCodes.ERROR_AUDIO_DEVICE_ALREADY_CONNECTED, |
| 954 | BluetoothStatusCodes.ERROR_NO_ACTIVE_DEVICES, |
| 955 | BluetoothStatusCodes.ERROR_NOT_ACTIVE_DEVICE, |
| 956 | BluetoothStatusCodes.ERROR_AUDIO_ROUTE_BLOCKED, |
| 957 | BluetoothStatusCodes.ERROR_CALL_ACTIVE, |
| 958 | BluetoothStatusCodes.ERROR_PROFILE_NOT_CONNECTED |
| 959 | }) |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 960 | public @interface ConnectAudioReturnValues {} |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 961 | |
| 962 | /** |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 963 | * Initiates a connection of SCO audio to the current active HFP device. The active HFP device |
| 964 | * can be identified with {@link BluetoothAdapter#getActiveDevices(int)}. |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 965 | * |
| 966 | * <p>If this function returns {@link BluetoothStatusCodes#SUCCESS}, the intent {@link |
| 967 | * #ACTION_AUDIO_STATE_CHANGED} will be broadcasted twice. First with {@link #EXTRA_STATE} set |
| 968 | * to {@link #STATE_AUDIO_CONNECTING}. This will be followed by a broadcast with {@link |
| 969 | * #EXTRA_STATE} set to either {@link #STATE_AUDIO_CONNECTED} if the audio connection is |
| 970 | * established or {@link #STATE_AUDIO_DISCONNECTED} if there was a failure in establishing the |
| 971 | * audio connection. |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 972 | * |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 973 | * @return whether the connection was successfully initiated or an error code on failure |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 974 | * @hide |
| 975 | */ |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 976 | @SystemApi |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 977 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 978 | @RequiresPermission( |
| 979 | allOf = { |
| 980 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 981 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 982 | }) |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 983 | public @ConnectAudioReturnValues int connectAudio() { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 984 | if (VDBG) log("connectAudio()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 985 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 986 | if (service == null) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 987 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 988 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| Rahul Sabnis | b83a2fe | 2022-01-24 14:38:40 -0800 | [diff] [blame] | 989 | return BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND; |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 990 | } else if (isEnabled()) { |
| 991 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 992 | return service.connectAudio(mAttributionSource); |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 993 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 994 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| William Escande | 17a262f | 2023-09-06 21:48:54 -0700 | [diff] [blame] | 995 | throw e.rethrowAsRuntimeException(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 996 | } |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 997 | } |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 998 | |
| 999 | Log.e(TAG, "connectAudio: Bluetooth disabled, but profile service still bound"); |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1000 | return BluetoothStatusCodes.ERROR_UNKNOWN; |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 1003 | /** @hide */ |
| 1004 | @Retention(RetentionPolicy.SOURCE) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1005 | @IntDef( |
| 1006 | value = { |
| 1007 | BluetoothStatusCodes.SUCCESS, |
| 1008 | BluetoothStatusCodes.ERROR_UNKNOWN, |
| 1009 | BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND, |
| 1010 | BluetoothStatusCodes.ERROR_TIMEOUT, |
| 1011 | BluetoothStatusCodes.ERROR_PROFILE_NOT_CONNECTED, |
| 1012 | BluetoothStatusCodes.ERROR_AUDIO_DEVICE_ALREADY_DISCONNECTED |
| 1013 | }) |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 1014 | public @interface DisconnectAudioReturnValues {} |
| 1015 | |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1016 | /** |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 1017 | * Initiates a disconnection of HFP SCO audio from actively connected devices. It also tears |
| 1018 | * down voice recognition or virtual voice call, if any exists. |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1019 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1020 | * <p>If this function returns {@link BluetoothStatusCodes#SUCCESS}, the intent {@link |
| 1021 | * #ACTION_AUDIO_STATE_CHANGED} will be broadcasted with {@link #EXTRA_STATE} set to {@link |
| 1022 | * #STATE_AUDIO_DISCONNECTED}. |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1023 | * |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 1024 | * @return whether the disconnection was initiated successfully or an error code on failure |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1025 | * @hide |
| 1026 | */ |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 1027 | @SystemApi |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 1028 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1029 | @RequiresPermission( |
| 1030 | allOf = { |
| 1031 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 1032 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 1033 | }) |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 1034 | public @DisconnectAudioReturnValues int disconnectAudio() { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1035 | if (VDBG) log("disconnectAudio()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1036 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1037 | if (service == null) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1038 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1039 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| Rahul Sabnis | b83a2fe | 2022-01-24 14:38:40 -0800 | [diff] [blame] | 1040 | return BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND; |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1041 | } else if (isEnabled()) { |
| 1042 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1043 | return service.disconnectAudio(mAttributionSource); |
| Rahul Sabnis | 5ed1b84 | 2021-12-23 11:36:40 -0800 | [diff] [blame] | 1044 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1045 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| William Escande | 17a262f | 2023-09-06 21:48:54 -0700 | [diff] [blame] | 1046 | throw e.rethrowAsRuntimeException(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1047 | } |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1048 | } |
| Rahul Sabnis | fc0cd8b | 2022-03-22 16:38:38 -0700 | [diff] [blame] | 1049 | |
| 1050 | Log.e(TAG, "disconnectAudio: Bluetooth disabled, but profile service still bound"); |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1051 | return BluetoothStatusCodes.ERROR_UNKNOWN; |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /** |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1055 | * Initiates a SCO channel connection as a virtual voice call to the current active device |
| 1056 | * Active handsfree device will be notified of incoming call and connected call. |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1057 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1058 | * <p>Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. If this function returns true, |
| 1059 | * this intent will be broadcasted with {@link #EXTRA_STATE} set to {@link |
| 1060 | * #STATE_AUDIO_CONNECTING}. |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1061 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1062 | * <p>{@link #EXTRA_STATE} will transition from {@link #STATE_AUDIO_CONNECTING} to {@link |
| 1063 | * #STATE_AUDIO_CONNECTED} when audio connection is established and to {@link |
| 1064 | * #STATE_AUDIO_DISCONNECTED} in case of failure to establish the audio connection. |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1065 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1066 | * @return true if successful, false if one of the following case applies - SCO audio is not |
| 1067 | * idle (connecting or connected) - virtual call has already started - there is no active |
| 1068 | * device - a Telecom managed call is going on - binder is dead or Bluetooth is disabled or |
| 1069 | * other error |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1070 | * @hide |
| 1071 | */ |
| Roopa Sattiraju | ffab995 | 2021-08-20 15:06:57 -0700 | [diff] [blame] | 1072 | @SystemApi |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 1073 | @RequiresLegacyBluetoothAdminPermission |
| 1074 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1075 | @RequiresPermission( |
| 1076 | allOf = { |
| 1077 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 1078 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 1079 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 1080 | }) |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1081 | public boolean startScoUsingVirtualVoiceCall() { |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 1082 | if (DBG) log("startScoUsingVirtualVoiceCall()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1083 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1084 | if (service == null) { |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1085 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1086 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1087 | } else if (isEnabled()) { |
| 1088 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1089 | return service.startScoUsingVirtualVoiceCall(mAttributionSource); |
| 1090 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1091 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 1092 | } |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1093 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1094 | return false; |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /** |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1098 | * Terminates an ongoing SCO connection and the associated virtual call. |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1099 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1100 | * <p>Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. If this function returns true, |
| 1101 | * this intent will be broadcasted with {@link #EXTRA_STATE} set to {@link |
| 1102 | * #STATE_AUDIO_DISCONNECTED}. |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1103 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1104 | * @return true if successful, false if one of the following case applies - virtual voice call |
| 1105 | * is not started or has ended - binder is dead or Bluetooth is disabled or other error |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1106 | * @hide |
| 1107 | */ |
| Roopa Sattiraju | ffab995 | 2021-08-20 15:06:57 -0700 | [diff] [blame] | 1108 | @SystemApi |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 1109 | @RequiresLegacyBluetoothAdminPermission |
| 1110 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1111 | @RequiresPermission( |
| 1112 | allOf = { |
| 1113 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 1114 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 1115 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 1116 | }) |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1117 | public boolean stopScoUsingVirtualVoiceCall() { |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 1118 | if (DBG) log("stopScoUsingVirtualVoiceCall()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1119 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1120 | if (service == null) { |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1121 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1122 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1123 | } else if (isEnabled()) { |
| 1124 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1125 | return service.stopScoUsingVirtualVoiceCall(mAttributionSource); |
| 1126 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1127 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 1128 | } |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1129 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1130 | return false; |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1133 | /** |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1134 | * Notify Headset of phone state change. This is a backdoor for phone app to call |
| 1135 | * BluetoothHeadset since there is currently not a good way to get precise call state change |
| 1136 | * outside of phone app. |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1137 | * |
| 1138 | * @hide |
| 1139 | */ |
| Mathew Inwood | 049f0f5 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1140 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 1141 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1142 | @RequiresPermission( |
| 1143 | allOf = { |
| 1144 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 1145 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 1146 | }) |
| 1147 | public void phoneStateChanged( |
| 1148 | int numActive, int numHeld, int callState, String number, int type, String name) { |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1149 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1150 | if (service == null) { |
| 1151 | Log.w(TAG, "Proxy not attached to service"); |
| 1152 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1153 | } else if (isEnabled()) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1154 | try { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1155 | service.phoneStateChanged( |
| 1156 | numActive, numHeld, callState, number, type, name, mAttributionSource); |
| 1157 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1158 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1159 | } |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /** |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1164 | * Send Headset of CLCC response |
| 1165 | * |
| 1166 | * @hide |
| 1167 | */ |
| Jeff Sharkey | 5ba8bfc | 2021-04-16 09:53:23 -0600 | [diff] [blame] | 1168 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1169 | @RequiresPermission( |
| 1170 | allOf = { |
| 1171 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 1172 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 1173 | }) |
| 1174 | public void clccResponse( |
| 1175 | int index, int direction, int status, int mode, boolean mpty, String number, int type) { |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1176 | final IBluetoothHeadset service = getService(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1177 | if (service == null) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1178 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1179 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1180 | } else if (isEnabled()) { |
| 1181 | try { |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1182 | service.clccResponse( |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1183 | index, direction, status, mode, mpty, number, type, mAttributionSource); |
| 1184 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1185 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 1186 | } |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1190 | /** |
| 1191 | * Sends a vendor-specific unsolicited result code to the headset. |
| 1192 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1193 | * <p>The actual string to be sent is <code>command + ": " + arg</code>. For example, if {@code |
| 1194 | * command} is {@link #VENDOR_RESULT_CODE_COMMAND_ANDROID} and {@code arg} is {@code "0"}, the |
| 1195 | * string <code>"+ANDROID: 0"</code> will be sent. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1196 | * |
| Ying Wang | 29d9389 | 2013-08-26 17:48:22 -0700 | [diff] [blame] | 1197 | * <p>Currently only {@link #VENDOR_RESULT_CODE_COMMAND_ANDROID} is allowed as {@code command}. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1198 | * |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1199 | * @param device Bluetooth headset. |
| 1200 | * @param command A vendor-specific command. |
| 1201 | * @param arg The argument that will be attached to the command. |
| 1202 | * @return {@code false} if there is no headset connected, or if the command is not an allowed |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1203 | * vendor-specific unsolicited result code, or on error. {@code true} otherwise. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1204 | * @throws IllegalArgumentException if {@code command} is {@code null}. |
| 1205 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 1206 | @RequiresLegacyBluetoothPermission |
| 1207 | @RequiresBluetoothConnectPermission |
| 1208 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1209 | public boolean sendVendorSpecificResultCode( |
| 1210 | BluetoothDevice device, String command, String arg) { |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1211 | if (DBG) { |
| 1212 | log("sendVendorSpecificResultCode()"); |
| 1213 | } |
| 1214 | if (command == null) { |
| 1215 | throw new IllegalArgumentException("command is null"); |
| 1216 | } |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1217 | final IBluetoothHeadset service = getService(); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1218 | if (service == null) { |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1219 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1220 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1221 | } else if (isEnabled() && isValidDevice(device)) { |
| 1222 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1223 | return service.sendVendorSpecificResultCode( |
| 1224 | device, command, arg, mAttributionSource); |
| 1225 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1226 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 1227 | } |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1228 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1229 | return false; |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| Mudumba Ananth | 80bf628 | 2014-04-27 13:11:00 -0700 | [diff] [blame] | 1232 | /** |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1233 | * Select a connected device as active. |
| 1234 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1235 | * <p>The active device selection is per profile. An active device's purpose is |
| 1236 | * profile-specific. For example, in HFP and HSP profiles, it is the device used for phone call |
| 1237 | * audio. If a remote device is not connected, it cannot be selected as active. |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1238 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1239 | * <p>This API returns false in scenarios like the profile on the device is not connected or |
| 1240 | * Bluetooth is not turned on. When this API returns true, it is guaranteed that the {@link |
| 1241 | * #ACTION_ACTIVE_DEVICE_CHANGED} intent will be broadcasted with the active device. |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1242 | * |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1243 | * @param device Remote Bluetooth Device, could be null if phone call audio should not be |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1244 | * streamed to a headset |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1245 | * @return false on immediate error, true otherwise |
| 1246 | * @hide |
| 1247 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 1248 | @RequiresLegacyBluetoothAdminPermission |
| 1249 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1250 | @RequiresPermission( |
| 1251 | allOf = { |
| 1252 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 1253 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 1254 | }) |
| Mathew Inwood | b426f90 | 2021-01-06 12:05:47 +0000 | [diff] [blame] | 1255 | @UnsupportedAppUsage(trackingBug = 171933273) |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1256 | public boolean setActiveDevice(@Nullable BluetoothDevice device) { |
| 1257 | if (DBG) { |
| 1258 | Log.d(TAG, "setActiveDevice: " + device); |
| 1259 | } |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1260 | final IBluetoothHeadset service = getService(); |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1261 | if (service == null) { |
| 1262 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1263 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1264 | } else if (isEnabled() && (device == null || isValidDevice(device))) { |
| 1265 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1266 | return service.setActiveDevice(device, mAttributionSource); |
| 1267 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1268 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 1269 | } |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1270 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1271 | return false; |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * Get the connected device that is active. |
| 1276 | * |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1277 | * @return the connected device that is active or null if no device is active. |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1278 | * @hide |
| 1279 | */ |
| Mathew Inwood | b426f90 | 2021-01-06 12:05:47 +0000 | [diff] [blame] | 1280 | @UnsupportedAppUsage(trackingBug = 171933273) |
| Rahul Sabnis | d979861 | 2019-12-04 14:21:10 -0800 | [diff] [blame] | 1281 | @Nullable |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 1282 | @RequiresLegacyBluetoothPermission |
| 1283 | @RequiresBluetoothConnectPermission |
| 1284 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1285 | public BluetoothDevice getActiveDevice() { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1286 | if (VDBG) Log.d(TAG, "getActiveDevice"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1287 | final IBluetoothHeadset service = getService(); |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1288 | if (service == null) { |
| 1289 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1290 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1291 | } else if (isEnabled()) { |
| 1292 | try { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1293 | return Attributable.setAttributionSource( |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1294 | service.getActiveDevice(mAttributionSource), mAttributionSource); |
| 1295 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1296 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 1297 | } |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1298 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1299 | return null; |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | /** |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1303 | * Check if in-band ringing is currently enabled. In-band ringing could be disabled during an |
| 1304 | * active connection. |
| Jack He | d8d204d | 2016-11-17 16:19:43 -0800 | [diff] [blame] | 1305 | * |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1306 | * @return true if in-band ringing is enabled, false if in-band ringing is disabled |
| 1307 | * @hide |
| 1308 | */ |
| Roopa Sattiraju | ffab995 | 2021-08-20 15:06:57 -0700 | [diff] [blame] | 1309 | @SystemApi |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 1310 | @RequiresLegacyBluetoothPermission |
| 1311 | @RequiresBluetoothConnectPermission |
| David Duarte | ee52b7e | 2023-12-02 01:32:11 +0000 | [diff] [blame] | 1312 | @RequiresPermission( |
| 1313 | allOf = { |
| 1314 | android.Manifest.permission.BLUETOOTH_CONNECT, |
| 1315 | android.Manifest.permission.BLUETOOTH_PRIVILEGED, |
| 1316 | }) |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1317 | public boolean isInbandRingingEnabled() { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1318 | if (DBG) log("isInbandRingingEnabled()"); |
| Etienne Ruffieux | c3bbf1a | 2022-11-07 14:40:27 -0800 | [diff] [blame] | 1319 | final IBluetoothHeadset service = getService(); |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1320 | if (service == null) { |
| 1321 | Log.w(TAG, "Proxy not attached to service"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1322 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 1323 | } else if (isEnabled()) { |
| 1324 | try { |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1325 | return service.isInbandRingingEnabled(mAttributionSource); |
| 1326 | } catch (RemoteException e) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 1327 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| 1328 | } |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1329 | } |
| William Escande | 879a70e | 2024-03-21 21:01:41 -0700 | [diff] [blame] | 1330 | return false; |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| Mathew Inwood | 7d54389 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1333 | @UnsupportedAppUsage |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1334 | private boolean isEnabled() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1335 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
| Jaikumar Ganesh | 2fbe8f4 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 1338 | private boolean isDisabled() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1339 | return mAdapter.getState() == BluetoothAdapter.STATE_OFF; |
| Jaikumar Ganesh | 2fbe8f4 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1342 | private static boolean isValidDevice(BluetoothDevice device) { |
| 1343 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| The Android Open Source Project | 0047a0f | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1346 | private static void log(String msg) { |
| 1347 | Log.d(TAG, msg); |
| 1348 | } |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | } |