Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.ServiceConnection; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 24 | import android.os.Binder; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 25 | import android.os.Bundle; |
| 26 | import android.os.IBinder; |
| 27 | import android.os.RemoteException; |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Public API to control Hands Free Profile (HFP role only). |
| 36 | * <p> |
| 37 | * This class defines methods that shall be used by application to manage profile |
| 38 | * connection, calls states and calls actions. |
| 39 | * <p> |
| 40 | * |
| 41 | * @hide |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 42 | */ |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 43 | public final class BluetoothHeadsetClient implements BluetoothProfile { |
| 44 | private static final String TAG = "BluetoothHeadsetClient"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 45 | private static final boolean DBG = true; |
| 46 | private static final boolean VDBG = false; |
| 47 | |
| 48 | /** |
| 49 | * Intent sent whenever connection to remote changes. |
| 50 | * |
| 51 | * <p>It includes two extras: |
| 52 | * <code>BluetoothProfile.EXTRA_PREVIOUS_STATE</code> |
| 53 | * and <code>BluetoothProfile.EXTRA_STATE</code>, which |
| 54 | * are mandatory. |
| 55 | * <p>There are also non mandatory feature extras: |
| 56 | * {@link #EXTRA_AG_FEATURE_3WAY_CALLING}, |
| 57 | * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION}, |
| 58 | * {@link #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}, |
| 59 | * {@link #EXTRA_AG_FEATURE_REJECT_CALL}, |
| 60 | * {@link #EXTRA_AG_FEATURE_ECC}, |
| 61 | * {@link #EXTRA_AG_FEATURE_RESPONSE_AND_HOLD}, |
| 62 | * {@link #EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL}, |
| 63 | * {@link #EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL}, |
| 64 | * {@link #EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT}, |
| 65 | * {@link #EXTRA_AG_FEATURE_MERGE}, |
| 66 | * {@link #EXTRA_AG_FEATURE_MERGE_AND_DETACH}, |
| 67 | * sent as boolean values only when <code>EXTRA_STATE</code> |
| 68 | * is set to <code>STATE_CONNECTED</code>.</p> |
| 69 | * |
| 70 | * <p>Note that features supported by AG are being sent as |
| 71 | * booleans with value <code>true</code>, |
| 72 | * and not supported ones are <strong>not</strong> being sent at all.</p> |
| 73 | */ |
| 74 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 75 | "android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Intent sent whenever audio state changes. |
| 79 | * |
| 80 | * <p>It includes two mandatory extras: |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 81 | * {@link BluetoothProfile#EXTRA_STATE}, |
| 82 | * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE}, |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 83 | * with possible values: |
| 84 | * {@link #STATE_AUDIO_CONNECTING}, |
| 85 | * {@link #STATE_AUDIO_CONNECTED}, |
| 86 | * {@link #STATE_AUDIO_DISCONNECTED}</p> |
| 87 | * <p>When <code>EXTRA_STATE</code> is set |
| 88 | * to </code>STATE_AUDIO_CONNECTED</code>, |
| 89 | * it also includes {@link #EXTRA_AUDIO_WBS} |
| 90 | * indicating wide band speech support.</p> |
| 91 | */ |
| 92 | public static final String ACTION_AUDIO_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 93 | "android.bluetooth.headsetclient.profile.action.AUDIO_STATE_CHANGED"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Intent sending updates of the Audio Gateway state. |
| 97 | * Each extra is being sent only when value it |
| 98 | * represents has been changed recently on AG. |
| 99 | * <p>It can contain one or more of the following extras: |
| 100 | * {@link #EXTRA_NETWORK_STATUS}, |
| 101 | * {@link #EXTRA_NETWORK_SIGNAL_STRENGTH}, |
| 102 | * {@link #EXTRA_NETWORK_ROAMING}, |
| 103 | * {@link #EXTRA_BATTERY_LEVEL}, |
| 104 | * {@link #EXTRA_OPERATOR_NAME}, |
| 105 | * {@link #EXTRA_VOICE_RECOGNITION}, |
| 106 | * {@link #EXTRA_IN_BAND_RING}</p> |
| 107 | */ |
| 108 | public static final String ACTION_AG_EVENT = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 109 | "android.bluetooth.headsetclient.profile.action.AG_EVENT"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Intent sent whenever state of a call changes. |
| 113 | * |
| 114 | * <p>It includes: |
| 115 | * {@link #EXTRA_CALL}, |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 116 | * with value of {@link BluetoothHeadsetClientCall} instance, |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 117 | * representing actual call state.</p> |
| 118 | */ |
| 119 | public static final String ACTION_CALL_CHANGED = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 120 | "android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Intent that notifies about the result of the last issued action. |
| 124 | * Please note that not every action results in explicit action result code being sent. |
| 125 | * Instead other notifications about new Audio Gateway state might be sent, |
| 126 | * like <code>ACTION_AG_EVENT</code> with <code>EXTRA_VOICE_RECOGNITION</code> value |
| 127 | * when for example user started voice recognition from HF unit. |
| 128 | */ |
| 129 | public static final String ACTION_RESULT = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 130 | "android.bluetooth.headsetclient.profile.action.RESULT"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Intent that notifies about the number attached to the last voice tag |
| 134 | * recorded on AG. |
| 135 | * |
| 136 | * <p>It contains: |
| 137 | * {@link #EXTRA_NUMBER}, |
| 138 | * with a <code>String</code> value representing phone number.</p> |
| 139 | */ |
| 140 | public static final String ACTION_LAST_VTAG = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 141 | "android.bluetooth.headsetclient.profile.action.LAST_VTAG"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 142 | |
| 143 | public static final int STATE_AUDIO_DISCONNECTED = 0; |
| 144 | public static final int STATE_AUDIO_CONNECTING = 1; |
| 145 | public static final int STATE_AUDIO_CONNECTED = 2; |
| 146 | |
| 147 | /** |
| 148 | * Extra with information if connected audio is WBS. |
| 149 | * <p>Possible values: <code>true</code>, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 150 | * <code>false</code>.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 151 | */ |
| 152 | public static final String EXTRA_AUDIO_WBS = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 153 | "android.bluetooth.headsetclient.extra.AUDIO_WBS"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Extra for AG_EVENT indicates network status. |
| 157 | * <p>Value: 0 - network unavailable, |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 158 | * 1 - network available </p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 159 | */ |
| 160 | public static final String EXTRA_NETWORK_STATUS = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 161 | "android.bluetooth.headsetclient.extra.NETWORK_STATUS"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 162 | /** |
| 163 | * Extra for AG_EVENT intent indicates network signal strength. |
| 164 | * <p>Value: <code>Integer</code> representing signal strength.</p> |
| 165 | */ |
| 166 | public static final String EXTRA_NETWORK_SIGNAL_STRENGTH = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 167 | "android.bluetooth.headsetclient.extra.NETWORK_SIGNAL_STRENGTH"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 168 | /** |
| 169 | * Extra for AG_EVENT intent indicates roaming state. |
| 170 | * <p>Value: 0 - no roaming |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 171 | * 1 - active roaming</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 172 | */ |
| 173 | public static final String EXTRA_NETWORK_ROAMING = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 174 | "android.bluetooth.headsetclient.extra.NETWORK_ROAMING"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 175 | /** |
| 176 | * Extra for AG_EVENT intent indicates the battery level. |
| 177 | * <p>Value: <code>Integer</code> representing signal strength.</p> |
| 178 | */ |
| 179 | public static final String EXTRA_BATTERY_LEVEL = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 180 | "android.bluetooth.headsetclient.extra.BATTERY_LEVEL"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 181 | /** |
| 182 | * Extra for AG_EVENT intent indicates operator name. |
| 183 | * <p>Value: <code>String</code> representing operator name.</p> |
| 184 | */ |
| 185 | public static final String EXTRA_OPERATOR_NAME = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 186 | "android.bluetooth.headsetclient.extra.OPERATOR_NAME"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 187 | /** |
| 188 | * Extra for AG_EVENT intent indicates voice recognition state. |
| 189 | * <p>Value: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 190 | * 0 - voice recognition stopped, |
| 191 | * 1 - voice recognition started.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 192 | */ |
| 193 | public static final String EXTRA_VOICE_RECOGNITION = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 194 | "android.bluetooth.headsetclient.extra.VOICE_RECOGNITION"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 195 | /** |
| 196 | * Extra for AG_EVENT intent indicates in band ring state. |
| 197 | * <p>Value: |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 198 | * 0 - in band ring tone not supported, or |
| 199 | * 1 - in band ring tone supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 200 | */ |
| 201 | public static final String EXTRA_IN_BAND_RING = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 202 | "android.bluetooth.headsetclient.extra.IN_BAND_RING"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * Extra for AG_EVENT intent indicates subscriber info. |
| 206 | * <p>Value: <code>String</code> containing subscriber information.</p> |
| 207 | */ |
| 208 | public static final String EXTRA_SUBSCRIBER_INFO = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 209 | "android.bluetooth.headsetclient.extra.SUBSCRIBER_INFO"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 210 | |
| 211 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 212 | * Extra for AG_CALL_CHANGED intent indicates the |
| 213 | * {@link BluetoothHeadsetClientCall} object that has changed. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 214 | */ |
| 215 | public static final String EXTRA_CALL = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 216 | "android.bluetooth.headsetclient.extra.CALL"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * Extra for ACTION_LAST_VTAG intent. |
| 220 | * <p>Value: <code>String</code> representing phone number |
| 221 | * corresponding to last voice tag recorded on AG</p> |
| 222 | */ |
| 223 | public static final String EXTRA_NUMBER = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 224 | "android.bluetooth.headsetclient.extra.NUMBER"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Extra for ACTION_RESULT intent that shows the result code of |
| 228 | * last issued action. |
| 229 | * <p>Possible results: |
| 230 | * {@link #ACTION_RESULT_OK}, |
| 231 | * {@link #ACTION_RESULT_ERROR}, |
| 232 | * {@link #ACTION_RESULT_ERROR_NO_CARRIER}, |
| 233 | * {@link #ACTION_RESULT_ERROR_BUSY}, |
| 234 | * {@link #ACTION_RESULT_ERROR_NO_ANSWER}, |
| 235 | * {@link #ACTION_RESULT_ERROR_DELAYED}, |
| 236 | * {@link #ACTION_RESULT_ERROR_BLACKLISTED}, |
| 237 | * {@link #ACTION_RESULT_ERROR_CME}</p> |
| 238 | */ |
| 239 | public static final String EXTRA_RESULT_CODE = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 240 | "android.bluetooth.headsetclient.extra.RESULT_CODE"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Extra for ACTION_RESULT intent that shows the extended result code of |
| 244 | * last issued action. |
| 245 | * <p>Value: <code>Integer</code> - error code.</p> |
| 246 | */ |
| 247 | public static final String EXTRA_CME_CODE = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 248 | "android.bluetooth.headsetclient.extra.CME_CODE"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 249 | |
| 250 | /* Extras for AG_FEATURES, extras type is boolean */ |
| 251 | // TODO verify if all of those are actually useful |
| 252 | /** |
| 253 | * AG feature: three way calling. |
| 254 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 255 | public static final String EXTRA_AG_FEATURE_3WAY_CALLING = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 256 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_3WAY_CALLING"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 257 | /** |
| 258 | * AG feature: voice recognition. |
| 259 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 260 | public static final String EXTRA_AG_FEATURE_VOICE_RECOGNITION = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 261 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_VOICE_RECOGNITION"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 262 | /** |
| 263 | * AG feature: fetching phone number for voice tagging procedure. |
| 264 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 265 | public static final String EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 266 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 267 | /** |
| 268 | * AG feature: ability to reject incoming call. |
| 269 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 270 | public static final String EXTRA_AG_FEATURE_REJECT_CALL = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 271 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_REJECT_CALL"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 272 | /** |
| 273 | * AG feature: enhanced call handling (terminate specific call, private consultation). |
| 274 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 275 | public static final String EXTRA_AG_FEATURE_ECC = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 276 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ECC"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 277 | /** |
| 278 | * AG feature: response and hold. |
| 279 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 280 | public static final String EXTRA_AG_FEATURE_RESPONSE_AND_HOLD = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 281 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RESPONSE_AND_HOLD"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 282 | /** |
| 283 | * AG call handling feature: accept held or waiting call in three way calling scenarios. |
| 284 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 285 | public static final String EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 286 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 287 | /** |
| 288 | * AG call handling feature: release held or waiting call in three way calling scenarios. |
| 289 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 290 | public static final String EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 291 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 292 | /** |
| 293 | * AG call handling feature: release active call and accept held or waiting call in three way |
| 294 | * calling scenarios. |
| 295 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 296 | public static final String EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 297 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 298 | /** |
| 299 | * AG call handling feature: merge two calls, held and active - multi party conference mode. |
| 300 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 301 | public static final String EXTRA_AG_FEATURE_MERGE = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 302 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 303 | /** |
| 304 | * AG call handling feature: merge calls and disconnect from multi party |
| 305 | * conversation leaving peers connected to each other. |
| 306 | * Note that this feature needs to be supported by mobile network operator |
| 307 | * as it requires connection and billing transfer. |
| 308 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 309 | public static final String EXTRA_AG_FEATURE_MERGE_AND_DETACH = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 310 | "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE_AND_DETACH"; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 311 | |
| 312 | /* Action result codes */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 313 | public static final int ACTION_RESULT_OK = 0; |
| 314 | public static final int ACTION_RESULT_ERROR = 1; |
| 315 | public static final int ACTION_RESULT_ERROR_NO_CARRIER = 2; |
| 316 | public static final int ACTION_RESULT_ERROR_BUSY = 3; |
| 317 | public static final int ACTION_RESULT_ERROR_NO_ANSWER = 4; |
| 318 | public static final int ACTION_RESULT_ERROR_DELAYED = 5; |
| 319 | public static final int ACTION_RESULT_ERROR_BLACKLISTED = 6; |
| 320 | public static final int ACTION_RESULT_ERROR_CME = 7; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 321 | |
| 322 | /* Detailed CME error codes */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 323 | public static final int CME_PHONE_FAILURE = 0; |
| 324 | public static final int CME_NO_CONNECTION_TO_PHONE = 1; |
| 325 | public static final int CME_OPERATION_NOT_ALLOWED = 3; |
| 326 | public static final int CME_OPERATION_NOT_SUPPORTED = 4; |
| 327 | public static final int CME_PHSIM_PIN_REQUIRED = 5; |
| 328 | public static final int CME_PHFSIM_PIN_REQUIRED = 6; |
| 329 | public static final int CME_PHFSIM_PUK_REQUIRED = 7; |
| 330 | public static final int CME_SIM_NOT_INSERTED = 10; |
| 331 | public static final int CME_SIM_PIN_REQUIRED = 11; |
| 332 | public static final int CME_SIM_PUK_REQUIRED = 12; |
| 333 | public static final int CME_SIM_FAILURE = 13; |
| 334 | public static final int CME_SIM_BUSY = 14; |
| 335 | public static final int CME_SIM_WRONG = 15; |
| 336 | public static final int CME_INCORRECT_PASSWORD = 16; |
| 337 | public static final int CME_SIM_PIN2_REQUIRED = 17; |
| 338 | public static final int CME_SIM_PUK2_REQUIRED = 18; |
| 339 | public static final int CME_MEMORY_FULL = 20; |
| 340 | public static final int CME_INVALID_INDEX = 21; |
| 341 | public static final int CME_NOT_FOUND = 22; |
| 342 | public static final int CME_MEMORY_FAILURE = 23; |
| 343 | public static final int CME_TEXT_STRING_TOO_LONG = 24; |
| 344 | public static final int CME_INVALID_CHARACTER_IN_TEXT_STRING = 25; |
| 345 | public static final int CME_DIAL_STRING_TOO_LONG = 26; |
| 346 | public static final int CME_INVALID_CHARACTER_IN_DIAL_STRING = 27; |
| 347 | public static final int CME_NO_NETWORK_SERVICE = 30; |
| 348 | public static final int CME_NETWORK_TIMEOUT = 31; |
| 349 | public static final int CME_EMERGENCY_SERVICE_ONLY = 32; |
| 350 | public static final int CME_NO_SIMULTANOUS_VOIP_CS_CALLS = 33; |
| 351 | public static final int CME_NOT_SUPPORTED_FOR_VOIP = 34; |
| 352 | public static final int CME_SIP_RESPONSE_CODE = 35; |
| 353 | public static final int CME_NETWORK_PERSONALIZATION_PIN_REQUIRED = 40; |
| 354 | public static final int CME_NETWORK_PERSONALIZATION_PUK_REQUIRED = 41; |
| 355 | public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PIN_REQUIRED = 42; |
| 356 | public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PUK_REQUIRED = 43; |
| 357 | public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PIN_REQUIRED = 44; |
| 358 | public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PUK_REQUIRED = 45; |
| 359 | public static final int CME_CORPORATE_PERSONALIZATION_PIN_REQUIRED = 46; |
| 360 | public static final int CME_CORPORATE_PERSONALIZATION_PUK_REQUIRED = 47; |
| 361 | public static final int CME_HIDDEN_KEY_REQUIRED = 48; |
| 362 | public static final int CME_EAP_NOT_SUPPORTED = 49; |
| 363 | public static final int CME_INCORRECT_PARAMETERS = 50; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 364 | |
| 365 | /* Action policy for other calls when accepting call */ |
| 366 | public static final int CALL_ACCEPT_NONE = 0; |
| 367 | public static final int CALL_ACCEPT_HOLD = 1; |
| 368 | public static final int CALL_ACCEPT_TERMINATE = 2; |
| 369 | |
| 370 | private Context mContext; |
| 371 | private ServiceListener mServiceListener; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 372 | private volatile IBluetoothHeadsetClient mService; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 373 | private BluetoothAdapter mAdapter; |
| 374 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 375 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 376 | new IBluetoothStateChangeCallback.Stub() { |
| 377 | @Override |
| 378 | public void onBluetoothStateChange(boolean up) { |
| 379 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 380 | if (!up) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 381 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 382 | synchronized (mConnection) { |
| 383 | try { |
| 384 | mService = null; |
| 385 | mContext.unbindService(mConnection); |
| 386 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 387 | Log.e(TAG, "", re); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | } else { |
| 391 | synchronized (mConnection) { |
| 392 | try { |
| 393 | if (mService == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 394 | if (VDBG) Log.d(TAG, "Binding service..."); |
| 395 | Intent intent = new Intent( |
| 396 | IBluetoothHeadsetClient.class.getName()); |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 397 | doBind(); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 398 | } |
| 399 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 400 | Log.e(TAG, "", re); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 405 | }; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 406 | |
| 407 | /** |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 408 | * Create a BluetoothHeadsetClient proxy object. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 409 | */ |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 410 | /*package*/ BluetoothHeadsetClient(Context context, ServiceListener l) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 411 | mContext = context; |
| 412 | mServiceListener = l; |
| 413 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 414 | |
| 415 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 416 | if (mgr != null) { |
| 417 | try { |
| 418 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 419 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 420 | Log.e(TAG, "", e); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 424 | doBind(); |
| 425 | } |
| 426 | |
| 427 | boolean doBind() { |
| 428 | Intent intent = new Intent(IBluetoothHeadsetClient.class.getName()); |
| 429 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 430 | intent.setComponent(comp); |
| 431 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 432 | UserHandle.CURRENT_OR_SELF)) { |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 433 | Log.e(TAG, "Could not bind to Bluetooth Headset Client Service with " + intent); |
| 434 | return false; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 435 | } |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 436 | return true; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Close the connection to the backing service. |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 441 | * Other public functions of BluetoothHeadsetClient will return default error |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 442 | * results once close() has been called. Multiple invocations of close() |
| 443 | * are ok. |
| 444 | */ |
| 445 | /*package*/ void close() { |
| 446 | if (VDBG) log("close()"); |
| 447 | |
| 448 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 449 | if (mgr != null) { |
| 450 | try { |
| 451 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 452 | } catch (Exception e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 453 | Log.e(TAG, "", e); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | synchronized (mConnection) { |
| 458 | if (mService != null) { |
| 459 | try { |
| 460 | mService = null; |
| 461 | mContext.unbindService(mConnection); |
| 462 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 463 | Log.e(TAG, "", re); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | } |
| 467 | mServiceListener = null; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Connects to remote device. |
| 472 | * |
| 473 | * Currently, the system supports only 1 connection. So, in case of the |
| 474 | * second connection, this implementation will disconnect already connected |
| 475 | * device automatically and will process the new one. |
| 476 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 477 | * @param device a remote device we want connect to |
| 478 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 479 | * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 480 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 481 | @UnsupportedAppUsage |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 482 | public boolean connect(BluetoothDevice device) { |
| 483 | if (DBG) log("connect(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 484 | final IBluetoothHeadsetClient service = mService; |
| 485 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 486 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 487 | return service.connect(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 488 | } catch (RemoteException e) { |
| 489 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 490 | return false; |
| 491 | } |
| 492 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 493 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Disconnects remote device |
| 499 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 500 | * @param device a remote device we want disconnect |
| 501 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 502 | * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 503 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 504 | @UnsupportedAppUsage |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 505 | public boolean disconnect(BluetoothDevice device) { |
| 506 | if (DBG) log("disconnect(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 507 | final IBluetoothHeadsetClient service = mService; |
| 508 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 509 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 510 | return service.disconnect(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 511 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 512 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 513 | return false; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 514 | } |
| 515 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 516 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Return the list of connected remote devices |
| 522 | * |
| 523 | * @return list of connected devices; empty list if nothing is connected. |
| 524 | */ |
| 525 | @Override |
| 526 | public List<BluetoothDevice> getConnectedDevices() { |
| 527 | if (VDBG) log("getConnectedDevices()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 528 | final IBluetoothHeadsetClient service = mService; |
| 529 | if (service != null && isEnabled()) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 530 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 531 | return service.getConnectedDevices(); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 532 | } catch (RemoteException e) { |
| 533 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 534 | return new ArrayList<BluetoothDevice>(); |
| 535 | } |
| 536 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 537 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 538 | return new ArrayList<BluetoothDevice>(); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Returns list of remote devices in a particular state |
| 543 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 544 | * @param states collection of states |
| 545 | * @return list of devices that state matches the states listed in <code>states</code>; empty |
| 546 | * list if nothing matches the <code>states</code> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 547 | */ |
| 548 | @Override |
| 549 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 550 | if (VDBG) log("getDevicesMatchingStates()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 551 | final IBluetoothHeadsetClient service = mService; |
| 552 | if (service != null && isEnabled()) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 553 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 554 | return service.getDevicesMatchingConnectionStates(states); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 555 | } catch (RemoteException e) { |
| 556 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 557 | return new ArrayList<BluetoothDevice>(); |
| 558 | } |
| 559 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 560 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 561 | return new ArrayList<BluetoothDevice>(); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Returns state of the <code>device</code> |
| 566 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 567 | * @param device a remote device |
| 568 | * @return the state of connection of the device |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 569 | */ |
| 570 | @Override |
| 571 | public int getConnectionState(BluetoothDevice device) { |
| 572 | if (VDBG) log("getConnectionState(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 573 | final IBluetoothHeadsetClient service = mService; |
| 574 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 575 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 576 | return service.getConnectionState(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 577 | } catch (RemoteException e) { |
| 578 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 579 | return BluetoothProfile.STATE_DISCONNECTED; |
| 580 | } |
| 581 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 582 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 583 | return BluetoothProfile.STATE_DISCONNECTED; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Set priority of the profile |
| 588 | * |
| 589 | * The device should already be paired. |
| 590 | */ |
| 591 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 592 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 593 | final IBluetoothHeadsetClient service = mService; |
| 594 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 595 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 596 | && priority != BluetoothProfile.PRIORITY_ON) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 597 | return false; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 598 | } |
| 599 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 600 | return service.setPriority(device, priority); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 601 | } catch (RemoteException e) { |
| 602 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 603 | return false; |
| 604 | } |
| 605 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 606 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 607 | return false; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Get the priority of the profile. |
| 612 | */ |
| 613 | public int getPriority(BluetoothDevice device) { |
| 614 | if (VDBG) log("getPriority(" + device + ")"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 615 | final IBluetoothHeadsetClient service = mService; |
| 616 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 617 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 618 | return service.getPriority(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 619 | } catch (RemoteException e) { |
| 620 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 621 | return PRIORITY_OFF; |
| 622 | } |
| 623 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 624 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 625 | return PRIORITY_OFF; |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Starts voice recognition. |
| 630 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 631 | * @param device remote device |
| 632 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 633 | * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 634 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 635 | * <p>Feature required for successful execution is being reported by: {@link |
| 636 | * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature |
| 637 | * is not supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 638 | */ |
| 639 | public boolean startVoiceRecognition(BluetoothDevice device) { |
| 640 | if (DBG) log("startVoiceRecognition()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 641 | final IBluetoothHeadsetClient service = mService; |
| 642 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 643 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 644 | return service.startVoiceRecognition(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 645 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 646 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 647 | } |
| 648 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 649 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Stops voice recognition. |
| 655 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 656 | * @param device remote device |
| 657 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 658 | * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 659 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 660 | * <p>Feature required for successful execution is being reported by: {@link |
| 661 | * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature |
| 662 | * is not supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 663 | */ |
| 664 | public boolean stopVoiceRecognition(BluetoothDevice device) { |
| 665 | if (DBG) log("stopVoiceRecognition()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 666 | final IBluetoothHeadsetClient service = mService; |
| 667 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 668 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 669 | return service.stopVoiceRecognition(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 670 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 671 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 672 | } |
| 673 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 674 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 675 | return false; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Returns list of all calls in any state. |
| 680 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 681 | * @param device remote device |
| 682 | * @return list of calls; empty list if none call exists |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 683 | */ |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 684 | public List<BluetoothHeadsetClientCall> getCurrentCalls(BluetoothDevice device) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 685 | if (DBG) log("getCurrentCalls()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 686 | final IBluetoothHeadsetClient service = mService; |
| 687 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 688 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 689 | return service.getCurrentCalls(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 690 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 691 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 692 | } |
| 693 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 694 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 695 | return null; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Returns list of current values of AG indicators. |
| 700 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 701 | * @param device remote device |
| 702 | * @return bundle of AG indicators; null if device is not in CONNECTED state |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 703 | */ |
| 704 | public Bundle getCurrentAgEvents(BluetoothDevice device) { |
| 705 | if (DBG) log("getCurrentCalls()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 706 | final IBluetoothHeadsetClient service = mService; |
| 707 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 708 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 709 | return service.getCurrentAgEvents(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 710 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 711 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 712 | } |
| 713 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 714 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 715 | return null; |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Accepts a call |
| 720 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 721 | * @param device remote device |
| 722 | * @param flag action policy while accepting a call. Possible values {@link #CALL_ACCEPT_NONE}, |
| 723 | * {@link #CALL_ACCEPT_HOLD}, {@link #CALL_ACCEPT_TERMINATE} |
| 724 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 725 | * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 726 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 727 | @UnsupportedAppUsage |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 728 | public boolean acceptCall(BluetoothDevice device, int flag) { |
| 729 | if (DBG) log("acceptCall()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 730 | final IBluetoothHeadsetClient service = mService; |
| 731 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 732 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 733 | return service.acceptCall(device, flag); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 734 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 735 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 736 | } |
| 737 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 738 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 739 | return false; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Holds a call. |
| 744 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 745 | * @param device remote device |
| 746 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 747 | * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 748 | */ |
| 749 | public boolean holdCall(BluetoothDevice device) { |
| 750 | if (DBG) log("holdCall()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 751 | final IBluetoothHeadsetClient service = mService; |
| 752 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 753 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 754 | return service.holdCall(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 755 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 756 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 757 | } |
| 758 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 759 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 760 | return false; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Rejects a call. |
| 765 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 766 | * @param device remote device |
| 767 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 768 | * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 769 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 770 | * <p>Feature required for successful execution is being reported by: {@link |
| 771 | * #EXTRA_AG_FEATURE_REJECT_CALL}. This method invocation will fail silently when feature is not |
| 772 | * supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 773 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 774 | @UnsupportedAppUsage |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 775 | public boolean rejectCall(BluetoothDevice device) { |
| 776 | if (DBG) log("rejectCall()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 777 | final IBluetoothHeadsetClient service = mService; |
| 778 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 779 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 780 | return service.rejectCall(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 781 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 782 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 783 | } |
| 784 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 785 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 786 | return false; |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Terminates a specified call. |
| 791 | * |
| 792 | * Works only when Extended Call Control is supported by Audio Gateway. |
| 793 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 794 | * @param device remote device |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 795 | * @param call Handle of call obtained in {@link #dial(BluetoothDevice, String)} or obtained via |
| 796 | * {@link #ACTION_CALL_CHANGED}. {@code call} may be null in which case we will hangup all active |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 797 | * calls. |
| 798 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 799 | * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 800 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 801 | * <p>Feature required for successful execution is being reported by: {@link |
| 802 | * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not |
| 803 | * supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 804 | */ |
Sanket Agarwal | 40bb6f3 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 805 | public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 806 | if (DBG) log("terminateCall()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 807 | final IBluetoothHeadsetClient service = mService; |
| 808 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 809 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 810 | return service.terminateCall(device, call); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 811 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 812 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 813 | } |
| 814 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 815 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 816 | return false; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Enters private mode with a specified call. |
| 821 | * |
| 822 | * Works only when Extended Call Control is supported by Audio Gateway. |
| 823 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 824 | * @param device remote device |
| 825 | * @param index index of the call to connect in private mode |
| 826 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 827 | * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 828 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 829 | * <p>Feature required for successful execution is being reported by: {@link |
| 830 | * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not |
| 831 | * supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 832 | */ |
| 833 | public boolean enterPrivateMode(BluetoothDevice device, int index) { |
| 834 | if (DBG) log("enterPrivateMode()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 835 | final IBluetoothHeadsetClient service = mService; |
| 836 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 837 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 838 | return service.enterPrivateMode(device, index); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 839 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 840 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 841 | } |
| 842 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 843 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 844 | return false; |
| 845 | } |
| 846 | |
| 847 | /** |
| 848 | * Performs explicit call transfer. |
| 849 | * |
| 850 | * That means connect other calls and disconnect. |
| 851 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 852 | * @param device remote device |
| 853 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 854 | * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 855 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 856 | * <p>Feature required for successful execution is being reported by: {@link |
| 857 | * #EXTRA_AG_FEATURE_MERGE_AND_DETACH}. This method invocation will fail silently when feature |
| 858 | * is not supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 859 | */ |
| 860 | public boolean explicitCallTransfer(BluetoothDevice device) { |
| 861 | if (DBG) log("explicitCallTransfer()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 862 | final IBluetoothHeadsetClient service = mService; |
| 863 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 864 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 865 | return service.explicitCallTransfer(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 866 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 867 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 868 | } |
| 869 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 870 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 871 | return false; |
| 872 | } |
| 873 | |
| 874 | /** |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 875 | * Places a call with specified number. |
| 876 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 877 | * @param device remote device |
| 878 | * @param number valid phone number |
| 879 | * @return <code>{@link BluetoothHeadsetClientCall} call</code> if command has been issued |
| 880 | * successfully; <code>{@link null}</code> otherwise; upon completion HFP sends {@link |
| 881 | * #ACTION_CALL_CHANGED} intent in case of success; {@link #ACTION_RESULT} is sent otherwise; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 882 | */ |
Sanket Agarwal | 40bb6f3 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 883 | public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 884 | if (DBG) log("dial()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 885 | final IBluetoothHeadsetClient service = mService; |
| 886 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 887 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 888 | return service.dial(device, number); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 889 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 890 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 891 | } |
| 892 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 893 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Sanket Agarwal | 40bb6f3 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 894 | return null; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Sends DTMF code. |
| 899 | * |
| 900 | * Possible code values : 0,1,2,3,4,5,6,7,8,9,A,B,C,D,*,# |
| 901 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 902 | * @param device remote device |
| 903 | * @param code ASCII code |
| 904 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 905 | * otherwise; upon completion HFP sends {@link #ACTION_RESULT} intent; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 906 | */ |
| 907 | public boolean sendDTMF(BluetoothDevice device, byte code) { |
| 908 | if (DBG) log("sendDTMF()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 909 | final IBluetoothHeadsetClient service = mService; |
| 910 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 911 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 912 | return service.sendDTMF(device, code); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 913 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 914 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 915 | } |
| 916 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 917 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 918 | return false; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Get a number corresponding to last voice tag recorded on AG. |
| 923 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 924 | * @param device remote device |
| 925 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 926 | * otherwise; upon completion HFP sends {@link #ACTION_LAST_VTAG} or {@link #ACTION_RESULT} |
| 927 | * intent; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 928 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 929 | * <p>Feature required for successful execution is being reported by: {@link |
| 930 | * #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}. This method invocation will fail silently when |
| 931 | * feature is not supported.</p> |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 932 | */ |
| 933 | public boolean getLastVoiceTagNumber(BluetoothDevice device) { |
| 934 | if (DBG) log("getLastVoiceTagNumber()"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 935 | final IBluetoothHeadsetClient service = mService; |
| 936 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 937 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 938 | return service.getLastVoiceTagNumber(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 939 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 940 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 941 | } |
| 942 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 943 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | |
| 947 | /** |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 948 | * Returns current audio state of Audio Gateway. |
| 949 | * |
| 950 | * Note: This is an internal function and shouldn't be exposed |
| 951 | */ |
Mathew Inwood | 7acad5e | 2018-08-01 15:00:35 +0100 | [diff] [blame] | 952 | @UnsupportedAppUsage |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 953 | public int getAudioState(BluetoothDevice device) { |
| 954 | if (VDBG) log("getAudioState"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 955 | final IBluetoothHeadsetClient service = mService; |
| 956 | if (service != null && isEnabled()) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 957 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 958 | return service.getAudioState(device); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 959 | } catch (RemoteException e) { |
| 960 | Log.e(TAG, e.toString()); |
| 961 | } |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 962 | } else { |
| 963 | Log.w(TAG, "Proxy not attached to service"); |
| 964 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 965 | } |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 966 | return BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /** |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 970 | * Sets whether audio routing is allowed. |
| 971 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 972 | * @param device remote device |
| 973 | * @param allowed if routing is allowed to the device Note: This is an internal function and |
| 974 | * shouldn't be exposed |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 975 | */ |
Sanket Agarwal | 039eeb8 | 2017-01-20 14:55:15 -0800 | [diff] [blame] | 976 | public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) { |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 977 | if (VDBG) log("setAudioRouteAllowed"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 978 | final IBluetoothHeadsetClient service = mService; |
| 979 | if (service != null && isEnabled()) { |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 980 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 981 | service.setAudioRouteAllowed(device, allowed); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 982 | } catch (RemoteException e) { |
| 983 | Log.e(TAG, e.toString()); |
| 984 | } |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 985 | } else { |
| 986 | Log.w(TAG, "Proxy not attached to service"); |
| 987 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * Returns whether audio routing is allowed. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 993 | * |
| 994 | * @param device remote device |
| 995 | * @return whether the command succeeded Note: This is an internal function and shouldn't be |
| 996 | * exposed |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 997 | */ |
Sanket Agarwal | 039eeb8 | 2017-01-20 14:55:15 -0800 | [diff] [blame] | 998 | public boolean getAudioRouteAllowed(BluetoothDevice device) { |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 999 | if (VDBG) log("getAudioRouteAllowed"); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1000 | final IBluetoothHeadsetClient service = mService; |
| 1001 | if (service != null && isEnabled()) { |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 1002 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1003 | return service.getAudioRouteAllowed(device); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1004 | } catch (RemoteException e) { |
| 1005 | Log.e(TAG, e.toString()); |
| 1006 | } |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 1007 | } else { |
| 1008 | Log.w(TAG, "Proxy not attached to service"); |
| 1009 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1010 | } |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1015 | * Initiates a connection of audio channel. |
| 1016 | * |
| 1017 | * It setup SCO channel with remote connected Handsfree AG device. |
| 1018 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1019 | * @param device remote device |
| 1020 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 1021 | * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1022 | */ |
Sanket Agarwal | 039eeb8 | 2017-01-20 14:55:15 -0800 | [diff] [blame] | 1023 | public boolean connectAudio(BluetoothDevice device) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1024 | final IBluetoothHeadsetClient service = mService; |
| 1025 | if (service != null && isEnabled()) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1026 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1027 | return service.connectAudio(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1028 | } catch (RemoteException e) { |
| 1029 | Log.e(TAG, e.toString()); |
| 1030 | } |
| 1031 | } else { |
| 1032 | Log.w(TAG, "Proxy not attached to service"); |
| 1033 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1034 | } |
| 1035 | return false; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Disconnects audio channel. |
| 1040 | * |
| 1041 | * It tears down the SCO channel from remote AG device. |
| 1042 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1043 | * @param device remote device |
| 1044 | * @return <code>true</code> if command has been issued successfully; <code>false</code> |
| 1045 | * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1046 | */ |
Sanket Agarwal | 039eeb8 | 2017-01-20 14:55:15 -0800 | [diff] [blame] | 1047 | public boolean disconnectAudio(BluetoothDevice device) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1048 | final IBluetoothHeadsetClient service = mService; |
| 1049 | if (service != null && isEnabled()) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1050 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1051 | return service.disconnectAudio(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1052 | } catch (RemoteException e) { |
| 1053 | Log.e(TAG, e.toString()); |
| 1054 | } |
| 1055 | } else { |
| 1056 | Log.w(TAG, "Proxy not attached to service"); |
| 1057 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1058 | } |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * Get Audio Gateway features |
| 1064 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1065 | * @param device remote device |
| 1066 | * @return bundle of AG features; null if no service or AG not connected |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1067 | */ |
| 1068 | public Bundle getCurrentAgFeatures(BluetoothDevice device) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1069 | final IBluetoothHeadsetClient service = mService; |
| 1070 | if (service != null && isEnabled()) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1071 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1072 | return service.getCurrentAgFeatures(device); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1073 | } catch (RemoteException e) { |
| 1074 | Log.e(TAG, e.toString()); |
| 1075 | } |
| 1076 | } else { |
| 1077 | Log.w(TAG, "Proxy not attached to service"); |
| 1078 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1079 | } |
| 1080 | return null; |
| 1081 | } |
| 1082 | |
| 1083 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1084 | private final ServiceConnection mConnection = new ServiceConnection() { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1085 | @Override |
| 1086 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 1087 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 1088 | mService = IBluetoothHeadsetClient.Stub.asInterface(Binder.allowBlocking(service)); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1089 | |
| 1090 | if (mServiceListener != null) { |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 1091 | mServiceListener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT, |
| 1092 | BluetoothHeadsetClient.this); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1093 | } |
| 1094 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 1095 | |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1096 | @Override |
| 1097 | public void onServiceDisconnected(ComponentName className) { |
| 1098 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 1099 | mService = null; |
| 1100 | if (mServiceListener != null) { |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 1101 | mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET_CLIENT); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | }; |
| 1105 | |
| 1106 | private boolean isEnabled() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1107 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1108 | } |
| 1109 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 1110 | private static boolean isValidDevice(BluetoothDevice device) { |
| 1111 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | private static void log(String msg) { |
| 1115 | Log.d(TAG, msg); |
| 1116 | } |
| 1117 | } |