| 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 | b426f90 | 2021-01-06 12:05:47 +0000 | [diff] [blame^] | 116 | @UnsupportedAppUsage(trackingBug = 171933273) |
| 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 | /** |
| 685 | * Start Bluetooth voice recognition. This methods sends the voice |
| 686 | * recognition AT command to the headset and establishes the |
| 687 | * audio connection. |
| 688 | * |
| 689 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 690 | * If this function returns true, this intent will be broadcasted with |
| 691 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_CONNECTING}. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 692 | * |
| Jaikumar Ganesh | 8ea890d | 2010-11-11 10:49:46 -0800 | [diff] [blame] | 693 | * <p> {@link #EXTRA_STATE} will transition from |
| 694 | * {@link #STATE_AUDIO_CONNECTING} to {@link #STATE_AUDIO_CONNECTED} when |
| 695 | * audio connection is established and to {@link #STATE_AUDIO_DISCONNECTED} |
| 696 | * in case of failure to establish the audio connection. |
| 697 | * |
| 698 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 699 | * |
| 700 | * @param device Bluetooth headset |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 701 | * @return false if there is no headset connected, or the connected headset doesn't support |
| 702 | * voice recognition, or voice recognition is already started, or audio channel is occupied, |
| 703 | * or on error, true otherwise |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 704 | */ |
| 705 | public boolean startVoiceRecognition(BluetoothDevice device) { |
| 706 | if (DBG) log("startVoiceRecognition()"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 707 | final IBluetoothHeadset service = mService; |
| 708 | if (service != null && isEnabled() && isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 709 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 710 | return service.startVoiceRecognition(device); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 711 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 712 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 715 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 716 | return false; |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Stop Bluetooth Voice Recognition mode, and shut down the |
| 721 | * Bluetooth audio path. |
| 722 | * |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 723 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
| 724 | * If this function returns true, this intent will be broadcasted with |
| 725 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_DISCONNECTED}. |
| 726 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 727 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 728 | * |
| 729 | * @param device Bluetooth headset |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 730 | * @return false if there is no headset connected, or voice recognition has not started, |
| 731 | * 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] | 732 | */ |
| 733 | public boolean stopVoiceRecognition(BluetoothDevice device) { |
| 734 | if (DBG) log("stopVoiceRecognition()"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 735 | final IBluetoothHeadset service = mService; |
| 736 | if (service != null && isEnabled() && isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 737 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 738 | return service.stopVoiceRecognition(device); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 739 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 740 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 741 | } |
| 742 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 743 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 744 | return false; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Check if Bluetooth SCO audio is connected. |
| 749 | * |
| Jaikumar Ganesh | b342757 | 2011-01-25 16:03:13 -0800 | [diff] [blame] | 750 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 751 | * |
| 752 | * @param device Bluetooth headset |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 753 | * @return true if SCO is connected, false otherwise or on error |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 754 | */ |
| 755 | public boolean isAudioConnected(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 756 | if (VDBG) log("isAudioConnected()"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 757 | final IBluetoothHeadset service = mService; |
| 758 | if (service != null && isEnabled() && isValidDevice(device)) { |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 759 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 760 | return service.isAudioConnected(device); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 761 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 762 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 765 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 766 | return false; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /** |
| Eric Laurent | 2e66fa2 | 2010-03-17 14:59:27 -0700 | [diff] [blame] | 770 | * Indicates if current platform supports voice dialing over bluetooth SCO. |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 771 | * |
| Eric Laurent | 2e66fa2 | 2010-03-17 14:59:27 -0700 | [diff] [blame] | 772 | * @return true if voice dialing over bluetooth is supported, false otherwise. |
| 773 | * @hide |
| 774 | */ |
| 775 | public static boolean isBluetoothVoiceDialingEnabled(Context context) { |
| 776 | return context.getResources().getBoolean( |
| 777 | com.android.internal.R.bool.config_bluetooth_sco_off_call); |
| 778 | } |
| 779 | |
| Jaikumar Ganesh | b84bbd9 | 2010-06-02 14:36:14 -0700 | [diff] [blame] | 780 | /** |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 781 | * Get the current audio state of the Headset. |
| 782 | * Note: This is an internal function and shouldn't be exposed |
| 783 | * |
| 784 | * @hide |
| 785 | */ |
| Mathew Inwood | 049f0f5 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 786 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 787 | public int getAudioState(BluetoothDevice device) { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 788 | if (VDBG) log("getAudioState"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 789 | final IBluetoothHeadset service = mService; |
| 790 | if (service != null && !isDisabled()) { |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 791 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 792 | return service.getAudioState(device); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 793 | } catch (RemoteException e) { |
| 794 | Log.e(TAG, e.toString()); |
| 795 | } |
| Jaikumar Ganesh | 23501e2 | 2010-11-01 11:59:57 -0700 | [diff] [blame] | 796 | } else { |
| 797 | Log.w(TAG, "Proxy not attached to service"); |
| 798 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 799 | } |
| 800 | return BluetoothHeadset.STATE_AUDIO_DISCONNECTED; |
| 801 | } |
| 802 | |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 803 | /** |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 804 | * Sets whether audio routing is allowed. When set to {@code false}, the AG will not route any |
| 805 | * audio to the HF unless explicitly told to. |
| 806 | * This method should be used in cases where the SCO channel is shared between multiple profiles |
| 807 | * and must be delegated by a source knowledgeable |
| 808 | * Note: This is an internal function and shouldn't be exposed |
| 809 | * |
| 810 | * @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] | 811 | * @hide |
| 812 | */ |
| 813 | public void setAudioRouteAllowed(boolean allowed) { |
| 814 | if (VDBG) log("setAudioRouteAllowed"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 815 | final IBluetoothHeadset service = mService; |
| 816 | if (service != null && isEnabled()) { |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 817 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 818 | service.setAudioRouteAllowed(allowed); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 819 | } catch (RemoteException e) { |
| 820 | Log.e(TAG, e.toString()); |
| 821 | } |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 822 | } else { |
| 823 | Log.w(TAG, "Proxy not attached to service"); |
| 824 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Returns whether audio routing is allowed. see {@link #setAudioRouteAllowed(boolean)}. |
| 830 | * Note: This is an internal function and shouldn't be exposed |
| 831 | * |
| 832 | * @hide |
| 833 | */ |
| 834 | public boolean getAudioRouteAllowed() { |
| 835 | if (VDBG) log("getAudioRouteAllowed"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 836 | final IBluetoothHeadset service = mService; |
| 837 | if (service != null && isEnabled()) { |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 838 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 839 | return service.getAudioRouteAllowed(); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 840 | } catch (RemoteException e) { |
| 841 | Log.e(TAG, e.toString()); |
| 842 | } |
| Bryce Lee | 0e154a3 | 2015-11-16 08:55:52 -0800 | [diff] [blame] | 843 | } else { |
| 844 | Log.w(TAG, "Proxy not attached to service"); |
| 845 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 846 | } |
| 847 | return false; |
| 848 | } |
| 849 | |
| 850 | /** |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 851 | * Force SCO audio to be opened regardless any other restrictions |
| 852 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 853 | * @param forced Whether or not SCO audio connection should be forced: True to force SCO audio |
| 854 | * False to use SCO audio in normal manner |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 855 | * @hide |
| 856 | */ |
| 857 | public void setForceScoAudio(boolean forced) { |
| 858 | if (VDBG) log("setForceScoAudio " + String.valueOf(forced)); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 859 | final IBluetoothHeadset service = mService; |
| 860 | if (service != null && isEnabled()) { |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 861 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 862 | service.setForceScoAudio(forced); |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 863 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 864 | Log.e(TAG, e.toString()); |
| Jack He | 798d728 | 2017-05-09 17:16:01 -0700 | [diff] [blame] | 865 | } |
| 866 | } else { |
| 867 | Log.w(TAG, "Proxy not attached to service"); |
| 868 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /** |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 873 | * 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] | 874 | * |
| 875 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 876 | * |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 877 | * @return true if at least one device's SCO audio is connected or connecting, false otherwise |
| 878 | * or on error |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 879 | * @hide |
| 880 | */ |
| 881 | public boolean isAudioOn() { |
| Matthew Xie | f8035a7 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 882 | if (VDBG) log("isAudioOn()"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 883 | final IBluetoothHeadset service = mService; |
| 884 | if (service != null && isEnabled()) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 885 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 886 | return service.isAudioOn(); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 887 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 888 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 889 | } |
| 890 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 891 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 892 | return false; |
| 893 | |
| 894 | } |
| 895 | |
| 896 | /** |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 897 | * Initiates a connection of headset audio to the current active device |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 898 | * |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 899 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
| 900 | * If this function returns true, this intent will be broadcasted with |
| 901 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_CONNECTING}. |
| 902 | * |
| 903 | * <p> {@link #EXTRA_STATE} will transition from |
| 904 | * {@link #STATE_AUDIO_CONNECTING} to {@link #STATE_AUDIO_CONNECTED} when |
| 905 | * audio connection is established and to {@link #STATE_AUDIO_DISCONNECTED} |
| 906 | * in case of failure to establish the audio connection. |
| 907 | * |
| 908 | * Note that this intent will not be sent if {@link BluetoothHeadset#isAudioOn()} is true |
| 909 | * before calling this method |
| 910 | * |
| 911 | * @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] | 912 | * @hide |
| 913 | */ |
| Mathew Inwood | 7d54389 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 914 | @UnsupportedAppUsage |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 915 | public boolean connectAudio() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 916 | final IBluetoothHeadset service = mService; |
| 917 | if (service != null && isEnabled()) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 918 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 919 | return service.connectAudio(); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 920 | } catch (RemoteException e) { |
| 921 | Log.e(TAG, e.toString()); |
| 922 | } |
| 923 | } else { |
| 924 | Log.w(TAG, "Proxy not attached to service"); |
| 925 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 926 | } |
| 927 | return false; |
| 928 | } |
| 929 | |
| 930 | /** |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 931 | * Initiates a disconnection of HFP SCO audio. |
| 932 | * Tear down voice recognition or virtual voice call if any. |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 933 | * |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 934 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
| 935 | * If this function returns true, this intent will be broadcasted with |
| 936 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_DISCONNECTED}. |
| 937 | * |
| 938 | * @return false if audio is not connected, or on error, true otherwise |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 939 | * @hide |
| 940 | */ |
| Mathew Inwood | 7d54389 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 941 | @UnsupportedAppUsage |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 942 | public boolean disconnectAudio() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 943 | final IBluetoothHeadset service = mService; |
| 944 | if (service != null && isEnabled()) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 945 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 946 | return service.disconnectAudio(); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 947 | } catch (RemoteException e) { |
| 948 | Log.e(TAG, e.toString()); |
| 949 | } |
| 950 | } else { |
| 951 | Log.w(TAG, "Proxy not attached to service"); |
| 952 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 953 | } |
| 954 | return false; |
| 955 | } |
| 956 | |
| 957 | /** |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 958 | * Initiates a SCO channel connection as a virtual voice call to the current active device |
| 959 | * Active handsfree device will be notified of incoming call and connected call. |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 960 | * |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 961 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
| 962 | * If this function returns true, this intent will be broadcasted with |
| 963 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_CONNECTING}. |
| 964 | * |
| 965 | * <p> {@link #EXTRA_STATE} will transition from |
| 966 | * {@link #STATE_AUDIO_CONNECTING} to {@link #STATE_AUDIO_CONNECTED} when |
| 967 | * audio connection is established and to {@link #STATE_AUDIO_DISCONNECTED} |
| 968 | * in case of failure to establish the audio connection. |
| 969 | * |
| 970 | * @return true if successful, false if one of the following case applies |
| 971 | * - SCO audio is not idle (connecting or connected) |
| 972 | * - virtual call has already started |
| 973 | * - there is no active device |
| 974 | * - a Telecom managed call is going on |
| 975 | * - binder is dead or Bluetooth is disabled or other error |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 976 | * @hide |
| 977 | */ |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 978 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| Mathew Inwood | 7d54389 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 979 | @UnsupportedAppUsage |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 980 | public boolean startScoUsingVirtualVoiceCall() { |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 981 | if (DBG) log("startScoUsingVirtualVoiceCall()"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 982 | final IBluetoothHeadset service = mService; |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 983 | if (service != null && isEnabled()) { |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 984 | try { |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 985 | return service.startScoUsingVirtualVoiceCall(); |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 986 | } catch (RemoteException e) { |
| 987 | Log.e(TAG, e.toString()); |
| 988 | } |
| 989 | } else { |
| 990 | Log.w(TAG, "Proxy not attached to service"); |
| 991 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 992 | } |
| 993 | return false; |
| 994 | } |
| 995 | |
| 996 | /** |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 997 | * Terminates an ongoing SCO connection and the associated virtual call. |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 998 | * |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 999 | * <p> Users can listen to {@link #ACTION_AUDIO_STATE_CHANGED}. |
| 1000 | * If this function returns true, this intent will be broadcasted with |
| 1001 | * {@link #EXTRA_STATE} set to {@link #STATE_AUDIO_DISCONNECTED}. |
| 1002 | * |
| 1003 | * @return true if successful, false if one of the following case applies |
| 1004 | * - virtual voice call is not started or has ended |
| 1005 | * - binder is dead or Bluetooth is disabled or other error |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1006 | * @hide |
| 1007 | */ |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1008 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| Mathew Inwood | 7d54389 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1009 | @UnsupportedAppUsage |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1010 | public boolean stopScoUsingVirtualVoiceCall() { |
| Jaikumar Ganesh | eea6d26 | 2011-01-24 13:55:27 -0800 | [diff] [blame] | 1011 | if (DBG) log("stopScoUsingVirtualVoiceCall()"); |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1012 | final IBluetoothHeadset service = mService; |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1013 | if (service != null && isEnabled()) { |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1014 | try { |
| Jack He | c46a01e | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 1015 | return service.stopScoUsingVirtualVoiceCall(); |
| Jaikumar Ganesh | 7af32b4 | 2010-10-26 17:10:09 -0700 | [diff] [blame] | 1016 | } catch (RemoteException e) { |
| 1017 | Log.e(TAG, e.toString()); |
| 1018 | } |
| 1019 | } else { |
| 1020 | Log.w(TAG, "Proxy not attached to service"); |
| 1021 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1022 | } |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1026 | /** |
| 1027 | * Notify Headset of phone state change. |
| 1028 | * This is a backdoor for phone app to call BluetoothHeadset since |
| 1029 | * there is currently not a good way to get precise call state change outside |
| 1030 | * of phone app. |
| 1031 | * |
| 1032 | * @hide |
| 1033 | */ |
| Mathew Inwood | 049f0f5 | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1034 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1035 | public void phoneStateChanged(int numActive, int numHeld, int callState, String number, |
| Benson Li | ed8d339 | 2018-07-17 18:19:59 +0800 | [diff] [blame] | 1036 | int type, String name) { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1037 | final IBluetoothHeadset service = mService; |
| 1038 | if (service != null && isEnabled()) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1039 | try { |
| Benson Li | ed8d339 | 2018-07-17 18:19:59 +0800 | [diff] [blame] | 1040 | service.phoneStateChanged(numActive, numHeld, callState, number, type, name); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1041 | } catch (RemoteException e) { |
| 1042 | Log.e(TAG, e.toString()); |
| 1043 | } |
| 1044 | } else { |
| 1045 | Log.w(TAG, "Proxy not attached to service"); |
| 1046 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1051 | * Send Headset of CLCC response |
| 1052 | * |
| 1053 | * @hide |
| 1054 | */ |
| 1055 | 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] | 1056 | String number, int type) { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1057 | final IBluetoothHeadset service = mService; |
| 1058 | if (service != null && isEnabled()) { |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1059 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1060 | service.clccResponse(index, direction, status, mode, mpty, number, type); |
| Matthew Xie | f3ee351 | 2012-02-16 16:57:18 -0800 | [diff] [blame] | 1061 | } catch (RemoteException e) { |
| 1062 | Log.e(TAG, e.toString()); |
| 1063 | } |
| 1064 | } else { |
| 1065 | Log.w(TAG, "Proxy not attached to service"); |
| 1066 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1070 | /** |
| 1071 | * Sends a vendor-specific unsolicited result code to the headset. |
| 1072 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1073 | * <p>The actual string to be sent is <code>command + ": " + arg</code>. For example, if {@code |
| 1074 | * command} is {@link #VENDOR_RESULT_CODE_COMMAND_ANDROID} and {@code arg} is {@code "0"}, the |
| 1075 | * string <code>"+ANDROID: 0"</code> will be sent. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1076 | * |
| Ying Wang | 29d9389 | 2013-08-26 17:48:22 -0700 | [diff] [blame] | 1077 | * <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] | 1078 | * |
| 1079 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 1080 | * |
| 1081 | * @param device Bluetooth headset. |
| 1082 | * @param command A vendor-specific command. |
| 1083 | * @param arg The argument that will be attached to the command. |
| 1084 | * @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] | 1085 | * vendor-specific unsolicited result code, or on error. {@code true} otherwise. |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1086 | * @throws IllegalArgumentException if {@code command} is {@code null}. |
| 1087 | */ |
| 1088 | public boolean sendVendorSpecificResultCode(BluetoothDevice device, String command, |
| 1089 | String arg) { |
| 1090 | if (DBG) { |
| 1091 | log("sendVendorSpecificResultCode()"); |
| 1092 | } |
| 1093 | if (command == null) { |
| 1094 | throw new IllegalArgumentException("command is null"); |
| 1095 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1096 | final IBluetoothHeadset service = mService; |
| 1097 | if (service != null && isEnabled() && isValidDevice(device)) { |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1098 | try { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1099 | return service.sendVendorSpecificResultCode(device, command, arg); |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1100 | } catch (RemoteException e) { |
| 1101 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 1102 | } |
| 1103 | } |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1104 | if (service == null) { |
| Edward Jee | 7c81f1f | 2013-08-16 04:07:49 -0700 | [diff] [blame] | 1105 | Log.w(TAG, "Proxy not attached to service"); |
| 1106 | } |
| 1107 | return false; |
| 1108 | } |
| 1109 | |
| Mudumba Ananth | 80bf628 | 2014-04-27 13:11:00 -0700 | [diff] [blame] | 1110 | /** |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1111 | * Select a connected device as active. |
| 1112 | * |
| 1113 | * The active device selection is per profile. An active device's |
| 1114 | * purpose is profile-specific. For example, in HFP and HSP profiles, |
| 1115 | * it is the device used for phone call audio. If a remote device is not |
| 1116 | * connected, it cannot be selected as active. |
| 1117 | * |
| 1118 | * <p> This API returns false in scenarios like the profile on the |
| 1119 | * device is not connected or Bluetooth is not turned on. |
| 1120 | * When this API returns true, it is guaranteed that the |
| 1121 | * {@link #ACTION_ACTIVE_DEVICE_CHANGED} intent will be broadcasted |
| 1122 | * with the active device. |
| 1123 | * |
| 1124 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 1125 | * permission. |
| 1126 | * |
| 1127 | * @param device Remote Bluetooth Device, could be null if phone call audio should not be |
| 1128 | * streamed to a headset |
| 1129 | * @return false on immediate error, true otherwise |
| 1130 | * @hide |
| 1131 | */ |
| 1132 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADMIN) |
| Mathew Inwood | b426f90 | 2021-01-06 12:05:47 +0000 | [diff] [blame^] | 1133 | @UnsupportedAppUsage(trackingBug = 171933273) |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1134 | public boolean setActiveDevice(@Nullable BluetoothDevice device) { |
| 1135 | if (DBG) { |
| 1136 | Log.d(TAG, "setActiveDevice: " + device); |
| 1137 | } |
| 1138 | final IBluetoothHeadset service = mService; |
| 1139 | if (service != null && isEnabled() && (device == null || isValidDevice(device))) { |
| 1140 | try { |
| 1141 | return service.setActiveDevice(device); |
| 1142 | } catch (RemoteException e) { |
| 1143 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 1144 | } |
| 1145 | } |
| 1146 | if (service == null) { |
| 1147 | Log.w(TAG, "Proxy not attached to service"); |
| 1148 | } |
| 1149 | return false; |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * Get the connected device that is active. |
| 1154 | * |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1155 | * @return the connected device that is active or null if no device |
| 1156 | * is active. |
| 1157 | * @hide |
| 1158 | */ |
| Mathew Inwood | b426f90 | 2021-01-06 12:05:47 +0000 | [diff] [blame^] | 1159 | @UnsupportedAppUsage(trackingBug = 171933273) |
| Rahul Sabnis | d979861 | 2019-12-04 14:21:10 -0800 | [diff] [blame] | 1160 | @Nullable |
| Rahul Sabnis | c228ce2 | 2020-03-18 17:46:33 -0700 | [diff] [blame] | 1161 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| Jack He | 889d234 | 2018-01-03 12:13:26 -0800 | [diff] [blame] | 1162 | public BluetoothDevice getActiveDevice() { |
| 1163 | if (VDBG) { |
| 1164 | Log.d(TAG, "getActiveDevice"); |
| 1165 | } |
| 1166 | final IBluetoothHeadset service = mService; |
| 1167 | if (service != null && isEnabled()) { |
| 1168 | try { |
| 1169 | return service.getActiveDevice(); |
| 1170 | } catch (RemoteException e) { |
| 1171 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 1172 | } |
| 1173 | } |
| 1174 | if (service == null) { |
| 1175 | Log.w(TAG, "Proxy not attached to service"); |
| 1176 | } |
| 1177 | return null; |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1181 | * Check if in-band ringing is currently enabled. In-band ringing could be disabled during an |
| 1182 | * active connection. |
| Jack He | d8d204d | 2016-11-17 16:19:43 -0800 | [diff] [blame] | 1183 | * |
| Jack He | c5fde73 | 2018-01-05 17:17:06 -0800 | [diff] [blame] | 1184 | * @return true if in-band ringing is enabled, false if in-band ringing is disabled |
| 1185 | * @hide |
| 1186 | */ |
| 1187 | @RequiresPermission(android.Manifest.permission.BLUETOOTH) |
| 1188 | public boolean isInbandRingingEnabled() { |
| 1189 | if (DBG) { |
| 1190 | log("isInbandRingingEnabled()"); |
| 1191 | } |
| 1192 | final IBluetoothHeadset service = mService; |
| 1193 | if (service != null && isEnabled()) { |
| 1194 | try { |
| 1195 | return service.isInbandRingingEnabled(); |
| 1196 | } catch (RemoteException e) { |
| 1197 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 1198 | } |
| 1199 | } |
| 1200 | if (service == null) { |
| 1201 | Log.w(TAG, "Proxy not attached to service"); |
| 1202 | } |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * Check if in-band ringing is supported for this platform. |
| 1208 | * |
| 1209 | * @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] | 1210 | * @hide |
| 1211 | */ |
| 1212 | public static boolean isInbandRingingSupported(Context context) { |
| 1213 | return context.getResources().getBoolean( |
| 1214 | com.android.internal.R.bool.config_bluetooth_hfp_inband_ringing_support); |
| 1215 | } |
| 1216 | |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 1217 | private final IBluetoothProfileServiceConnection mConnection = |
| 1218 | new IBluetoothProfileServiceConnection.Stub() { |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1219 | @Override |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 1221 | if (DBG) Log.d(TAG, "Proxy object connected"); |
| Jeff Sharkey | 73458a8 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 1222 | mService = IBluetoothHeadset.Stub.asInterface(Binder.allowBlocking(service)); |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1223 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1224 | MESSAGE_HEADSET_SERVICE_CONNECTED)); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | } |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1226 | |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1227 | @Override |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | public void onServiceDisconnected(ComponentName className) { |
| 1229 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| Ugo Yu | 1652b94 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 1230 | doUnbind(); |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1231 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1232 | MESSAGE_HEADSET_SERVICE_DISCONNECTED)); |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | } |
| 1234 | }; |
| The Android Open Source Project | 0047a0f | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1235 | |
| Mathew Inwood | 7d54389 | 2018-08-01 15:07:20 +0100 | [diff] [blame] | 1236 | @UnsupportedAppUsage |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1237 | private boolean isEnabled() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1238 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| Jaikumar Ganesh | 2fbe8f4 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 1241 | private boolean isDisabled() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1242 | return mAdapter.getState() == BluetoothAdapter.STATE_OFF; |
| Jaikumar Ganesh | 2fbe8f4 | 2011-04-06 11:09:30 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1245 | private static boolean isValidDevice(BluetoothDevice device) { |
| 1246 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
| Jaikumar Ganesh | 2af0776 | 2010-08-24 17:36:13 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| The Android Open Source Project | 0047a0f | 2009-03-05 20:00:43 -0800 | [diff] [blame] | 1249 | private static void log(String msg) { |
| 1250 | Log.d(TAG, msg); |
| 1251 | } |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1252 | |
| 1253 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 1254 | @Override |
| 1255 | public void handleMessage(Message msg) { |
| 1256 | switch (msg.what) { |
| 1257 | case MESSAGE_HEADSET_SERVICE_CONNECTED: { |
| 1258 | if (mServiceListener != null) { |
| 1259 | mServiceListener.onServiceConnected(BluetoothProfile.HEADSET, |
| 1260 | BluetoothHeadset.this); |
| 1261 | } |
| 1262 | break; |
| 1263 | } |
| 1264 | case MESSAGE_HEADSET_SERVICE_DISCONNECTED: { |
| 1265 | if (mServiceListener != null) { |
| 1266 | mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET); |
| 1267 | } |
| Benjamin Franz | 3362fef | 2014-11-12 15:57:54 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | }; |
| The Android Open Source Project | 3389776 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | } |