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