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