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