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; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.RemoteException; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
Sanket Agarwal | 52a04e4 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 30 | import java.util.UUID; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 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 |
| 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 = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -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: |
| 79 | * {@link BluetoothProfile.EXTRA_STATE}, |
| 80 | * {@link BluetoothProfile.EXTRA_PREVIOUS_STATE}, |
| 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 = |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -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>, |
| 148 | * <code>false</code>.</p> |
| 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, |
| 156 | * 1 - network available </p> |
| 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 |
| 169 | * 1 - active roaming</p> |
| 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: |
| 188 | * 0 - voice recognition stopped, |
| 189 | * 1 - voice recognition started.</p> |
| 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: |
| 196 | * 0 - in band ring tone not supported, or |
| 197 | * 1 - in band ring tone supported.</p> |
| 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 | /** |
| 210 | * Extra for AG_CALL_CHANGED intent indicates the |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 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 | */ |
| 253 | public final static 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 | */ |
| 258 | public final static 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 | */ |
| 263 | public final static 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 | */ |
| 268 | public final static 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 | */ |
| 273 | public final static 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 | */ |
| 278 | public final static 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 | */ |
| 283 | public final static 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 | */ |
| 288 | public final static 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 | */ |
| 294 | public final static 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 | */ |
| 299 | public final static 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 | */ |
| 307 | public final static 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 */ |
| 311 | public final static int ACTION_RESULT_OK = 0; |
| 312 | public final static int ACTION_RESULT_ERROR = 1; |
| 313 | public final static int ACTION_RESULT_ERROR_NO_CARRIER = 2; |
| 314 | public final static int ACTION_RESULT_ERROR_BUSY = 3; |
| 315 | public final static int ACTION_RESULT_ERROR_NO_ANSWER = 4; |
| 316 | public final static int ACTION_RESULT_ERROR_DELAYED = 5; |
| 317 | public final static int ACTION_RESULT_ERROR_BLACKLISTED = 6; |
| 318 | public final static int ACTION_RESULT_ERROR_CME = 7; |
| 319 | |
| 320 | /* Detailed CME error codes */ |
| 321 | public final static int CME_PHONE_FAILURE = 0; |
| 322 | public final static int CME_NO_CONNECTION_TO_PHONE = 1; |
| 323 | public final static int CME_OPERATION_NOT_ALLOWED = 3; |
| 324 | public final static int CME_OPERATION_NOT_SUPPORTED = 4; |
| 325 | public final static int CME_PHSIM_PIN_REQUIRED = 5; |
| 326 | public final static int CME_PHFSIM_PIN_REQUIRED = 6; |
| 327 | public final static int CME_PHFSIM_PUK_REQUIRED = 7; |
| 328 | public final static int CME_SIM_NOT_INSERTED = 10; |
| 329 | public final static int CME_SIM_PIN_REQUIRED = 11; |
| 330 | public final static int CME_SIM_PUK_REQUIRED = 12; |
| 331 | public final static int CME_SIM_FAILURE = 13; |
| 332 | public final static int CME_SIM_BUSY = 14; |
| 333 | public final static int CME_SIM_WRONG = 15; |
| 334 | public final static int CME_INCORRECT_PASSWORD = 16; |
| 335 | public final static int CME_SIM_PIN2_REQUIRED = 17; |
| 336 | public final static int CME_SIM_PUK2_REQUIRED = 18; |
| 337 | public final static int CME_MEMORY_FULL = 20; |
| 338 | public final static int CME_INVALID_INDEX = 21; |
| 339 | public final static int CME_NOT_FOUND = 22; |
| 340 | public final static int CME_MEMORY_FAILURE = 23; |
| 341 | public final static int CME_TEXT_STRING_TOO_LONG = 24; |
| 342 | public final static int CME_INVALID_CHARACTER_IN_TEXT_STRING = 25; |
| 343 | public final static int CME_DIAL_STRING_TOO_LONG = 26; |
| 344 | public final static int CME_INVALID_CHARACTER_IN_DIAL_STRING = 27; |
| 345 | public final static int CME_NO_NETWORK_SERVICE = 30; |
| 346 | public final static int CME_NETWORK_TIMEOUT = 31; |
| 347 | public final static int CME_EMERGENCY_SERVICE_ONLY = 32; |
| 348 | public final static int CME_NO_SIMULTANOUS_VOIP_CS_CALLS = 33; |
| 349 | public final static int CME_NOT_SUPPORTED_FOR_VOIP = 34; |
| 350 | public final static int CME_SIP_RESPONSE_CODE = 35; |
| 351 | public final static int CME_NETWORK_PERSONALIZATION_PIN_REQUIRED = 40; |
| 352 | public final static int CME_NETWORK_PERSONALIZATION_PUK_REQUIRED = 41; |
| 353 | public final static int CME_NETWORK_SUBSET_PERSONALIZATION_PIN_REQUIRED = 42; |
| 354 | public final static int CME_NETWORK_SUBSET_PERSONALIZATION_PUK_REQUIRED = 43; |
| 355 | public final static int CME_SERVICE_PROVIDER_PERSONALIZATION_PIN_REQUIRED = 44; |
| 356 | public final static int CME_SERVICE_PROVIDER_PERSONALIZATION_PUK_REQUIRED = 45; |
| 357 | public final static int CME_CORPORATE_PERSONALIZATION_PIN_REQUIRED = 46; |
| 358 | public final static int CME_CORPORATE_PERSONALIZATION_PUK_REQUIRED = 47; |
| 359 | public final static int CME_HIDDEN_KEY_REQUIRED = 48; |
| 360 | public final static int CME_EAP_NOT_SUPPORTED = 49; |
| 361 | public final static int CME_INCORRECT_PARAMETERS = 50; |
| 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; |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 370 | private IBluetoothHeadsetClient mService; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 371 | private BluetoothAdapter mAdapter; |
| 372 | |
| 373 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 374 | new IBluetoothStateChangeCallback.Stub() { |
| 375 | @Override |
| 376 | public void onBluetoothStateChange(boolean up) { |
| 377 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 378 | if (!up) { |
| 379 | if (VDBG) Log.d(TAG,"Unbinding service..."); |
| 380 | synchronized (mConnection) { |
| 381 | try { |
| 382 | mService = null; |
| 383 | mContext.unbindService(mConnection); |
| 384 | } catch (Exception re) { |
| 385 | Log.e(TAG,"",re); |
| 386 | } |
| 387 | } |
| 388 | } else { |
| 389 | synchronized (mConnection) { |
| 390 | try { |
| 391 | if (mService == null) { |
| 392 | if (VDBG) Log.d(TAG,"Binding service..."); |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 393 | Intent intent = new Intent(IBluetoothHeadsetClient.class.getName()); |
| 394 | doBind(); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 395 | } |
| 396 | } catch (Exception re) { |
| 397 | Log.e(TAG,"",re); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | /** |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 405 | * Create a BluetoothHeadsetClient proxy object. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 406 | */ |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 407 | /*package*/ BluetoothHeadsetClient(Context context, ServiceListener l) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 408 | mContext = context; |
| 409 | mServiceListener = l; |
| 410 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 411 | |
| 412 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 413 | if (mgr != null) { |
| 414 | try { |
| 415 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 416 | } catch (RemoteException e) { |
| 417 | Log.e(TAG,"",e); |
| 418 | } |
| 419 | } |
| 420 | |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 421 | doBind(); |
| 422 | } |
| 423 | |
| 424 | boolean doBind() { |
| 425 | Intent intent = new Intent(IBluetoothHeadsetClient.class.getName()); |
| 426 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 427 | intent.setComponent(comp); |
| 428 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
| 429 | android.os.Process.myUserHandle())) { |
| 430 | Log.e(TAG, "Could not bind to Bluetooth Headset Client Service with " + intent); |
| 431 | return false; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 432 | } |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 433 | return true; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Close the connection to the backing service. |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 438 | * Other public functions of BluetoothHeadsetClient will return default error |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 439 | * results once close() has been called. Multiple invocations of close() |
| 440 | * are ok. |
| 441 | */ |
| 442 | /*package*/ void close() { |
| 443 | if (VDBG) log("close()"); |
| 444 | |
| 445 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 446 | if (mgr != null) { |
| 447 | try { |
| 448 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 449 | } catch (Exception e) { |
| 450 | Log.e(TAG,"",e); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | synchronized (mConnection) { |
| 455 | if (mService != null) { |
| 456 | try { |
| 457 | mService = null; |
| 458 | mContext.unbindService(mConnection); |
| 459 | } catch (Exception re) { |
| 460 | Log.e(TAG,"",re); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | mServiceListener = null; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Connects to remote device. |
| 469 | * |
| 470 | * Currently, the system supports only 1 connection. So, in case of the |
| 471 | * second connection, this implementation will disconnect already connected |
| 472 | * device automatically and will process the new one. |
| 473 | * |
| 474 | * @param device a remote device we want connect to |
| 475 | * @return <code>true</code> if command has been issued successfully; |
| 476 | * <code>false</code> otherwise; |
| 477 | * upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 478 | * intent. |
| 479 | */ |
| 480 | public boolean connect(BluetoothDevice device) { |
| 481 | if (DBG) log("connect(" + device + ")"); |
| 482 | if (mService != null && isEnabled() && |
| 483 | isValidDevice(device)) { |
| 484 | try { |
| 485 | return mService.connect(device); |
| 486 | } catch (RemoteException e) { |
| 487 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 488 | return false; |
| 489 | } |
| 490 | } |
| 491 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Disconnects remote device |
| 497 | * |
| 498 | * @param device a remote device we want disconnect |
| 499 | * @return <code>true</code> if command has been issued successfully; |
| 500 | * <code>false</code> otherwise; |
| 501 | * upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} |
| 502 | * intent. |
| 503 | */ |
| 504 | public boolean disconnect(BluetoothDevice device) { |
| 505 | if (DBG) log("disconnect(" + device + ")"); |
| 506 | if (mService != null && isEnabled() && |
| 507 | isValidDevice(device)) { |
| 508 | try { |
| 509 | return mService.disconnect(device); |
| 510 | } catch (RemoteException e) { |
| 511 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 512 | return false; |
| 513 | } |
| 514 | } |
| 515 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Return the list of connected remote devices |
| 521 | * |
| 522 | * @return list of connected devices; empty list if nothing is connected. |
| 523 | */ |
| 524 | @Override |
| 525 | public List<BluetoothDevice> getConnectedDevices() { |
| 526 | if (VDBG) log("getConnectedDevices()"); |
| 527 | if (mService != null && isEnabled()) { |
| 528 | try { |
| 529 | return mService.getConnectedDevices(); |
| 530 | } catch (RemoteException e) { |
| 531 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 532 | return new ArrayList<BluetoothDevice>(); |
| 533 | } |
| 534 | } |
| 535 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 536 | return new ArrayList<BluetoothDevice>(); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Returns list of remote devices in a particular state |
| 541 | * |
| 542 | * @param states collection of states |
| 543 | * @return list of devices that state matches the states listed in |
| 544 | * <code>states</code>; empty list if nothing matches the |
| 545 | * <code>states</code> |
| 546 | */ |
| 547 | @Override |
| 548 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 549 | if (VDBG) log("getDevicesMatchingStates()"); |
| 550 | if (mService != null && isEnabled()) { |
| 551 | try { |
| 552 | return mService.getDevicesMatchingConnectionStates(states); |
| 553 | } catch (RemoteException e) { |
| 554 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 555 | return new ArrayList<BluetoothDevice>(); |
| 556 | } |
| 557 | } |
| 558 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 559 | return new ArrayList<BluetoothDevice>(); |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Returns state of the <code>device</code> |
| 564 | * |
| 565 | * @param device a remote device |
| 566 | * @return the state of connection of the device |
| 567 | */ |
| 568 | @Override |
| 569 | public int getConnectionState(BluetoothDevice device) { |
| 570 | if (VDBG) log("getConnectionState(" + device + ")"); |
| 571 | if (mService != null && isEnabled() && |
| 572 | isValidDevice(device)) { |
| 573 | try { |
| 574 | return mService.getConnectionState(device); |
| 575 | } catch (RemoteException e) { |
| 576 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 577 | return BluetoothProfile.STATE_DISCONNECTED; |
| 578 | } |
| 579 | } |
| 580 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 581 | return BluetoothProfile.STATE_DISCONNECTED; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Set priority of the profile |
| 586 | * |
| 587 | * The device should already be paired. |
| 588 | */ |
| 589 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 590 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
| 591 | if (mService != null && isEnabled() && |
| 592 | isValidDevice(device)) { |
| 593 | if (priority != BluetoothProfile.PRIORITY_OFF && |
| 594 | priority != BluetoothProfile.PRIORITY_ON) { |
| 595 | return false; |
| 596 | } |
| 597 | try { |
| 598 | return mService.setPriority(device, priority); |
| 599 | } catch (RemoteException e) { |
| 600 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 601 | return false; |
| 602 | } |
| 603 | } |
| 604 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Get the priority of the profile. |
| 610 | */ |
| 611 | public int getPriority(BluetoothDevice device) { |
| 612 | if (VDBG) log("getPriority(" + device + ")"); |
| 613 | if (mService != null && isEnabled() && |
| 614 | isValidDevice(device)) { |
| 615 | try { |
| 616 | return mService.getPriority(device); |
| 617 | } catch (RemoteException e) { |
| 618 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 619 | return PRIORITY_OFF; |
| 620 | } |
| 621 | } |
| 622 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 623 | return PRIORITY_OFF; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Starts voice recognition. |
| 628 | * |
| 629 | * @param device remote device |
| 630 | * @return <code>true</code> if command has been issued successfully; |
| 631 | * <code>false</code> otherwise; |
| 632 | * upon completion HFP sends {@link #ACTION_AG_EVENT} |
| 633 | * intent. |
| 634 | * |
| 635 | * <p>Feature required for successful execution is being reported by: |
| 636 | * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. |
| 637 | * This method invocation will fail silently when feature is not supported.</p> |
| 638 | */ |
| 639 | public boolean startVoiceRecognition(BluetoothDevice device) { |
| 640 | if (DBG) log("startVoiceRecognition()"); |
| 641 | if (mService != null && isEnabled() && |
| 642 | isValidDevice(device)) { |
| 643 | try { |
| 644 | return mService.startVoiceRecognition(device); |
| 645 | } catch (RemoteException e) { |
| 646 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 647 | } |
| 648 | } |
| 649 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Stops voice recognition. |
| 655 | * |
| 656 | * @param device remote device |
| 657 | * @return <code>true</code> if command has been issued successfully; |
| 658 | * <code>false</code> otherwise; |
| 659 | * upon completion HFP sends {@link #ACTION_AG_EVENT} |
| 660 | * intent. |
| 661 | * |
| 662 | * <p>Feature required for successful execution is being reported by: |
| 663 | * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. |
| 664 | * This method invocation will fail silently when feature is not supported.</p> |
| 665 | */ |
| 666 | public boolean stopVoiceRecognition(BluetoothDevice device) { |
| 667 | if (DBG) log("stopVoiceRecognition()"); |
| 668 | if (mService != null && isEnabled() && |
| 669 | isValidDevice(device)) { |
| 670 | try { |
| 671 | return mService.stopVoiceRecognition(device); |
| 672 | } catch (RemoteException e) { |
| 673 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 674 | } |
| 675 | } |
| 676 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 677 | return false; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Returns list of all calls in any state. |
| 682 | * |
| 683 | * @param device remote device |
| 684 | * @return list of calls; empty list if none call exists |
| 685 | */ |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 686 | public List<BluetoothHeadsetClientCall> getCurrentCalls(BluetoothDevice device) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 687 | if (DBG) log("getCurrentCalls()"); |
| 688 | if (mService != null && isEnabled() && |
| 689 | isValidDevice(device)) { |
| 690 | try { |
| 691 | return mService.getCurrentCalls(device); |
| 692 | } catch (RemoteException e) { |
| 693 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 694 | } |
| 695 | } |
| 696 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 697 | return null; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Returns list of current values of AG indicators. |
| 702 | * |
| 703 | * @param device remote device |
| 704 | * @return bundle of AG indicators; null if device is not in |
| 705 | * CONNECTED state |
| 706 | */ |
| 707 | public Bundle getCurrentAgEvents(BluetoothDevice device) { |
| 708 | if (DBG) log("getCurrentCalls()"); |
| 709 | if (mService != null && isEnabled() && |
| 710 | isValidDevice(device)) { |
| 711 | try { |
| 712 | return mService.getCurrentAgEvents(device); |
| 713 | } catch (RemoteException e) { |
| 714 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 715 | } |
| 716 | } |
| 717 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 718 | return null; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Accepts a call |
| 723 | * |
| 724 | * @param device remote device |
| 725 | * @param flag action policy while accepting a call. Possible values |
| 726 | * {@link #CALL_ACCEPT_NONE}, {@link #CALL_ACCEPT_HOLD}, |
| 727 | * {@link #CALL_ACCEPT_TERMINATE} |
| 728 | * @return <code>true</code> if command has been issued successfully; |
| 729 | * <code>false</code> otherwise; |
| 730 | * upon completion HFP sends {@link #ACTION_CALL_CHANGED} |
| 731 | * intent. |
| 732 | */ |
| 733 | public boolean acceptCall(BluetoothDevice device, int flag) { |
| 734 | if (DBG) log("acceptCall()"); |
| 735 | if (mService != null && isEnabled() && |
| 736 | isValidDevice(device)) { |
| 737 | try { |
| 738 | return mService.acceptCall(device, flag); |
| 739 | } catch (RemoteException e) { |
| 740 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 741 | } |
| 742 | } |
| 743 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Holds a call. |
| 749 | * |
| 750 | * @param device remote device |
| 751 | * @return <code>true</code> if command has been issued successfully; |
| 752 | * <code>false</code> otherwise; |
| 753 | * upon completion HFP sends {@link #ACTION_CALL_CHANGED} |
| 754 | * intent. |
| 755 | */ |
| 756 | public boolean holdCall(BluetoothDevice device) { |
| 757 | if (DBG) log("holdCall()"); |
| 758 | if (mService != null && isEnabled() && |
| 759 | isValidDevice(device)) { |
| 760 | try { |
| 761 | return mService.holdCall(device); |
| 762 | } catch (RemoteException e) { |
| 763 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 764 | } |
| 765 | } |
| 766 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Rejects a call. |
| 772 | * |
| 773 | * @param device remote device |
| 774 | * @return <code>true</code> if command has been issued successfully; |
| 775 | * <code>false</code> otherwise; |
| 776 | * upon completion HFP sends {@link #ACTION_CALL_CHANGED} |
| 777 | * intent. |
| 778 | * |
| 779 | * <p>Feature required for successful execution is being reported by: |
| 780 | * {@link #EXTRA_AG_FEATURE_REJECT_CALL}. |
| 781 | * This method invocation will fail silently when feature is not supported.</p> |
| 782 | */ |
| 783 | public boolean rejectCall(BluetoothDevice device) { |
| 784 | if (DBG) log("rejectCall()"); |
| 785 | if (mService != null && isEnabled() && |
| 786 | isValidDevice(device)) { |
| 787 | try { |
| 788 | return mService.rejectCall(device); |
| 789 | } catch (RemoteException e) { |
| 790 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 791 | } |
| 792 | } |
| 793 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Terminates a specified call. |
| 799 | * |
| 800 | * Works only when Extended Call Control is supported by Audio Gateway. |
| 801 | * |
| 802 | * @param device remote device |
Sanket Agarwal | 52a04e4 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 803 | * @param call Handle of call obtained in {@link dial()} or obtained via |
| 804 | * {@link ACTION_CALL_CHANGED}. {@code call} may be null in which |
| 805 | * case we will hangup all active calls. |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 806 | * @return <code>true</code> if command has been issued successfully; |
| 807 | * <code>false</code> otherwise; |
| 808 | * upon completion HFP sends {@link #ACTION_CALL_CHANGED} |
| 809 | * intent. |
| 810 | * |
| 811 | * <p>Feature required for successful execution is being reported by: |
| 812 | * {@link #EXTRA_AG_FEATURE_ECC}. |
| 813 | * This method invocation will fail silently when feature is not supported.</p> |
| 814 | */ |
Sanket Agarwal | 52a04e4 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 815 | public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 816 | if (DBG) log("terminateCall()"); |
| 817 | if (mService != null && isEnabled() && |
| 818 | isValidDevice(device)) { |
| 819 | try { |
Sanket Agarwal | 52a04e4 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 820 | return mService.terminateCall(device, call); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 821 | } catch (RemoteException e) { |
| 822 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 823 | } |
| 824 | } |
| 825 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 826 | return false; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Enters private mode with a specified call. |
| 831 | * |
| 832 | * Works only when Extended Call Control is supported by Audio Gateway. |
| 833 | * |
| 834 | * @param device remote device |
| 835 | * @param index index of the call to connect in private mode |
| 836 | * @return <code>true</code> if command has been issued successfully; |
| 837 | * <code>false</code> otherwise; |
| 838 | * upon completion HFP sends {@link #ACTION_CALL_CHANGED} |
| 839 | * intent. |
| 840 | * |
| 841 | * <p>Feature required for successful execution is being reported by: |
| 842 | * {@link #EXTRA_AG_FEATURE_ECC}. |
| 843 | * This method invocation will fail silently when feature is not supported.</p> |
| 844 | */ |
| 845 | public boolean enterPrivateMode(BluetoothDevice device, int index) { |
| 846 | if (DBG) log("enterPrivateMode()"); |
| 847 | if (mService != null && isEnabled() && |
| 848 | isValidDevice(device)) { |
| 849 | try { |
| 850 | return mService.enterPrivateMode(device, index); |
| 851 | } catch (RemoteException e) { |
| 852 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 853 | } |
| 854 | } |
| 855 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Performs explicit call transfer. |
| 861 | * |
| 862 | * That means connect other calls and disconnect. |
| 863 | * |
| 864 | * @param device remote device |
| 865 | * @return <code>true</code> if command has been issued successfully; |
| 866 | * <code>false</code> otherwise; |
| 867 | * upon completion HFP sends {@link #ACTION_CALL_CHANGED} |
| 868 | * intent. |
| 869 | * |
| 870 | * <p>Feature required for successful execution is being reported by: |
| 871 | * {@link #EXTRA_AG_FEATURE_MERGE_AND_DETACH}. |
| 872 | * This method invocation will fail silently when feature is not supported.</p> |
| 873 | */ |
| 874 | public boolean explicitCallTransfer(BluetoothDevice device) { |
| 875 | if (DBG) log("explicitCallTransfer()"); |
| 876 | if (mService != null && isEnabled() && |
| 877 | isValidDevice(device)) { |
| 878 | try { |
| 879 | return mService.explicitCallTransfer(device); |
| 880 | } catch (RemoteException e) { |
| 881 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 882 | } |
| 883 | } |
| 884 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 885 | return false; |
| 886 | } |
| 887 | |
| 888 | /** |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 889 | * Places a call with specified number. |
| 890 | * |
| 891 | * @param device remote device |
| 892 | * @param number valid phone number |
Sanket Agarwal | 52a04e4 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 893 | * @return <code>{@link BluetoothHeadsetClientCall} call</code> if command has been |
| 894 | * issued successfully; |
| 895 | * <code>{@link null}</code> otherwise; |
| 896 | * upon completion HFP sends {@link #ACTION_CALL_CHANGED} |
| 897 | * intent in case of success; {@link #ACTION_RESULT} is sent |
| 898 | * otherwise; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 899 | */ |
Sanket Agarwal | 52a04e4 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 900 | public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) { |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 901 | if (DBG) log("dial()"); |
| 902 | if (mService != null && isEnabled() && |
| 903 | isValidDevice(device)) { |
| 904 | try { |
| 905 | return mService.dial(device, number); |
| 906 | } catch (RemoteException e) { |
| 907 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 908 | } |
| 909 | } |
| 910 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Sanket Agarwal | 52a04e4 | 2016-06-27 20:13:54 -0700 | [diff] [blame] | 911 | return null; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /** |
| 915 | * Sends DTMF code. |
| 916 | * |
| 917 | * Possible code values : 0,1,2,3,4,5,6,7,8,9,A,B,C,D,*,# |
| 918 | * |
| 919 | * @param device remote device |
| 920 | * @param code ASCII code |
| 921 | * @return <code>true</code> if command has been issued successfully; |
| 922 | * <code>false</code> otherwise; |
| 923 | * upon completion HFP sends {@link #ACTION_RESULT} intent; |
| 924 | */ |
| 925 | public boolean sendDTMF(BluetoothDevice device, byte code) { |
| 926 | if (DBG) log("sendDTMF()"); |
| 927 | if (mService != null && isEnabled() && |
| 928 | isValidDevice(device)) { |
| 929 | try { |
| 930 | return mService.sendDTMF(device, code); |
| 931 | } catch (RemoteException e) { |
| 932 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 933 | } |
| 934 | } |
| 935 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Get a number corresponding to last voice tag recorded on AG. |
| 941 | * |
| 942 | * @param device remote device |
| 943 | * @return <code>true</code> if command has been issued successfully; |
| 944 | * <code>false</code> otherwise; |
| 945 | * upon completion HFP sends {@link #ACTION_LAST_VTAG} |
| 946 | * or {@link #ACTION_RESULT} intent; |
| 947 | * |
| 948 | * <p>Feature required for successful execution is being reported by: |
| 949 | * {@link #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}. |
| 950 | * This method invocation will fail silently when feature is not supported.</p> |
| 951 | */ |
| 952 | public boolean getLastVoiceTagNumber(BluetoothDevice device) { |
| 953 | if (DBG) log("getLastVoiceTagNumber()"); |
| 954 | if (mService != null && isEnabled() && |
| 955 | isValidDevice(device)) { |
| 956 | try { |
| 957 | return mService.getLastVoiceTagNumber(device); |
| 958 | } catch (RemoteException e) { |
| 959 | Log.e(TAG, Log.getStackTraceString(new Throwable())); |
| 960 | } |
| 961 | } |
| 962 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * Accept the incoming connection. |
| 968 | */ |
| 969 | public boolean acceptIncomingConnect(BluetoothDevice device) { |
| 970 | if (DBG) log("acceptIncomingConnect"); |
| 971 | if (mService != null && isEnabled()) { |
| 972 | try { |
| 973 | return mService.acceptIncomingConnect(device); |
| 974 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 975 | } else { |
| 976 | Log.w(TAG, "Proxy not attached to service"); |
| 977 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 978 | } |
| 979 | return false; |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Reject the incoming connection. |
| 984 | */ |
| 985 | public boolean rejectIncomingConnect(BluetoothDevice device) { |
| 986 | if (DBG) log("rejectIncomingConnect"); |
| 987 | if (mService != null) { |
| 988 | try { |
| 989 | return mService.rejectIncomingConnect(device); |
| 990 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 991 | } else { |
| 992 | Log.w(TAG, "Proxy not attached to service"); |
| 993 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 994 | } |
| 995 | return false; |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * Returns current audio state of Audio Gateway. |
| 1000 | * |
| 1001 | * Note: This is an internal function and shouldn't be exposed |
| 1002 | */ |
| 1003 | public int getAudioState(BluetoothDevice device) { |
| 1004 | if (VDBG) log("getAudioState"); |
| 1005 | if (mService != null && isEnabled()) { |
| 1006 | try { |
| 1007 | return mService.getAudioState(device); |
| 1008 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 1009 | } else { |
| 1010 | Log.w(TAG, "Proxy not attached to service"); |
| 1011 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1012 | } |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 1013 | return BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED; |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /** |
Bryce Lee | dc13382 | 2015-10-28 22:21:54 -0700 | [diff] [blame] | 1017 | * Sets whether audio routing is allowed. |
| 1018 | * |
| 1019 | * Note: This is an internal function and shouldn't be exposed |
| 1020 | */ |
| 1021 | public void setAudioRouteAllowed(boolean allowed) { |
| 1022 | if (VDBG) log("setAudioRouteAllowed"); |
| 1023 | if (mService != null && isEnabled()) { |
| 1024 | try { |
| 1025 | mService.setAudioRouteAllowed(allowed); |
| 1026 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 1027 | } else { |
| 1028 | Log.w(TAG, "Proxy not attached to service"); |
| 1029 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * Returns whether audio routing is allowed. |
| 1035 | * |
| 1036 | * Note: This is an internal function and shouldn't be exposed |
| 1037 | */ |
| 1038 | public boolean getAudioRouteAllowed() { |
| 1039 | if (VDBG) log("getAudioRouteAllowed"); |
| 1040 | if (mService != null && isEnabled()) { |
| 1041 | try { |
| 1042 | return mService.getAudioRouteAllowed(); |
| 1043 | } catch (RemoteException e) {Log.e(TAG, e.toString());} |
| 1044 | } else { |
| 1045 | Log.w(TAG, "Proxy not attached to service"); |
| 1046 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1047 | } |
| 1048 | return false; |
| 1049 | } |
| 1050 | |
| 1051 | /** |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1052 | * Initiates a connection of audio channel. |
| 1053 | * |
| 1054 | * It setup SCO channel with remote connected Handsfree AG device. |
| 1055 | * |
| 1056 | * @return <code>true</code> if command has been issued successfully; |
| 1057 | * <code>false</code> otherwise; |
| 1058 | * upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} |
| 1059 | * intent; |
| 1060 | */ |
| 1061 | public boolean connectAudio() { |
| 1062 | if (mService != null && isEnabled()) { |
| 1063 | try { |
| 1064 | return mService.connectAudio(); |
| 1065 | } catch (RemoteException e) { |
| 1066 | Log.e(TAG, e.toString()); |
| 1067 | } |
| 1068 | } else { |
| 1069 | Log.w(TAG, "Proxy not attached to service"); |
| 1070 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1071 | } |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
| 1076 | * Disconnects audio channel. |
| 1077 | * |
| 1078 | * It tears down the SCO channel from remote AG device. |
| 1079 | * |
| 1080 | * @return <code>true</code> if command has been issued successfully; |
| 1081 | * <code>false</code> otherwise; |
| 1082 | * upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} |
| 1083 | * intent; |
| 1084 | */ |
| 1085 | public boolean disconnectAudio() { |
| 1086 | if (mService != null && isEnabled()) { |
| 1087 | try { |
| 1088 | return mService.disconnectAudio(); |
| 1089 | } catch (RemoteException e) { |
| 1090 | Log.e(TAG, e.toString()); |
| 1091 | } |
| 1092 | } else { |
| 1093 | Log.w(TAG, "Proxy not attached to service"); |
| 1094 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1095 | } |
| 1096 | return false; |
| 1097 | } |
| 1098 | |
| 1099 | /** |
| 1100 | * Get Audio Gateway features |
| 1101 | * |
| 1102 | * @param device remote device |
| 1103 | * @return bundle of AG features; null if no service or |
| 1104 | * AG not connected |
| 1105 | */ |
| 1106 | public Bundle getCurrentAgFeatures(BluetoothDevice device) { |
| 1107 | if (mService != null && isEnabled()) { |
| 1108 | try { |
| 1109 | return mService.getCurrentAgFeatures(device); |
| 1110 | } catch (RemoteException e) { |
| 1111 | Log.e(TAG, e.toString()); |
| 1112 | } |
| 1113 | } else { |
| 1114 | Log.w(TAG, "Proxy not attached to service"); |
| 1115 | if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); |
| 1116 | } |
| 1117 | return null; |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | private ServiceConnection mConnection = new ServiceConnection() { |
| 1122 | @Override |
| 1123 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 1124 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 1125 | mService = IBluetoothHeadsetClient.Stub.asInterface(service); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1126 | |
| 1127 | if (mServiceListener != null) { |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 1128 | mServiceListener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT, |
| 1129 | BluetoothHeadsetClient.this); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | @Override |
| 1133 | public void onServiceDisconnected(ComponentName className) { |
| 1134 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 1135 | mService = null; |
| 1136 | if (mServiceListener != null) { |
Mike Lockwood | cf916d3 | 2014-06-12 11:23:40 -0700 | [diff] [blame] | 1137 | mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET_CLIENT); |
Hemant Gupta | 7aca90f | 2013-08-19 19:03:51 +0530 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | }; |
| 1141 | |
| 1142 | private boolean isEnabled() { |
| 1143 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | private boolean isValidDevice(BluetoothDevice device) { |
| 1148 | if (device == null) return false; |
| 1149 | |
| 1150 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | private static void log(String msg) { |
| 1155 | Log.d(TAG, msg); |
| 1156 | } |
| 1157 | } |