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