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