blob: 5d00f09501b0dd062d41d7b7b00d70357a266001 [file] [log] [blame]
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301/*
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
17package android.bluetooth;
18
Mathew Inwood4dc66d32018-08-01 15:07:20 +010019import android.annotation.UnsupportedAppUsage;
Hemant Gupta7aca90f2013-08-19 19:03:51 +053020import android.content.Context;
Jeff Sharkey0a17db12016-11-04 11:23:46 -060021import android.os.Binder;
Hemant Gupta7aca90f2013-08-19 19:03:51 +053022import android.os.Bundle;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.util.Log;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * Public API to control Hands Free Profile (HFP role only).
32 * <p>
33 * This class defines methods that shall be used by application to manage profile
34 * connection, calls states and calls actions.
35 * <p>
36 *
37 * @hide
Jack Hea355e5e2017-08-22 16:06:54 -070038 */
Mike Lockwoodcf916d32014-06-12 11:23:40 -070039public final class BluetoothHeadsetClient implements BluetoothProfile {
40 private static final String TAG = "BluetoothHeadsetClient";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053041 private static final boolean DBG = true;
42 private static final boolean VDBG = false;
43
44 /**
45 * Intent sent whenever connection to remote changes.
46 *
47 * <p>It includes two extras:
48 * <code>BluetoothProfile.EXTRA_PREVIOUS_STATE</code>
49 * and <code>BluetoothProfile.EXTRA_STATE</code>, which
50 * are mandatory.
51 * <p>There are also non mandatory feature extras:
52 * {@link #EXTRA_AG_FEATURE_3WAY_CALLING},
53 * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION},
54 * {@link #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT},
55 * {@link #EXTRA_AG_FEATURE_REJECT_CALL},
56 * {@link #EXTRA_AG_FEATURE_ECC},
57 * {@link #EXTRA_AG_FEATURE_RESPONSE_AND_HOLD},
58 * {@link #EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL},
59 * {@link #EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL},
60 * {@link #EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT},
61 * {@link #EXTRA_AG_FEATURE_MERGE},
62 * {@link #EXTRA_AG_FEATURE_MERGE_AND_DETACH},
63 * sent as boolean values only when <code>EXTRA_STATE</code>
64 * is set to <code>STATE_CONNECTED</code>.</p>
65 *
66 * <p>Note that features supported by AG are being sent as
67 * booleans with value <code>true</code>,
68 * and not supported ones are <strong>not</strong> being sent at all.</p>
69 */
70 public static final String ACTION_CONNECTION_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070071 "android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053072
73 /**
74 * Intent sent whenever audio state changes.
75 *
76 * <p>It includes two mandatory extras:
Jack He16eeac32017-08-17 12:11:18 -070077 * {@link BluetoothProfile#EXTRA_STATE},
78 * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE},
Hemant Gupta7aca90f2013-08-19 19:03:51 +053079 * with possible values:
80 * {@link #STATE_AUDIO_CONNECTING},
81 * {@link #STATE_AUDIO_CONNECTED},
82 * {@link #STATE_AUDIO_DISCONNECTED}</p>
83 * <p>When <code>EXTRA_STATE</code> is set
84 * to </code>STATE_AUDIO_CONNECTED</code>,
85 * it also includes {@link #EXTRA_AUDIO_WBS}
86 * indicating wide band speech support.</p>
87 */
88 public static final String ACTION_AUDIO_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070089 "android.bluetooth.headsetclient.profile.action.AUDIO_STATE_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053090
91 /**
92 * Intent sending updates of the Audio Gateway state.
93 * Each extra is being sent only when value it
94 * represents has been changed recently on AG.
95 * <p>It can contain one or more of the following extras:
96 * {@link #EXTRA_NETWORK_STATUS},
97 * {@link #EXTRA_NETWORK_SIGNAL_STRENGTH},
98 * {@link #EXTRA_NETWORK_ROAMING},
99 * {@link #EXTRA_BATTERY_LEVEL},
100 * {@link #EXTRA_OPERATOR_NAME},
101 * {@link #EXTRA_VOICE_RECOGNITION},
102 * {@link #EXTRA_IN_BAND_RING}</p>
103 */
104 public static final String ACTION_AG_EVENT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700105 "android.bluetooth.headsetclient.profile.action.AG_EVENT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530106
107 /**
108 * Intent sent whenever state of a call changes.
109 *
110 * <p>It includes:
111 * {@link #EXTRA_CALL},
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700112 * with value of {@link BluetoothHeadsetClientCall} instance,
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530113 * representing actual call state.</p>
114 */
115 public static final String ACTION_CALL_CHANGED =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700116 "android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530117
118 /**
119 * Intent that notifies about the result of the last issued action.
120 * Please note that not every action results in explicit action result code being sent.
121 * Instead other notifications about new Audio Gateway state might be sent,
122 * like <code>ACTION_AG_EVENT</code> with <code>EXTRA_VOICE_RECOGNITION</code> value
123 * when for example user started voice recognition from HF unit.
124 */
125 public static final String ACTION_RESULT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700126 "android.bluetooth.headsetclient.profile.action.RESULT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530127
128 /**
Deqiang Chen2fec85d2019-06-25 14:30:55 -0700129 * Intent that notifies about vendor specific event arrival. Events not defined in
130 * HFP spec will be matched with supported vendor event list and this intent will
131 * be broadcasted upon a match. Supported vendor events are of format of
132 * of "+eventCode" or "+eventCode=xxxx" or "+eventCode:=xxxx".
133 * Vendor event can be a response to an vendor specific command or unsolicited.
134 *
135 */
136 public static final String ACTION_VENDOR_SPECIFIC_HEADSETCLIENT_EVENT =
137 "android.bluetooth.headsetclient.profile.action.VENDOR_SPECIFIC_EVENT";
138
139 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530140 * Intent that notifies about the number attached to the last voice tag
141 * recorded on AG.
142 *
143 * <p>It contains:
144 * {@link #EXTRA_NUMBER},
145 * with a <code>String</code> value representing phone number.</p>
146 */
147 public static final String ACTION_LAST_VTAG =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700148 "android.bluetooth.headsetclient.profile.action.LAST_VTAG";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530149
150 public static final int STATE_AUDIO_DISCONNECTED = 0;
151 public static final int STATE_AUDIO_CONNECTING = 1;
152 public static final int STATE_AUDIO_CONNECTED = 2;
153
154 /**
155 * Extra with information if connected audio is WBS.
156 * <p>Possible values: <code>true</code>,
Jack Hea355e5e2017-08-22 16:06:54 -0700157 * <code>false</code>.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530158 */
159 public static final String EXTRA_AUDIO_WBS =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700160 "android.bluetooth.headsetclient.extra.AUDIO_WBS";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530161
162 /**
163 * Extra for AG_EVENT indicates network status.
164 * <p>Value: 0 - network unavailable,
Jack Hea355e5e2017-08-22 16:06:54 -0700165 * 1 - network available </p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530166 */
167 public static final String EXTRA_NETWORK_STATUS =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700168 "android.bluetooth.headsetclient.extra.NETWORK_STATUS";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530169 /**
170 * Extra for AG_EVENT intent indicates network signal strength.
171 * <p>Value: <code>Integer</code> representing signal strength.</p>
172 */
173 public static final String EXTRA_NETWORK_SIGNAL_STRENGTH =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700174 "android.bluetooth.headsetclient.extra.NETWORK_SIGNAL_STRENGTH";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530175 /**
176 * Extra for AG_EVENT intent indicates roaming state.
177 * <p>Value: 0 - no roaming
Jack Hea355e5e2017-08-22 16:06:54 -0700178 * 1 - active roaming</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530179 */
180 public static final String EXTRA_NETWORK_ROAMING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700181 "android.bluetooth.headsetclient.extra.NETWORK_ROAMING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530182 /**
183 * Extra for AG_EVENT intent indicates the battery level.
184 * <p>Value: <code>Integer</code> representing signal strength.</p>
185 */
186 public static final String EXTRA_BATTERY_LEVEL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700187 "android.bluetooth.headsetclient.extra.BATTERY_LEVEL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530188 /**
189 * Extra for AG_EVENT intent indicates operator name.
190 * <p>Value: <code>String</code> representing operator name.</p>
191 */
192 public static final String EXTRA_OPERATOR_NAME =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700193 "android.bluetooth.headsetclient.extra.OPERATOR_NAME";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530194 /**
195 * Extra for AG_EVENT intent indicates voice recognition state.
196 * <p>Value:
Jack Hea355e5e2017-08-22 16:06:54 -0700197 * 0 - voice recognition stopped,
198 * 1 - voice recognition started.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530199 */
200 public static final String EXTRA_VOICE_RECOGNITION =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700201 "android.bluetooth.headsetclient.extra.VOICE_RECOGNITION";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530202 /**
203 * Extra for AG_EVENT intent indicates in band ring state.
204 * <p>Value:
Jack Hea355e5e2017-08-22 16:06:54 -0700205 * 0 - in band ring tone not supported, or
206 * 1 - in band ring tone supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530207 */
208 public static final String EXTRA_IN_BAND_RING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700209 "android.bluetooth.headsetclient.extra.IN_BAND_RING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530210
211 /**
212 * Extra for AG_EVENT intent indicates subscriber info.
213 * <p>Value: <code>String</code> containing subscriber information.</p>
214 */
215 public static final String EXTRA_SUBSCRIBER_INFO =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700216 "android.bluetooth.headsetclient.extra.SUBSCRIBER_INFO";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530217
218 /**
Jack Hea355e5e2017-08-22 16:06:54 -0700219 * Extra for AG_CALL_CHANGED intent indicates the
220 * {@link BluetoothHeadsetClientCall} object that has changed.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530221 */
222 public static final String EXTRA_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700223 "android.bluetooth.headsetclient.extra.CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530224
225 /**
226 * Extra for ACTION_LAST_VTAG intent.
227 * <p>Value: <code>String</code> representing phone number
228 * corresponding to last voice tag recorded on AG</p>
229 */
230 public static final String EXTRA_NUMBER =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700231 "android.bluetooth.headsetclient.extra.NUMBER";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530232
233 /**
234 * Extra for ACTION_RESULT intent that shows the result code of
235 * last issued action.
236 * <p>Possible results:
237 * {@link #ACTION_RESULT_OK},
238 * {@link #ACTION_RESULT_ERROR},
239 * {@link #ACTION_RESULT_ERROR_NO_CARRIER},
240 * {@link #ACTION_RESULT_ERROR_BUSY},
241 * {@link #ACTION_RESULT_ERROR_NO_ANSWER},
242 * {@link #ACTION_RESULT_ERROR_DELAYED},
243 * {@link #ACTION_RESULT_ERROR_BLACKLISTED},
244 * {@link #ACTION_RESULT_ERROR_CME}</p>
245 */
246 public static final String EXTRA_RESULT_CODE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700247 "android.bluetooth.headsetclient.extra.RESULT_CODE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530248
249 /**
250 * Extra for ACTION_RESULT intent that shows the extended result code of
251 * last issued action.
252 * <p>Value: <code>Integer</code> - error code.</p>
253 */
254 public static final String EXTRA_CME_CODE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700255 "android.bluetooth.headsetclient.extra.CME_CODE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530256
Deqiang Chen2fec85d2019-06-25 14:30:55 -0700257 /**
258 * Extra for VENDOR_SPECIFIC_HEADSETCLIENT_EVENT intent that
259 * indicates vendor ID.
260 */
261 public static final String EXTRA_VENDOR_ID =
262 "android.bluetooth.headsetclient.extra.VENDOR_ID";
263
264 /**
265 * Extra for VENDOR_SPECIFIC_HEADSETCLIENT_EVENT intent that
266 * indicates vendor event code.
267 */
268 public static final String EXTRA_VENDOR_EVENT_CODE =
269 "android.bluetooth.headsetclient.extra.VENDOR_EVENT_CODE";
270
271 /**
272 * Extra for VENDOR_SPECIFIC_HEADSETCLIENT_EVENT intent that
273 * contains full vendor event including event code and full arguments.
274 */
275 public static final String EXTRA_VENDOR_EVENT_FULL_ARGS =
276 "android.bluetooth.headsetclient.extra.VENDOR_EVENT_FULL_ARGS";
277
278
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530279 /* Extras for AG_FEATURES, extras type is boolean */
280 // TODO verify if all of those are actually useful
281 /**
282 * AG feature: three way calling.
283 */
Jack He2992cd02017-08-22 21:21:23 -0700284 public static final String EXTRA_AG_FEATURE_3WAY_CALLING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700285 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_3WAY_CALLING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530286 /**
287 * AG feature: voice recognition.
288 */
Jack He2992cd02017-08-22 21:21:23 -0700289 public static final String EXTRA_AG_FEATURE_VOICE_RECOGNITION =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700290 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_VOICE_RECOGNITION";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530291 /**
292 * AG feature: fetching phone number for voice tagging procedure.
293 */
Jack He2992cd02017-08-22 21:21:23 -0700294 public static final String EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700295 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530296 /**
297 * AG feature: ability to reject incoming call.
298 */
Jack He2992cd02017-08-22 21:21:23 -0700299 public static final String EXTRA_AG_FEATURE_REJECT_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700300 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_REJECT_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530301 /**
302 * AG feature: enhanced call handling (terminate specific call, private consultation).
303 */
Jack He2992cd02017-08-22 21:21:23 -0700304 public static final String EXTRA_AG_FEATURE_ECC =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700305 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ECC";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530306 /**
307 * AG feature: response and hold.
308 */
Jack He2992cd02017-08-22 21:21:23 -0700309 public static final String EXTRA_AG_FEATURE_RESPONSE_AND_HOLD =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700310 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RESPONSE_AND_HOLD";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530311 /**
312 * AG call handling feature: accept held or waiting call in three way calling scenarios.
313 */
Jack He2992cd02017-08-22 21:21:23 -0700314 public static final String EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700315 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530316 /**
317 * AG call handling feature: release held or waiting call in three way calling scenarios.
318 */
Jack He2992cd02017-08-22 21:21:23 -0700319 public static final String EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700320 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530321 /**
322 * AG call handling feature: release active call and accept held or waiting call in three way
323 * calling scenarios.
324 */
Jack He2992cd02017-08-22 21:21:23 -0700325 public static final String EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700326 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530327 /**
328 * AG call handling feature: merge two calls, held and active - multi party conference mode.
329 */
Jack He2992cd02017-08-22 21:21:23 -0700330 public static final String EXTRA_AG_FEATURE_MERGE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700331 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530332 /**
333 * AG call handling feature: merge calls and disconnect from multi party
334 * conversation leaving peers connected to each other.
335 * Note that this feature needs to be supported by mobile network operator
336 * as it requires connection and billing transfer.
337 */
Jack He2992cd02017-08-22 21:21:23 -0700338 public static final String EXTRA_AG_FEATURE_MERGE_AND_DETACH =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700339 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE_AND_DETACH";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530340
341 /* Action result codes */
Jack He2992cd02017-08-22 21:21:23 -0700342 public static final int ACTION_RESULT_OK = 0;
343 public static final int ACTION_RESULT_ERROR = 1;
344 public static final int ACTION_RESULT_ERROR_NO_CARRIER = 2;
345 public static final int ACTION_RESULT_ERROR_BUSY = 3;
346 public static final int ACTION_RESULT_ERROR_NO_ANSWER = 4;
347 public static final int ACTION_RESULT_ERROR_DELAYED = 5;
348 public static final int ACTION_RESULT_ERROR_BLACKLISTED = 6;
349 public static final int ACTION_RESULT_ERROR_CME = 7;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530350
351 /* Detailed CME error codes */
Jack He2992cd02017-08-22 21:21:23 -0700352 public static final int CME_PHONE_FAILURE = 0;
353 public static final int CME_NO_CONNECTION_TO_PHONE = 1;
354 public static final int CME_OPERATION_NOT_ALLOWED = 3;
355 public static final int CME_OPERATION_NOT_SUPPORTED = 4;
356 public static final int CME_PHSIM_PIN_REQUIRED = 5;
357 public static final int CME_PHFSIM_PIN_REQUIRED = 6;
358 public static final int CME_PHFSIM_PUK_REQUIRED = 7;
359 public static final int CME_SIM_NOT_INSERTED = 10;
360 public static final int CME_SIM_PIN_REQUIRED = 11;
361 public static final int CME_SIM_PUK_REQUIRED = 12;
362 public static final int CME_SIM_FAILURE = 13;
363 public static final int CME_SIM_BUSY = 14;
364 public static final int CME_SIM_WRONG = 15;
365 public static final int CME_INCORRECT_PASSWORD = 16;
366 public static final int CME_SIM_PIN2_REQUIRED = 17;
367 public static final int CME_SIM_PUK2_REQUIRED = 18;
368 public static final int CME_MEMORY_FULL = 20;
369 public static final int CME_INVALID_INDEX = 21;
370 public static final int CME_NOT_FOUND = 22;
371 public static final int CME_MEMORY_FAILURE = 23;
372 public static final int CME_TEXT_STRING_TOO_LONG = 24;
373 public static final int CME_INVALID_CHARACTER_IN_TEXT_STRING = 25;
374 public static final int CME_DIAL_STRING_TOO_LONG = 26;
375 public static final int CME_INVALID_CHARACTER_IN_DIAL_STRING = 27;
376 public static final int CME_NO_NETWORK_SERVICE = 30;
377 public static final int CME_NETWORK_TIMEOUT = 31;
378 public static final int CME_EMERGENCY_SERVICE_ONLY = 32;
379 public static final int CME_NO_SIMULTANOUS_VOIP_CS_CALLS = 33;
380 public static final int CME_NOT_SUPPORTED_FOR_VOIP = 34;
381 public static final int CME_SIP_RESPONSE_CODE = 35;
382 public static final int CME_NETWORK_PERSONALIZATION_PIN_REQUIRED = 40;
383 public static final int CME_NETWORK_PERSONALIZATION_PUK_REQUIRED = 41;
384 public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PIN_REQUIRED = 42;
385 public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PUK_REQUIRED = 43;
386 public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PIN_REQUIRED = 44;
387 public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PUK_REQUIRED = 45;
388 public static final int CME_CORPORATE_PERSONALIZATION_PIN_REQUIRED = 46;
389 public static final int CME_CORPORATE_PERSONALIZATION_PUK_REQUIRED = 47;
390 public static final int CME_HIDDEN_KEY_REQUIRED = 48;
391 public static final int CME_EAP_NOT_SUPPORTED = 49;
392 public static final int CME_INCORRECT_PARAMETERS = 50;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530393
394 /* Action policy for other calls when accepting call */
395 public static final int CALL_ACCEPT_NONE = 0;
396 public static final int CALL_ACCEPT_HOLD = 1;
397 public static final int CALL_ACCEPT_TERMINATE = 2;
398
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530399 private BluetoothAdapter mAdapter;
Ugo Yud0115462019-03-26 21:38:08 +0800400 private final BluetoothProfileConnector<IBluetoothHeadsetClient> mProfileConnector =
401 new BluetoothProfileConnector(this, BluetoothProfile.HEADSET_CLIENT,
402 "BluetoothHeadsetClient", IBluetoothHeadsetClient.class.getName()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530403 @Override
Ugo Yud0115462019-03-26 21:38:08 +0800404 public IBluetoothHeadsetClient getServiceInterface(IBinder service) {
405 return IBluetoothHeadsetClient.Stub.asInterface(Binder.allowBlocking(service));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530406 }
Ugo Yud0115462019-03-26 21:38:08 +0800407 };
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530408
409 /**
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700410 * Create a BluetoothHeadsetClient proxy object.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530411 */
Ugo Yud0115462019-03-26 21:38:08 +0800412 /*package*/ BluetoothHeadsetClient(Context context, ServiceListener listener) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530413 mAdapter = BluetoothAdapter.getDefaultAdapter();
Ugo Yud0115462019-03-26 21:38:08 +0800414 mProfileConnector.connect(context, listener);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530415 }
416
417 /**
418 * Close the connection to the backing service.
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700419 * Other public functions of BluetoothHeadsetClient will return default error
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530420 * results once close() has been called. Multiple invocations of close()
421 * are ok.
422 */
423 /*package*/ void close() {
424 if (VDBG) log("close()");
Ugo Yud0115462019-03-26 21:38:08 +0800425 mProfileConnector.disconnect();
426 }
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530427
Ugo Yud0115462019-03-26 21:38:08 +0800428 private IBluetoothHeadsetClient getService() {
429 return mProfileConnector.getService();
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530430 }
431
432 /**
433 * Connects to remote device.
434 *
435 * Currently, the system supports only 1 connection. So, in case of the
436 * second connection, this implementation will disconnect already connected
437 * device automatically and will process the new one.
438 *
Jack Hea355e5e2017-08-22 16:06:54 -0700439 * @param device a remote device we want connect to
440 * @return <code>true</code> if command has been issued successfully; <code>false</code>
441 * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530442 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100443 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530444 public boolean connect(BluetoothDevice device) {
445 if (DBG) log("connect(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800446 final IBluetoothHeadsetClient service =
447 getService();
Jack He16eeac32017-08-17 12:11:18 -0700448 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530449 try {
Jack He16eeac32017-08-17 12:11:18 -0700450 return service.connect(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530451 } catch (RemoteException e) {
452 Log.e(TAG, Log.getStackTraceString(new Throwable()));
453 return false;
454 }
455 }
Jack He16eeac32017-08-17 12:11:18 -0700456 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530457 return false;
458 }
459
460 /**
461 * Disconnects remote device
462 *
Jack Hea355e5e2017-08-22 16:06:54 -0700463 * @param device a remote device we want disconnect
464 * @return <code>true</code> if command has been issued successfully; <code>false</code>
465 * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530466 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100467 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530468 public boolean disconnect(BluetoothDevice device) {
469 if (DBG) log("disconnect(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800470 final IBluetoothHeadsetClient service =
471 getService();
Jack He16eeac32017-08-17 12:11:18 -0700472 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530473 try {
Jack He16eeac32017-08-17 12:11:18 -0700474 return service.disconnect(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530475 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700476 Log.e(TAG, Log.getStackTraceString(new Throwable()));
477 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530478 }
479 }
Jack He16eeac32017-08-17 12:11:18 -0700480 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530481 return false;
482 }
483
484 /**
485 * Return the list of connected remote devices
486 *
487 * @return list of connected devices; empty list if nothing is connected.
488 */
489 @Override
490 public List<BluetoothDevice> getConnectedDevices() {
491 if (VDBG) log("getConnectedDevices()");
Ugo Yud0115462019-03-26 21:38:08 +0800492 final IBluetoothHeadsetClient service =
493 getService();
Jack He16eeac32017-08-17 12:11:18 -0700494 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530495 try {
Jack He16eeac32017-08-17 12:11:18 -0700496 return service.getConnectedDevices();
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530497 } catch (RemoteException e) {
498 Log.e(TAG, Log.getStackTraceString(new Throwable()));
499 return new ArrayList<BluetoothDevice>();
500 }
501 }
Jack He16eeac32017-08-17 12:11:18 -0700502 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530503 return new ArrayList<BluetoothDevice>();
504 }
505
506 /**
507 * Returns list of remote devices in a particular state
508 *
Jack Hea355e5e2017-08-22 16:06:54 -0700509 * @param states collection of states
510 * @return list of devices that state matches the states listed in <code>states</code>; empty
511 * list if nothing matches the <code>states</code>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530512 */
513 @Override
514 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
515 if (VDBG) log("getDevicesMatchingStates()");
Ugo Yud0115462019-03-26 21:38:08 +0800516 final IBluetoothHeadsetClient service =
517 getService();
Jack He16eeac32017-08-17 12:11:18 -0700518 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530519 try {
Jack He16eeac32017-08-17 12:11:18 -0700520 return service.getDevicesMatchingConnectionStates(states);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530521 } catch (RemoteException e) {
522 Log.e(TAG, Log.getStackTraceString(new Throwable()));
523 return new ArrayList<BluetoothDevice>();
524 }
525 }
Jack He16eeac32017-08-17 12:11:18 -0700526 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530527 return new ArrayList<BluetoothDevice>();
528 }
529
530 /**
531 * Returns state of the <code>device</code>
532 *
Jack Hea355e5e2017-08-22 16:06:54 -0700533 * @param device a remote device
534 * @return the state of connection of the device
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530535 */
536 @Override
537 public int getConnectionState(BluetoothDevice device) {
538 if (VDBG) log("getConnectionState(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800539 final IBluetoothHeadsetClient service =
540 getService();
Jack He16eeac32017-08-17 12:11:18 -0700541 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530542 try {
Jack He16eeac32017-08-17 12:11:18 -0700543 return service.getConnectionState(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530544 } catch (RemoteException e) {
545 Log.e(TAG, Log.getStackTraceString(new Throwable()));
546 return BluetoothProfile.STATE_DISCONNECTED;
547 }
548 }
Jack He16eeac32017-08-17 12:11:18 -0700549 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530550 return BluetoothProfile.STATE_DISCONNECTED;
551 }
552
553 /**
554 * Set priority of the profile
555 *
556 * The device should already be paired.
557 */
558 public boolean setPriority(BluetoothDevice device, int priority) {
559 if (DBG) log("setPriority(" + device + ", " + priority + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800560 final IBluetoothHeadsetClient service =
561 getService();
Jack He16eeac32017-08-17 12:11:18 -0700562 if (service != null && isEnabled() && isValidDevice(device)) {
Jack He2992cd02017-08-22 21:21:23 -0700563 if (priority != BluetoothProfile.PRIORITY_OFF
564 && priority != BluetoothProfile.PRIORITY_ON) {
Jack Hea355e5e2017-08-22 16:06:54 -0700565 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530566 }
567 try {
Jack He16eeac32017-08-17 12:11:18 -0700568 return service.setPriority(device, priority);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530569 } catch (RemoteException e) {
570 Log.e(TAG, Log.getStackTraceString(new Throwable()));
571 return false;
572 }
573 }
Jack He16eeac32017-08-17 12:11:18 -0700574 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530575 return false;
576 }
577
578 /**
579 * Get the priority of the profile.
580 */
581 public int getPriority(BluetoothDevice device) {
582 if (VDBG) log("getPriority(" + device + ")");
Ugo Yud0115462019-03-26 21:38:08 +0800583 final IBluetoothHeadsetClient service =
584 getService();
Jack He16eeac32017-08-17 12:11:18 -0700585 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530586 try {
Jack He16eeac32017-08-17 12:11:18 -0700587 return service.getPriority(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530588 } catch (RemoteException e) {
589 Log.e(TAG, Log.getStackTraceString(new Throwable()));
590 return PRIORITY_OFF;
591 }
592 }
Jack He16eeac32017-08-17 12:11:18 -0700593 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530594 return PRIORITY_OFF;
595 }
596
597 /**
598 * Starts voice recognition.
599 *
Jack Hea355e5e2017-08-22 16:06:54 -0700600 * @param device remote device
601 * @return <code>true</code> if command has been issued successfully; <code>false</code>
602 * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530603 *
Jack Hea355e5e2017-08-22 16:06:54 -0700604 * <p>Feature required for successful execution is being reported by: {@link
605 * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
606 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530607 */
608 public boolean startVoiceRecognition(BluetoothDevice device) {
609 if (DBG) log("startVoiceRecognition()");
Ugo Yud0115462019-03-26 21:38:08 +0800610 final IBluetoothHeadsetClient service =
611 getService();
Jack He16eeac32017-08-17 12:11:18 -0700612 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530613 try {
Jack He16eeac32017-08-17 12:11:18 -0700614 return service.startVoiceRecognition(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530615 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700616 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530617 }
618 }
Jack He16eeac32017-08-17 12:11:18 -0700619 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530620 return false;
621 }
622
623 /**
Deqiang Chen2fec85d2019-06-25 14:30:55 -0700624 * Send vendor specific AT command.
625 *
626 * @param device remote device
627 * @param vendorId vendor number by Bluetooth SIG
628 * @param atCommand command to be sent. It start with + prefix and only one command at one time.
629 * @return <code>true</code> if command has been issued successfully; <code>false</code>
630 * otherwise.
631 */
632 public boolean sendVendorAtCommand(BluetoothDevice device, int vendorId,
633 String atCommand) {
634 if (DBG) log("sendVendorSpecificCommand()");
635 final IBluetoothHeadsetClient service =
636 getService();
637 if (service != null && isEnabled() && isValidDevice(device)) {
638 try {
639 return service.sendVendorAtCommand(device, vendorId, atCommand);
640 } catch (RemoteException e) {
641 Log.e(TAG, Log.getStackTraceString(new Throwable()));
642 }
643 }
644 if (service == null) Log.w(TAG, "Proxy not attached to service");
645 return false;
646 }
647
648 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530649 * Stops voice recognition.
650 *
Jack Hea355e5e2017-08-22 16:06:54 -0700651 * @param device remote device
652 * @return <code>true</code> if command has been issued successfully; <code>false</code>
653 * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530654 *
Jack Hea355e5e2017-08-22 16:06:54 -0700655 * <p>Feature required for successful execution is being reported by: {@link
656 * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
657 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530658 */
659 public boolean stopVoiceRecognition(BluetoothDevice device) {
660 if (DBG) log("stopVoiceRecognition()");
Ugo Yud0115462019-03-26 21:38:08 +0800661 final IBluetoothHeadsetClient service =
662 getService();
Jack He16eeac32017-08-17 12:11:18 -0700663 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530664 try {
Jack He16eeac32017-08-17 12:11:18 -0700665 return service.stopVoiceRecognition(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530666 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700667 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530668 }
669 }
Jack He16eeac32017-08-17 12:11:18 -0700670 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530671 return false;
672 }
673
674 /**
675 * Returns list of all calls in any state.
676 *
Jack Hea355e5e2017-08-22 16:06:54 -0700677 * @param device remote device
678 * @return list of calls; empty list if none call exists
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530679 */
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700680 public List<BluetoothHeadsetClientCall> getCurrentCalls(BluetoothDevice device) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530681 if (DBG) log("getCurrentCalls()");
Ugo Yud0115462019-03-26 21:38:08 +0800682 final IBluetoothHeadsetClient service =
683 getService();
Jack He16eeac32017-08-17 12:11:18 -0700684 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530685 try {
Jack He16eeac32017-08-17 12:11:18 -0700686 return service.getCurrentCalls(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530687 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700688 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530689 }
690 }
Jack He16eeac32017-08-17 12:11:18 -0700691 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530692 return null;
693 }
694
695 /**
696 * Returns list of current values of AG indicators.
697 *
Jack Hea355e5e2017-08-22 16:06:54 -0700698 * @param device remote device
699 * @return bundle of AG indicators; null if device is not in CONNECTED state
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530700 */
701 public Bundle getCurrentAgEvents(BluetoothDevice device) {
702 if (DBG) log("getCurrentCalls()");
Ugo Yud0115462019-03-26 21:38:08 +0800703 final IBluetoothHeadsetClient service =
704 getService();
Jack He16eeac32017-08-17 12:11:18 -0700705 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530706 try {
Jack He16eeac32017-08-17 12:11:18 -0700707 return service.getCurrentAgEvents(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530708 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700709 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530710 }
711 }
Jack He16eeac32017-08-17 12:11:18 -0700712 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530713 return null;
714 }
715
716 /**
717 * Accepts a call
718 *
Jack Hea355e5e2017-08-22 16:06:54 -0700719 * @param device remote device
720 * @param flag action policy while accepting a call. Possible values {@link #CALL_ACCEPT_NONE},
721 * {@link #CALL_ACCEPT_HOLD}, {@link #CALL_ACCEPT_TERMINATE}
722 * @return <code>true</code> if command has been issued successfully; <code>false</code>
723 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530724 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100725 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530726 public boolean acceptCall(BluetoothDevice device, int flag) {
727 if (DBG) log("acceptCall()");
Ugo Yud0115462019-03-26 21:38:08 +0800728 final IBluetoothHeadsetClient service =
729 getService();
Jack He16eeac32017-08-17 12:11:18 -0700730 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530731 try {
Jack He16eeac32017-08-17 12:11:18 -0700732 return service.acceptCall(device, flag);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530733 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700734 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530735 }
736 }
Jack He16eeac32017-08-17 12:11:18 -0700737 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530738 return false;
739 }
740
741 /**
742 * Holds a call.
743 *
Jack Hea355e5e2017-08-22 16:06:54 -0700744 * @param device remote device
745 * @return <code>true</code> if command has been issued successfully; <code>false</code>
746 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530747 */
748 public boolean holdCall(BluetoothDevice device) {
749 if (DBG) log("holdCall()");
Ugo Yud0115462019-03-26 21:38:08 +0800750 final IBluetoothHeadsetClient service =
751 getService();
Jack He16eeac32017-08-17 12:11:18 -0700752 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530753 try {
Jack He16eeac32017-08-17 12:11:18 -0700754 return service.holdCall(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530755 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700756 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530757 }
758 }
Jack He16eeac32017-08-17 12:11:18 -0700759 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530760 return false;
761 }
762
763 /**
764 * Rejects a call.
765 *
Jack Hea355e5e2017-08-22 16:06:54 -0700766 * @param device remote device
767 * @return <code>true</code> if command has been issued successfully; <code>false</code>
768 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530769 *
Jack Hea355e5e2017-08-22 16:06:54 -0700770 * <p>Feature required for successful execution is being reported by: {@link
771 * #EXTRA_AG_FEATURE_REJECT_CALL}. This method invocation will fail silently when feature is not
772 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530773 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100774 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530775 public boolean rejectCall(BluetoothDevice device) {
776 if (DBG) log("rejectCall()");
Ugo Yud0115462019-03-26 21:38:08 +0800777 final IBluetoothHeadsetClient service =
778 getService();
Jack He16eeac32017-08-17 12:11:18 -0700779 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530780 try {
Jack He16eeac32017-08-17 12:11:18 -0700781 return service.rejectCall(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530782 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700783 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530784 }
785 }
Jack He16eeac32017-08-17 12:11:18 -0700786 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530787 return false;
788 }
789
790 /**
791 * Terminates a specified call.
792 *
793 * Works only when Extended Call Control is supported by Audio Gateway.
794 *
Jack Hea355e5e2017-08-22 16:06:54 -0700795 * @param device remote device
Jack He2992cd02017-08-22 21:21:23 -0700796 * @param call Handle of call obtained in {@link #dial(BluetoothDevice, String)} or obtained via
797 * {@link #ACTION_CALL_CHANGED}. {@code call} may be null in which case we will hangup all active
Jack Hea355e5e2017-08-22 16:06:54 -0700798 * calls.
799 * @return <code>true</code> if command has been issued successfully; <code>false</code>
800 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530801 *
Jack Hea355e5e2017-08-22 16:06:54 -0700802 * <p>Feature required for successful execution is being reported by: {@link
803 * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
804 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530805 */
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700806 public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530807 if (DBG) log("terminateCall()");
Ugo Yud0115462019-03-26 21:38:08 +0800808 final IBluetoothHeadsetClient service =
809 getService();
Jack He16eeac32017-08-17 12:11:18 -0700810 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530811 try {
Jack He16eeac32017-08-17 12:11:18 -0700812 return service.terminateCall(device, call);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530813 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700814 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530815 }
816 }
Jack He16eeac32017-08-17 12:11:18 -0700817 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530818 return false;
819 }
820
821 /**
822 * Enters private mode with a specified call.
823 *
824 * Works only when Extended Call Control is supported by Audio Gateway.
825 *
Jack Hea355e5e2017-08-22 16:06:54 -0700826 * @param device remote device
827 * @param index index of the call to connect in private mode
828 * @return <code>true</code> if command has been issued successfully; <code>false</code>
829 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530830 *
Jack Hea355e5e2017-08-22 16:06:54 -0700831 * <p>Feature required for successful execution is being reported by: {@link
832 * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
833 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530834 */
835 public boolean enterPrivateMode(BluetoothDevice device, int index) {
836 if (DBG) log("enterPrivateMode()");
Ugo Yud0115462019-03-26 21:38:08 +0800837 final IBluetoothHeadsetClient service =
838 getService();
Jack He16eeac32017-08-17 12:11:18 -0700839 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530840 try {
Jack He16eeac32017-08-17 12:11:18 -0700841 return service.enterPrivateMode(device, index);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530842 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700843 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530844 }
845 }
Jack He16eeac32017-08-17 12:11:18 -0700846 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530847 return false;
848 }
849
850 /**
851 * Performs explicit call transfer.
852 *
853 * That means connect other calls and disconnect.
854 *
Jack Hea355e5e2017-08-22 16:06:54 -0700855 * @param device remote device
856 * @return <code>true</code> if command has been issued successfully; <code>false</code>
857 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530858 *
Jack Hea355e5e2017-08-22 16:06:54 -0700859 * <p>Feature required for successful execution is being reported by: {@link
860 * #EXTRA_AG_FEATURE_MERGE_AND_DETACH}. This method invocation will fail silently when feature
861 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530862 */
863 public boolean explicitCallTransfer(BluetoothDevice device) {
864 if (DBG) log("explicitCallTransfer()");
Ugo Yud0115462019-03-26 21:38:08 +0800865 final IBluetoothHeadsetClient service =
866 getService();
Jack He16eeac32017-08-17 12:11:18 -0700867 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530868 try {
Jack He16eeac32017-08-17 12:11:18 -0700869 return service.explicitCallTransfer(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530870 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700871 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530872 }
873 }
Jack He16eeac32017-08-17 12:11:18 -0700874 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530875 return false;
876 }
877
878 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530879 * Places a call with specified number.
880 *
Jack Hea355e5e2017-08-22 16:06:54 -0700881 * @param device remote device
882 * @param number valid phone number
883 * @return <code>{@link BluetoothHeadsetClientCall} call</code> if command has been issued
884 * successfully; <code>{@link null}</code> otherwise; upon completion HFP sends {@link
885 * #ACTION_CALL_CHANGED} intent in case of success; {@link #ACTION_RESULT} is sent otherwise;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530886 */
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700887 public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530888 if (DBG) log("dial()");
Ugo Yud0115462019-03-26 21:38:08 +0800889 final IBluetoothHeadsetClient service =
890 getService();
Jack He16eeac32017-08-17 12:11:18 -0700891 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530892 try {
Jack He16eeac32017-08-17 12:11:18 -0700893 return service.dial(device, number);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530894 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700895 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530896 }
897 }
Jack He16eeac32017-08-17 12:11:18 -0700898 if (service == null) Log.w(TAG, "Proxy not attached to service");
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700899 return null;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530900 }
901
902 /**
903 * Sends DTMF code.
904 *
905 * Possible code values : 0,1,2,3,4,5,6,7,8,9,A,B,C,D,*,#
906 *
Jack Hea355e5e2017-08-22 16:06:54 -0700907 * @param device remote device
908 * @param code ASCII code
909 * @return <code>true</code> if command has been issued successfully; <code>false</code>
910 * otherwise; upon completion HFP sends {@link #ACTION_RESULT} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530911 */
912 public boolean sendDTMF(BluetoothDevice device, byte code) {
913 if (DBG) log("sendDTMF()");
Ugo Yud0115462019-03-26 21:38:08 +0800914 final IBluetoothHeadsetClient service =
915 getService();
Jack He16eeac32017-08-17 12:11:18 -0700916 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530917 try {
Jack He16eeac32017-08-17 12:11:18 -0700918 return service.sendDTMF(device, code);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530919 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700920 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530921 }
922 }
Jack He16eeac32017-08-17 12:11:18 -0700923 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530924 return false;
925 }
926
927 /**
928 * Get a number corresponding to last voice tag recorded on AG.
929 *
Jack Hea355e5e2017-08-22 16:06:54 -0700930 * @param device remote device
931 * @return <code>true</code> if command has been issued successfully; <code>false</code>
932 * otherwise; upon completion HFP sends {@link #ACTION_LAST_VTAG} or {@link #ACTION_RESULT}
933 * intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530934 *
Jack Hea355e5e2017-08-22 16:06:54 -0700935 * <p>Feature required for successful execution is being reported by: {@link
936 * #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}. This method invocation will fail silently when
937 * feature is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530938 */
939 public boolean getLastVoiceTagNumber(BluetoothDevice device) {
940 if (DBG) log("getLastVoiceTagNumber()");
Ugo Yud0115462019-03-26 21:38:08 +0800941 final IBluetoothHeadsetClient service =
942 getService();
Jack He16eeac32017-08-17 12:11:18 -0700943 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530944 try {
Jack He16eeac32017-08-17 12:11:18 -0700945 return service.getLastVoiceTagNumber(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530946 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700947 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530948 }
949 }
Jack He16eeac32017-08-17 12:11:18 -0700950 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530951 return false;
952 }
953
954 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530955 * Returns current audio state of Audio Gateway.
956 *
957 * Note: This is an internal function and shouldn't be exposed
958 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100959 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530960 public int getAudioState(BluetoothDevice device) {
961 if (VDBG) log("getAudioState");
Ugo Yud0115462019-03-26 21:38:08 +0800962 final IBluetoothHeadsetClient service =
963 getService();
Jack He16eeac32017-08-17 12:11:18 -0700964 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530965 try {
Jack He16eeac32017-08-17 12:11:18 -0700966 return service.getAudioState(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700967 } catch (RemoteException e) {
968 Log.e(TAG, e.toString());
969 }
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530970 } else {
971 Log.w(TAG, "Proxy not attached to service");
972 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
973 }
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700974 return BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530975 }
976
977 /**
Bryce Leedc133822015-10-28 22:21:54 -0700978 * Sets whether audio routing is allowed.
979 *
Jack Hea355e5e2017-08-22 16:06:54 -0700980 * @param device remote device
981 * @param allowed if routing is allowed to the device Note: This is an internal function and
982 * shouldn't be exposed
Bryce Leedc133822015-10-28 22:21:54 -0700983 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -0800984 public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) {
Bryce Leedc133822015-10-28 22:21:54 -0700985 if (VDBG) log("setAudioRouteAllowed");
Ugo Yud0115462019-03-26 21:38:08 +0800986 final IBluetoothHeadsetClient service =
987 getService();
Jack He16eeac32017-08-17 12:11:18 -0700988 if (service != null && isEnabled()) {
Bryce Leedc133822015-10-28 22:21:54 -0700989 try {
Jack He16eeac32017-08-17 12:11:18 -0700990 service.setAudioRouteAllowed(device, allowed);
Jack Hea355e5e2017-08-22 16:06:54 -0700991 } catch (RemoteException e) {
992 Log.e(TAG, e.toString());
993 }
Bryce Leedc133822015-10-28 22:21:54 -0700994 } else {
995 Log.w(TAG, "Proxy not attached to service");
996 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
997 }
998 }
999
1000 /**
1001 * Returns whether audio routing is allowed.
Jack Hea355e5e2017-08-22 16:06:54 -07001002 *
1003 * @param device remote device
1004 * @return whether the command succeeded Note: This is an internal function and shouldn't be
1005 * exposed
Bryce Leedc133822015-10-28 22:21:54 -07001006 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -08001007 public boolean getAudioRouteAllowed(BluetoothDevice device) {
Bryce Leedc133822015-10-28 22:21:54 -07001008 if (VDBG) log("getAudioRouteAllowed");
Ugo Yud0115462019-03-26 21:38:08 +08001009 final IBluetoothHeadsetClient service =
1010 getService();
Jack He16eeac32017-08-17 12:11:18 -07001011 if (service != null && isEnabled()) {
Bryce Leedc133822015-10-28 22:21:54 -07001012 try {
Jack He16eeac32017-08-17 12:11:18 -07001013 return service.getAudioRouteAllowed(device);
Jack Hea355e5e2017-08-22 16:06:54 -07001014 } catch (RemoteException e) {
1015 Log.e(TAG, e.toString());
1016 }
Bryce Leedc133822015-10-28 22:21:54 -07001017 } else {
1018 Log.w(TAG, "Proxy not attached to service");
1019 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1020 }
1021 return false;
1022 }
1023
1024 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301025 * Initiates a connection of audio channel.
1026 *
1027 * It setup SCO channel with remote connected Handsfree AG device.
1028 *
Jack Hea355e5e2017-08-22 16:06:54 -07001029 * @param device remote device
1030 * @return <code>true</code> if command has been issued successfully; <code>false</code>
1031 * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301032 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -08001033 public boolean connectAudio(BluetoothDevice device) {
Ugo Yud0115462019-03-26 21:38:08 +08001034 final IBluetoothHeadsetClient service =
1035 getService();
Jack He16eeac32017-08-17 12:11:18 -07001036 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301037 try {
Jack He16eeac32017-08-17 12:11:18 -07001038 return service.connectAudio(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301039 } catch (RemoteException e) {
1040 Log.e(TAG, e.toString());
1041 }
1042 } else {
1043 Log.w(TAG, "Proxy not attached to service");
1044 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1045 }
1046 return false;
1047 }
1048
1049 /**
1050 * Disconnects audio channel.
1051 *
1052 * It tears down the SCO channel from remote AG device.
1053 *
Jack Hea355e5e2017-08-22 16:06:54 -07001054 * @param device remote device
1055 * @return <code>true</code> if command has been issued successfully; <code>false</code>
1056 * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301057 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -08001058 public boolean disconnectAudio(BluetoothDevice device) {
Ugo Yud0115462019-03-26 21:38:08 +08001059 final IBluetoothHeadsetClient service =
1060 getService();
Jack He16eeac32017-08-17 12:11:18 -07001061 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301062 try {
Jack He16eeac32017-08-17 12:11:18 -07001063 return service.disconnectAudio(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301064 } catch (RemoteException e) {
1065 Log.e(TAG, e.toString());
1066 }
1067 } else {
1068 Log.w(TAG, "Proxy not attached to service");
1069 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1070 }
1071 return false;
1072 }
1073
1074 /**
1075 * Get Audio Gateway features
1076 *
Jack Hea355e5e2017-08-22 16:06:54 -07001077 * @param device remote device
1078 * @return bundle of AG features; null if no service or AG not connected
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301079 */
1080 public Bundle getCurrentAgFeatures(BluetoothDevice device) {
Ugo Yud0115462019-03-26 21:38:08 +08001081 final IBluetoothHeadsetClient service =
1082 getService();
Jack He16eeac32017-08-17 12:11:18 -07001083 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301084 try {
Jack He16eeac32017-08-17 12:11:18 -07001085 return service.getCurrentAgFeatures(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301086 } catch (RemoteException e) {
1087 Log.e(TAG, e.toString());
1088 }
1089 } else {
1090 Log.w(TAG, "Proxy not attached to service");
1091 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1092 }
1093 return null;
1094 }
1095
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301096 private boolean isEnabled() {
Jack He16eeac32017-08-17 12:11:18 -07001097 return mAdapter.getState() == BluetoothAdapter.STATE_ON;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301098 }
1099
Jack He16eeac32017-08-17 12:11:18 -07001100 private static boolean isValidDevice(BluetoothDevice device) {
1101 return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301102 }
1103
1104 private static void log(String msg) {
1105 Log.d(TAG, msg);
1106 }
1107}