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