blob: 549c1faddd907c4c8ce5870485840b0dd35b53a0 [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 Inwood7acad5e2018-08-01 15:00:35 +010019import android.annotation.UnsupportedAppUsage;
Hemant Gupta7aca90f2013-08-19 19:03:51 +053020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
Jeff Sharkey0a17db12016-11-04 11:23:46 -060024import android.os.Binder;
Hemant Gupta7aca90f2013-08-19 19:03:51 +053025import android.os.Bundle;
26import android.os.IBinder;
27import android.os.RemoteException;
jovanak2a9131f2018-10-15 17:50:19 -070028import android.os.UserHandle;
Hemant Gupta7aca90f2013-08-19 19:03:51 +053029import android.util.Log;
30
31import java.util.ArrayList;
32import java.util.List;
33
34/**
35 * Public API to control Hands Free Profile (HFP role only).
36 * <p>
37 * This class defines methods that shall be used by application to manage profile
38 * connection, calls states and calls actions.
39 * <p>
40 *
41 * @hide
Jack Hea355e5e2017-08-22 16:06:54 -070042 */
Mike Lockwoodcf916d32014-06-12 11:23:40 -070043public final class BluetoothHeadsetClient implements BluetoothProfile {
44 private static final String TAG = "BluetoothHeadsetClient";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053045 private static final boolean DBG = true;
46 private static final boolean VDBG = false;
47
48 /**
49 * Intent sent whenever connection to remote changes.
50 *
51 * <p>It includes two extras:
52 * <code>BluetoothProfile.EXTRA_PREVIOUS_STATE</code>
53 * and <code>BluetoothProfile.EXTRA_STATE</code>, which
54 * are mandatory.
55 * <p>There are also non mandatory feature extras:
56 * {@link #EXTRA_AG_FEATURE_3WAY_CALLING},
57 * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION},
58 * {@link #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT},
59 * {@link #EXTRA_AG_FEATURE_REJECT_CALL},
60 * {@link #EXTRA_AG_FEATURE_ECC},
61 * {@link #EXTRA_AG_FEATURE_RESPONSE_AND_HOLD},
62 * {@link #EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL},
63 * {@link #EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL},
64 * {@link #EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT},
65 * {@link #EXTRA_AG_FEATURE_MERGE},
66 * {@link #EXTRA_AG_FEATURE_MERGE_AND_DETACH},
67 * sent as boolean values only when <code>EXTRA_STATE</code>
68 * is set to <code>STATE_CONNECTED</code>.</p>
69 *
70 * <p>Note that features supported by AG are being sent as
71 * booleans with value <code>true</code>,
72 * and not supported ones are <strong>not</strong> being sent at all.</p>
73 */
74 public static final String ACTION_CONNECTION_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070075 "android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053076
77 /**
78 * Intent sent whenever audio state changes.
79 *
80 * <p>It includes two mandatory extras:
Jack He16eeac32017-08-17 12:11:18 -070081 * {@link BluetoothProfile#EXTRA_STATE},
82 * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE},
Hemant Gupta7aca90f2013-08-19 19:03:51 +053083 * with possible values:
84 * {@link #STATE_AUDIO_CONNECTING},
85 * {@link #STATE_AUDIO_CONNECTED},
86 * {@link #STATE_AUDIO_DISCONNECTED}</p>
87 * <p>When <code>EXTRA_STATE</code> is set
88 * to </code>STATE_AUDIO_CONNECTED</code>,
89 * it also includes {@link #EXTRA_AUDIO_WBS}
90 * indicating wide band speech support.</p>
91 */
92 public static final String ACTION_AUDIO_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070093 "android.bluetooth.headsetclient.profile.action.AUDIO_STATE_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053094
95 /**
96 * Intent sending updates of the Audio Gateway state.
97 * Each extra is being sent only when value it
98 * represents has been changed recently on AG.
99 * <p>It can contain one or more of the following extras:
100 * {@link #EXTRA_NETWORK_STATUS},
101 * {@link #EXTRA_NETWORK_SIGNAL_STRENGTH},
102 * {@link #EXTRA_NETWORK_ROAMING},
103 * {@link #EXTRA_BATTERY_LEVEL},
104 * {@link #EXTRA_OPERATOR_NAME},
105 * {@link #EXTRA_VOICE_RECOGNITION},
106 * {@link #EXTRA_IN_BAND_RING}</p>
107 */
108 public static final String ACTION_AG_EVENT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700109 "android.bluetooth.headsetclient.profile.action.AG_EVENT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530110
111 /**
112 * Intent sent whenever state of a call changes.
113 *
114 * <p>It includes:
115 * {@link #EXTRA_CALL},
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700116 * with value of {@link BluetoothHeadsetClientCall} instance,
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530117 * representing actual call state.</p>
118 */
119 public static final String ACTION_CALL_CHANGED =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700120 "android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530121
122 /**
123 * Intent that notifies about the result of the last issued action.
124 * Please note that not every action results in explicit action result code being sent.
125 * Instead other notifications about new Audio Gateway state might be sent,
126 * like <code>ACTION_AG_EVENT</code> with <code>EXTRA_VOICE_RECOGNITION</code> value
127 * when for example user started voice recognition from HF unit.
128 */
129 public static final String ACTION_RESULT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700130 "android.bluetooth.headsetclient.profile.action.RESULT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530131
132 /**
133 * Intent that notifies about the number attached to the last voice tag
134 * recorded on AG.
135 *
136 * <p>It contains:
137 * {@link #EXTRA_NUMBER},
138 * with a <code>String</code> value representing phone number.</p>
139 */
140 public static final String ACTION_LAST_VTAG =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700141 "android.bluetooth.headsetclient.profile.action.LAST_VTAG";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530142
143 public static final int STATE_AUDIO_DISCONNECTED = 0;
144 public static final int STATE_AUDIO_CONNECTING = 1;
145 public static final int STATE_AUDIO_CONNECTED = 2;
146
147 /**
148 * Extra with information if connected audio is WBS.
149 * <p>Possible values: <code>true</code>,
Jack Hea355e5e2017-08-22 16:06:54 -0700150 * <code>false</code>.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530151 */
152 public static final String EXTRA_AUDIO_WBS =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700153 "android.bluetooth.headsetclient.extra.AUDIO_WBS";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530154
155 /**
156 * Extra for AG_EVENT indicates network status.
157 * <p>Value: 0 - network unavailable,
Jack Hea355e5e2017-08-22 16:06:54 -0700158 * 1 - network available </p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530159 */
160 public static final String EXTRA_NETWORK_STATUS =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700161 "android.bluetooth.headsetclient.extra.NETWORK_STATUS";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530162 /**
163 * Extra for AG_EVENT intent indicates network signal strength.
164 * <p>Value: <code>Integer</code> representing signal strength.</p>
165 */
166 public static final String EXTRA_NETWORK_SIGNAL_STRENGTH =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700167 "android.bluetooth.headsetclient.extra.NETWORK_SIGNAL_STRENGTH";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530168 /**
169 * Extra for AG_EVENT intent indicates roaming state.
170 * <p>Value: 0 - no roaming
Jack Hea355e5e2017-08-22 16:06:54 -0700171 * 1 - active roaming</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530172 */
173 public static final String EXTRA_NETWORK_ROAMING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700174 "android.bluetooth.headsetclient.extra.NETWORK_ROAMING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530175 /**
176 * Extra for AG_EVENT intent indicates the battery level.
177 * <p>Value: <code>Integer</code> representing signal strength.</p>
178 */
179 public static final String EXTRA_BATTERY_LEVEL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700180 "android.bluetooth.headsetclient.extra.BATTERY_LEVEL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530181 /**
182 * Extra for AG_EVENT intent indicates operator name.
183 * <p>Value: <code>String</code> representing operator name.</p>
184 */
185 public static final String EXTRA_OPERATOR_NAME =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700186 "android.bluetooth.headsetclient.extra.OPERATOR_NAME";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530187 /**
188 * Extra for AG_EVENT intent indicates voice recognition state.
189 * <p>Value:
Jack Hea355e5e2017-08-22 16:06:54 -0700190 * 0 - voice recognition stopped,
191 * 1 - voice recognition started.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530192 */
193 public static final String EXTRA_VOICE_RECOGNITION =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700194 "android.bluetooth.headsetclient.extra.VOICE_RECOGNITION";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530195 /**
196 * Extra for AG_EVENT intent indicates in band ring state.
197 * <p>Value:
Jack Hea355e5e2017-08-22 16:06:54 -0700198 * 0 - in band ring tone not supported, or
199 * 1 - in band ring tone supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530200 */
201 public static final String EXTRA_IN_BAND_RING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700202 "android.bluetooth.headsetclient.extra.IN_BAND_RING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530203
204 /**
205 * Extra for AG_EVENT intent indicates subscriber info.
206 * <p>Value: <code>String</code> containing subscriber information.</p>
207 */
208 public static final String EXTRA_SUBSCRIBER_INFO =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700209 "android.bluetooth.headsetclient.extra.SUBSCRIBER_INFO";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530210
211 /**
Jack Hea355e5e2017-08-22 16:06:54 -0700212 * Extra for AG_CALL_CHANGED intent indicates the
213 * {@link BluetoothHeadsetClientCall} object that has changed.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530214 */
215 public static final String EXTRA_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700216 "android.bluetooth.headsetclient.extra.CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530217
218 /**
219 * Extra for ACTION_LAST_VTAG intent.
220 * <p>Value: <code>String</code> representing phone number
221 * corresponding to last voice tag recorded on AG</p>
222 */
223 public static final String EXTRA_NUMBER =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700224 "android.bluetooth.headsetclient.extra.NUMBER";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530225
226 /**
227 * Extra for ACTION_RESULT intent that shows the result code of
228 * last issued action.
229 * <p>Possible results:
230 * {@link #ACTION_RESULT_OK},
231 * {@link #ACTION_RESULT_ERROR},
232 * {@link #ACTION_RESULT_ERROR_NO_CARRIER},
233 * {@link #ACTION_RESULT_ERROR_BUSY},
234 * {@link #ACTION_RESULT_ERROR_NO_ANSWER},
235 * {@link #ACTION_RESULT_ERROR_DELAYED},
236 * {@link #ACTION_RESULT_ERROR_BLACKLISTED},
237 * {@link #ACTION_RESULT_ERROR_CME}</p>
238 */
239 public static final String EXTRA_RESULT_CODE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700240 "android.bluetooth.headsetclient.extra.RESULT_CODE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530241
242 /**
243 * Extra for ACTION_RESULT intent that shows the extended result code of
244 * last issued action.
245 * <p>Value: <code>Integer</code> - error code.</p>
246 */
247 public static final String EXTRA_CME_CODE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700248 "android.bluetooth.headsetclient.extra.CME_CODE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530249
250 /* Extras for AG_FEATURES, extras type is boolean */
251 // TODO verify if all of those are actually useful
252 /**
253 * AG feature: three way calling.
254 */
Jack He2992cd02017-08-22 21:21:23 -0700255 public static final String EXTRA_AG_FEATURE_3WAY_CALLING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700256 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_3WAY_CALLING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530257 /**
258 * AG feature: voice recognition.
259 */
Jack He2992cd02017-08-22 21:21:23 -0700260 public static final String EXTRA_AG_FEATURE_VOICE_RECOGNITION =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700261 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_VOICE_RECOGNITION";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530262 /**
263 * AG feature: fetching phone number for voice tagging procedure.
264 */
Jack He2992cd02017-08-22 21:21:23 -0700265 public static final String EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700266 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530267 /**
268 * AG feature: ability to reject incoming call.
269 */
Jack He2992cd02017-08-22 21:21:23 -0700270 public static final String EXTRA_AG_FEATURE_REJECT_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700271 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_REJECT_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530272 /**
273 * AG feature: enhanced call handling (terminate specific call, private consultation).
274 */
Jack He2992cd02017-08-22 21:21:23 -0700275 public static final String EXTRA_AG_FEATURE_ECC =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700276 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ECC";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530277 /**
278 * AG feature: response and hold.
279 */
Jack He2992cd02017-08-22 21:21:23 -0700280 public static final String EXTRA_AG_FEATURE_RESPONSE_AND_HOLD =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700281 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RESPONSE_AND_HOLD";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530282 /**
283 * AG call handling feature: accept held or waiting call in three way calling scenarios.
284 */
Jack He2992cd02017-08-22 21:21:23 -0700285 public static final String EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700286 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530287 /**
288 * AG call handling feature: release held or waiting call in three way calling scenarios.
289 */
Jack He2992cd02017-08-22 21:21:23 -0700290 public static final String EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700291 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530292 /**
293 * AG call handling feature: release active call and accept held or waiting call in three way
294 * calling scenarios.
295 */
Jack He2992cd02017-08-22 21:21:23 -0700296 public static final String EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700297 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530298 /**
299 * AG call handling feature: merge two calls, held and active - multi party conference mode.
300 */
Jack He2992cd02017-08-22 21:21:23 -0700301 public static final String EXTRA_AG_FEATURE_MERGE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700302 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530303 /**
304 * AG call handling feature: merge calls and disconnect from multi party
305 * conversation leaving peers connected to each other.
306 * Note that this feature needs to be supported by mobile network operator
307 * as it requires connection and billing transfer.
308 */
Jack He2992cd02017-08-22 21:21:23 -0700309 public static final String EXTRA_AG_FEATURE_MERGE_AND_DETACH =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700310 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE_AND_DETACH";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530311
312 /* Action result codes */
Jack He2992cd02017-08-22 21:21:23 -0700313 public static final int ACTION_RESULT_OK = 0;
314 public static final int ACTION_RESULT_ERROR = 1;
315 public static final int ACTION_RESULT_ERROR_NO_CARRIER = 2;
316 public static final int ACTION_RESULT_ERROR_BUSY = 3;
317 public static final int ACTION_RESULT_ERROR_NO_ANSWER = 4;
318 public static final int ACTION_RESULT_ERROR_DELAYED = 5;
319 public static final int ACTION_RESULT_ERROR_BLACKLISTED = 6;
320 public static final int ACTION_RESULT_ERROR_CME = 7;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530321
322 /* Detailed CME error codes */
Jack He2992cd02017-08-22 21:21:23 -0700323 public static final int CME_PHONE_FAILURE = 0;
324 public static final int CME_NO_CONNECTION_TO_PHONE = 1;
325 public static final int CME_OPERATION_NOT_ALLOWED = 3;
326 public static final int CME_OPERATION_NOT_SUPPORTED = 4;
327 public static final int CME_PHSIM_PIN_REQUIRED = 5;
328 public static final int CME_PHFSIM_PIN_REQUIRED = 6;
329 public static final int CME_PHFSIM_PUK_REQUIRED = 7;
330 public static final int CME_SIM_NOT_INSERTED = 10;
331 public static final int CME_SIM_PIN_REQUIRED = 11;
332 public static final int CME_SIM_PUK_REQUIRED = 12;
333 public static final int CME_SIM_FAILURE = 13;
334 public static final int CME_SIM_BUSY = 14;
335 public static final int CME_SIM_WRONG = 15;
336 public static final int CME_INCORRECT_PASSWORD = 16;
337 public static final int CME_SIM_PIN2_REQUIRED = 17;
338 public static final int CME_SIM_PUK2_REQUIRED = 18;
339 public static final int CME_MEMORY_FULL = 20;
340 public static final int CME_INVALID_INDEX = 21;
341 public static final int CME_NOT_FOUND = 22;
342 public static final int CME_MEMORY_FAILURE = 23;
343 public static final int CME_TEXT_STRING_TOO_LONG = 24;
344 public static final int CME_INVALID_CHARACTER_IN_TEXT_STRING = 25;
345 public static final int CME_DIAL_STRING_TOO_LONG = 26;
346 public static final int CME_INVALID_CHARACTER_IN_DIAL_STRING = 27;
347 public static final int CME_NO_NETWORK_SERVICE = 30;
348 public static final int CME_NETWORK_TIMEOUT = 31;
349 public static final int CME_EMERGENCY_SERVICE_ONLY = 32;
350 public static final int CME_NO_SIMULTANOUS_VOIP_CS_CALLS = 33;
351 public static final int CME_NOT_SUPPORTED_FOR_VOIP = 34;
352 public static final int CME_SIP_RESPONSE_CODE = 35;
353 public static final int CME_NETWORK_PERSONALIZATION_PIN_REQUIRED = 40;
354 public static final int CME_NETWORK_PERSONALIZATION_PUK_REQUIRED = 41;
355 public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PIN_REQUIRED = 42;
356 public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PUK_REQUIRED = 43;
357 public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PIN_REQUIRED = 44;
358 public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PUK_REQUIRED = 45;
359 public static final int CME_CORPORATE_PERSONALIZATION_PIN_REQUIRED = 46;
360 public static final int CME_CORPORATE_PERSONALIZATION_PUK_REQUIRED = 47;
361 public static final int CME_HIDDEN_KEY_REQUIRED = 48;
362 public static final int CME_EAP_NOT_SUPPORTED = 49;
363 public static final int CME_INCORRECT_PARAMETERS = 50;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530364
365 /* Action policy for other calls when accepting call */
366 public static final int CALL_ACCEPT_NONE = 0;
367 public static final int CALL_ACCEPT_HOLD = 1;
368 public static final int CALL_ACCEPT_TERMINATE = 2;
369
370 private Context mContext;
371 private ServiceListener mServiceListener;
Jack He16eeac32017-08-17 12:11:18 -0700372 private volatile IBluetoothHeadsetClient mService;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530373 private BluetoothAdapter mAdapter;
374
Jack He2992cd02017-08-22 21:21:23 -0700375 private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530376 new IBluetoothStateChangeCallback.Stub() {
377 @Override
378 public void onBluetoothStateChange(boolean up) {
379 if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
380 if (!up) {
Jack Hea355e5e2017-08-22 16:06:54 -0700381 if (VDBG) Log.d(TAG, "Unbinding service...");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530382 synchronized (mConnection) {
383 try {
384 mService = null;
385 mContext.unbindService(mConnection);
386 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700387 Log.e(TAG, "", re);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530388 }
389 }
390 } else {
391 synchronized (mConnection) {
392 try {
393 if (mService == null) {
Jack Hea355e5e2017-08-22 16:06:54 -0700394 if (VDBG) Log.d(TAG, "Binding service...");
395 Intent intent = new Intent(
396 IBluetoothHeadsetClient.class.getName());
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700397 doBind();
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530398 }
399 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700400 Log.e(TAG, "", re);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530401 }
402 }
403 }
404 }
Jack Hea355e5e2017-08-22 16:06:54 -0700405 };
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530406
407 /**
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700408 * Create a BluetoothHeadsetClient proxy object.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530409 */
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700410 /*package*/ BluetoothHeadsetClient(Context context, ServiceListener l) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530411 mContext = context;
412 mServiceListener = l;
413 mAdapter = BluetoothAdapter.getDefaultAdapter();
414
415 IBluetoothManager mgr = mAdapter.getBluetoothManager();
416 if (mgr != null) {
417 try {
418 mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
419 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700420 Log.e(TAG, "", e);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530421 }
422 }
423
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700424 doBind();
425 }
426
427 boolean doBind() {
428 Intent intent = new Intent(IBluetoothHeadsetClient.class.getName());
429 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
430 intent.setComponent(comp);
431 if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
jovanak2a9131f2018-10-15 17:50:19 -0700432 UserHandle.CURRENT_OR_SELF)) {
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700433 Log.e(TAG, "Could not bind to Bluetooth Headset Client Service with " + intent);
434 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530435 }
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700436 return true;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530437 }
438
439 /**
440 * Close the connection to the backing service.
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700441 * Other public functions of BluetoothHeadsetClient will return default error
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530442 * results once close() has been called. Multiple invocations of close()
443 * are ok.
444 */
445 /*package*/ void close() {
446 if (VDBG) log("close()");
447
448 IBluetoothManager mgr = mAdapter.getBluetoothManager();
449 if (mgr != null) {
450 try {
451 mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
452 } catch (Exception e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700453 Log.e(TAG, "", e);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530454 }
455 }
456
457 synchronized (mConnection) {
458 if (mService != null) {
459 try {
460 mService = null;
461 mContext.unbindService(mConnection);
462 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700463 Log.e(TAG, "", re);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530464 }
465 }
466 }
467 mServiceListener = null;
468 }
469
470 /**
471 * Connects to remote device.
472 *
473 * Currently, the system supports only 1 connection. So, in case of the
474 * second connection, this implementation will disconnect already connected
475 * device automatically and will process the new one.
476 *
Jack Hea355e5e2017-08-22 16:06:54 -0700477 * @param device a remote device we want connect to
478 * @return <code>true</code> if command has been issued successfully; <code>false</code>
479 * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530480 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +0100481 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530482 public boolean connect(BluetoothDevice device) {
483 if (DBG) log("connect(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700484 final IBluetoothHeadsetClient service = mService;
485 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530486 try {
Jack He16eeac32017-08-17 12:11:18 -0700487 return service.connect(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530488 } catch (RemoteException e) {
489 Log.e(TAG, Log.getStackTraceString(new Throwable()));
490 return false;
491 }
492 }
Jack He16eeac32017-08-17 12:11:18 -0700493 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530494 return false;
495 }
496
497 /**
498 * Disconnects remote device
499 *
Jack Hea355e5e2017-08-22 16:06:54 -0700500 * @param device a remote device we want disconnect
501 * @return <code>true</code> if command has been issued successfully; <code>false</code>
502 * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530503 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +0100504 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530505 public boolean disconnect(BluetoothDevice device) {
506 if (DBG) log("disconnect(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700507 final IBluetoothHeadsetClient service = mService;
508 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530509 try {
Jack He16eeac32017-08-17 12:11:18 -0700510 return service.disconnect(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530511 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700512 Log.e(TAG, Log.getStackTraceString(new Throwable()));
513 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530514 }
515 }
Jack He16eeac32017-08-17 12:11:18 -0700516 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530517 return false;
518 }
519
520 /**
521 * Return the list of connected remote devices
522 *
523 * @return list of connected devices; empty list if nothing is connected.
524 */
525 @Override
526 public List<BluetoothDevice> getConnectedDevices() {
527 if (VDBG) log("getConnectedDevices()");
Jack He16eeac32017-08-17 12:11:18 -0700528 final IBluetoothHeadsetClient service = mService;
529 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530530 try {
Jack He16eeac32017-08-17 12:11:18 -0700531 return service.getConnectedDevices();
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530532 } catch (RemoteException e) {
533 Log.e(TAG, Log.getStackTraceString(new Throwable()));
534 return new ArrayList<BluetoothDevice>();
535 }
536 }
Jack He16eeac32017-08-17 12:11:18 -0700537 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530538 return new ArrayList<BluetoothDevice>();
539 }
540
541 /**
542 * Returns list of remote devices in a particular state
543 *
Jack Hea355e5e2017-08-22 16:06:54 -0700544 * @param states collection of states
545 * @return list of devices that state matches the states listed in <code>states</code>; empty
546 * list if nothing matches the <code>states</code>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530547 */
548 @Override
549 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
550 if (VDBG) log("getDevicesMatchingStates()");
Jack He16eeac32017-08-17 12:11:18 -0700551 final IBluetoothHeadsetClient service = mService;
552 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530553 try {
Jack He16eeac32017-08-17 12:11:18 -0700554 return service.getDevicesMatchingConnectionStates(states);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530555 } catch (RemoteException e) {
556 Log.e(TAG, Log.getStackTraceString(new Throwable()));
557 return new ArrayList<BluetoothDevice>();
558 }
559 }
Jack He16eeac32017-08-17 12:11:18 -0700560 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530561 return new ArrayList<BluetoothDevice>();
562 }
563
564 /**
565 * Returns state of the <code>device</code>
566 *
Jack Hea355e5e2017-08-22 16:06:54 -0700567 * @param device a remote device
568 * @return the state of connection of the device
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530569 */
570 @Override
571 public int getConnectionState(BluetoothDevice device) {
572 if (VDBG) log("getConnectionState(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700573 final IBluetoothHeadsetClient service = mService;
574 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530575 try {
Jack He16eeac32017-08-17 12:11:18 -0700576 return service.getConnectionState(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530577 } catch (RemoteException e) {
578 Log.e(TAG, Log.getStackTraceString(new Throwable()));
579 return BluetoothProfile.STATE_DISCONNECTED;
580 }
581 }
Jack He16eeac32017-08-17 12:11:18 -0700582 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530583 return BluetoothProfile.STATE_DISCONNECTED;
584 }
585
586 /**
587 * Set priority of the profile
588 *
589 * The device should already be paired.
590 */
591 public boolean setPriority(BluetoothDevice device, int priority) {
592 if (DBG) log("setPriority(" + device + ", " + priority + ")");
Jack He16eeac32017-08-17 12:11:18 -0700593 final IBluetoothHeadsetClient service = mService;
594 if (service != null && isEnabled() && isValidDevice(device)) {
Jack He2992cd02017-08-22 21:21:23 -0700595 if (priority != BluetoothProfile.PRIORITY_OFF
596 && priority != BluetoothProfile.PRIORITY_ON) {
Jack Hea355e5e2017-08-22 16:06:54 -0700597 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530598 }
599 try {
Jack He16eeac32017-08-17 12:11:18 -0700600 return service.setPriority(device, priority);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530601 } catch (RemoteException e) {
602 Log.e(TAG, Log.getStackTraceString(new Throwable()));
603 return false;
604 }
605 }
Jack He16eeac32017-08-17 12:11:18 -0700606 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530607 return false;
608 }
609
610 /**
611 * Get the priority of the profile.
612 */
613 public int getPriority(BluetoothDevice device) {
614 if (VDBG) log("getPriority(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700615 final IBluetoothHeadsetClient service = mService;
616 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530617 try {
Jack He16eeac32017-08-17 12:11:18 -0700618 return service.getPriority(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530619 } catch (RemoteException e) {
620 Log.e(TAG, Log.getStackTraceString(new Throwable()));
621 return PRIORITY_OFF;
622 }
623 }
Jack He16eeac32017-08-17 12:11:18 -0700624 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530625 return PRIORITY_OFF;
626 }
627
628 /**
629 * Starts voice recognition.
630 *
Jack Hea355e5e2017-08-22 16:06:54 -0700631 * @param device remote device
632 * @return <code>true</code> if command has been issued successfully; <code>false</code>
633 * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530634 *
Jack Hea355e5e2017-08-22 16:06:54 -0700635 * <p>Feature required for successful execution is being reported by: {@link
636 * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
637 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530638 */
639 public boolean startVoiceRecognition(BluetoothDevice device) {
640 if (DBG) log("startVoiceRecognition()");
Jack He16eeac32017-08-17 12:11:18 -0700641 final IBluetoothHeadsetClient service = mService;
642 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530643 try {
Jack He16eeac32017-08-17 12:11:18 -0700644 return service.startVoiceRecognition(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530645 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700646 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530647 }
648 }
Jack He16eeac32017-08-17 12:11:18 -0700649 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530650 return false;
651 }
652
653 /**
654 * Stops voice recognition.
655 *
Jack Hea355e5e2017-08-22 16:06:54 -0700656 * @param device remote device
657 * @return <code>true</code> if command has been issued successfully; <code>false</code>
658 * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530659 *
Jack Hea355e5e2017-08-22 16:06:54 -0700660 * <p>Feature required for successful execution is being reported by: {@link
661 * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
662 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530663 */
664 public boolean stopVoiceRecognition(BluetoothDevice device) {
665 if (DBG) log("stopVoiceRecognition()");
Jack He16eeac32017-08-17 12:11:18 -0700666 final IBluetoothHeadsetClient service = mService;
667 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530668 try {
Jack He16eeac32017-08-17 12:11:18 -0700669 return service.stopVoiceRecognition(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530670 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700671 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530672 }
673 }
Jack He16eeac32017-08-17 12:11:18 -0700674 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530675 return false;
676 }
677
678 /**
679 * Returns list of all calls in any state.
680 *
Jack Hea355e5e2017-08-22 16:06:54 -0700681 * @param device remote device
682 * @return list of calls; empty list if none call exists
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530683 */
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700684 public List<BluetoothHeadsetClientCall> getCurrentCalls(BluetoothDevice device) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530685 if (DBG) log("getCurrentCalls()");
Jack He16eeac32017-08-17 12:11:18 -0700686 final IBluetoothHeadsetClient service = mService;
687 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530688 try {
Jack He16eeac32017-08-17 12:11:18 -0700689 return service.getCurrentCalls(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530690 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700691 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530692 }
693 }
Jack He16eeac32017-08-17 12:11:18 -0700694 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530695 return null;
696 }
697
698 /**
699 * Returns list of current values of AG indicators.
700 *
Jack Hea355e5e2017-08-22 16:06:54 -0700701 * @param device remote device
702 * @return bundle of AG indicators; null if device is not in CONNECTED state
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530703 */
704 public Bundle getCurrentAgEvents(BluetoothDevice device) {
705 if (DBG) log("getCurrentCalls()");
Jack He16eeac32017-08-17 12:11:18 -0700706 final IBluetoothHeadsetClient service = mService;
707 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530708 try {
Jack He16eeac32017-08-17 12:11:18 -0700709 return service.getCurrentAgEvents(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530710 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700711 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530712 }
713 }
Jack He16eeac32017-08-17 12:11:18 -0700714 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530715 return null;
716 }
717
718 /**
719 * Accepts a call
720 *
Jack Hea355e5e2017-08-22 16:06:54 -0700721 * @param device remote device
722 * @param flag action policy while accepting a call. Possible values {@link #CALL_ACCEPT_NONE},
723 * {@link #CALL_ACCEPT_HOLD}, {@link #CALL_ACCEPT_TERMINATE}
724 * @return <code>true</code> if command has been issued successfully; <code>false</code>
725 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530726 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +0100727 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530728 public boolean acceptCall(BluetoothDevice device, int flag) {
729 if (DBG) log("acceptCall()");
Jack He16eeac32017-08-17 12:11:18 -0700730 final IBluetoothHeadsetClient service = mService;
731 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530732 try {
Jack He16eeac32017-08-17 12:11:18 -0700733 return service.acceptCall(device, flag);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530734 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700735 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530736 }
737 }
Jack He16eeac32017-08-17 12:11:18 -0700738 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530739 return false;
740 }
741
742 /**
743 * Holds a call.
744 *
Jack Hea355e5e2017-08-22 16:06:54 -0700745 * @param device remote device
746 * @return <code>true</code> if command has been issued successfully; <code>false</code>
747 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530748 */
749 public boolean holdCall(BluetoothDevice device) {
750 if (DBG) log("holdCall()");
Jack He16eeac32017-08-17 12:11:18 -0700751 final IBluetoothHeadsetClient service = mService;
752 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 Inwood7acad5e2018-08-01 15:00:35 +0100774 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530775 public boolean rejectCall(BluetoothDevice device) {
776 if (DBG) log("rejectCall()");
Jack He16eeac32017-08-17 12:11:18 -0700777 final IBluetoothHeadsetClient service = mService;
778 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530779 try {
Jack He16eeac32017-08-17 12:11:18 -0700780 return service.rejectCall(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530781 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700782 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530783 }
784 }
Jack He16eeac32017-08-17 12:11:18 -0700785 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530786 return false;
787 }
788
789 /**
790 * Terminates a specified call.
791 *
792 * Works only when Extended Call Control is supported by Audio Gateway.
793 *
Jack Hea355e5e2017-08-22 16:06:54 -0700794 * @param device remote device
Jack He2992cd02017-08-22 21:21:23 -0700795 * @param call Handle of call obtained in {@link #dial(BluetoothDevice, String)} or obtained via
796 * {@link #ACTION_CALL_CHANGED}. {@code call} may be null in which case we will hangup all active
Jack Hea355e5e2017-08-22 16:06:54 -0700797 * calls.
798 * @return <code>true</code> if command has been issued successfully; <code>false</code>
799 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530800 *
Jack Hea355e5e2017-08-22 16:06:54 -0700801 * <p>Feature required for successful execution is being reported by: {@link
802 * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
803 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530804 */
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700805 public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530806 if (DBG) log("terminateCall()");
Jack He16eeac32017-08-17 12:11:18 -0700807 final IBluetoothHeadsetClient service = mService;
808 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530809 try {
Jack He16eeac32017-08-17 12:11:18 -0700810 return service.terminateCall(device, call);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530811 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700812 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530813 }
814 }
Jack He16eeac32017-08-17 12:11:18 -0700815 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530816 return false;
817 }
818
819 /**
820 * Enters private mode with a specified call.
821 *
822 * Works only when Extended Call Control is supported by Audio Gateway.
823 *
Jack Hea355e5e2017-08-22 16:06:54 -0700824 * @param device remote device
825 * @param index index of the call to connect in private mode
826 * @return <code>true</code> if command has been issued successfully; <code>false</code>
827 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530828 *
Jack Hea355e5e2017-08-22 16:06:54 -0700829 * <p>Feature required for successful execution is being reported by: {@link
830 * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
831 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530832 */
833 public boolean enterPrivateMode(BluetoothDevice device, int index) {
834 if (DBG) log("enterPrivateMode()");
Jack He16eeac32017-08-17 12:11:18 -0700835 final IBluetoothHeadsetClient service = mService;
836 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530837 try {
Jack He16eeac32017-08-17 12:11:18 -0700838 return service.enterPrivateMode(device, index);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530839 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700840 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530841 }
842 }
Jack He16eeac32017-08-17 12:11:18 -0700843 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530844 return false;
845 }
846
847 /**
848 * Performs explicit call transfer.
849 *
850 * That means connect other calls and disconnect.
851 *
Jack Hea355e5e2017-08-22 16:06:54 -0700852 * @param device remote device
853 * @return <code>true</code> if command has been issued successfully; <code>false</code>
854 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530855 *
Jack Hea355e5e2017-08-22 16:06:54 -0700856 * <p>Feature required for successful execution is being reported by: {@link
857 * #EXTRA_AG_FEATURE_MERGE_AND_DETACH}. This method invocation will fail silently when feature
858 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530859 */
860 public boolean explicitCallTransfer(BluetoothDevice device) {
861 if (DBG) log("explicitCallTransfer()");
Jack He16eeac32017-08-17 12:11:18 -0700862 final IBluetoothHeadsetClient service = mService;
863 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530864 try {
Jack He16eeac32017-08-17 12:11:18 -0700865 return service.explicitCallTransfer(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530866 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700867 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530868 }
869 }
Jack He16eeac32017-08-17 12:11:18 -0700870 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530871 return false;
872 }
873
874 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530875 * Places a call with specified number.
876 *
Jack Hea355e5e2017-08-22 16:06:54 -0700877 * @param device remote device
878 * @param number valid phone number
879 * @return <code>{@link BluetoothHeadsetClientCall} call</code> if command has been issued
880 * successfully; <code>{@link null}</code> otherwise; upon completion HFP sends {@link
881 * #ACTION_CALL_CHANGED} intent in case of success; {@link #ACTION_RESULT} is sent otherwise;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530882 */
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700883 public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530884 if (DBG) log("dial()");
Jack He16eeac32017-08-17 12:11:18 -0700885 final IBluetoothHeadsetClient service = mService;
886 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530887 try {
Jack He16eeac32017-08-17 12:11:18 -0700888 return service.dial(device, number);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530889 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700890 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530891 }
892 }
Jack He16eeac32017-08-17 12:11:18 -0700893 if (service == null) Log.w(TAG, "Proxy not attached to service");
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700894 return null;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530895 }
896
897 /**
898 * Sends DTMF code.
899 *
900 * Possible code values : 0,1,2,3,4,5,6,7,8,9,A,B,C,D,*,#
901 *
Jack Hea355e5e2017-08-22 16:06:54 -0700902 * @param device remote device
903 * @param code ASCII code
904 * @return <code>true</code> if command has been issued successfully; <code>false</code>
905 * otherwise; upon completion HFP sends {@link #ACTION_RESULT} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530906 */
907 public boolean sendDTMF(BluetoothDevice device, byte code) {
908 if (DBG) log("sendDTMF()");
Jack He16eeac32017-08-17 12:11:18 -0700909 final IBluetoothHeadsetClient service = mService;
910 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530911 try {
Jack He16eeac32017-08-17 12:11:18 -0700912 return service.sendDTMF(device, code);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530913 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700914 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530915 }
916 }
Jack He16eeac32017-08-17 12:11:18 -0700917 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530918 return false;
919 }
920
921 /**
922 * Get a number corresponding to last voice tag recorded on AG.
923 *
Jack Hea355e5e2017-08-22 16:06:54 -0700924 * @param device remote device
925 * @return <code>true</code> if command has been issued successfully; <code>false</code>
926 * otherwise; upon completion HFP sends {@link #ACTION_LAST_VTAG} or {@link #ACTION_RESULT}
927 * intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530928 *
Jack Hea355e5e2017-08-22 16:06:54 -0700929 * <p>Feature required for successful execution is being reported by: {@link
930 * #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}. This method invocation will fail silently when
931 * feature is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530932 */
933 public boolean getLastVoiceTagNumber(BluetoothDevice device) {
934 if (DBG) log("getLastVoiceTagNumber()");
Jack He16eeac32017-08-17 12:11:18 -0700935 final IBluetoothHeadsetClient service = mService;
936 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530937 try {
Jack He16eeac32017-08-17 12:11:18 -0700938 return service.getLastVoiceTagNumber(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530939 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700940 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530941 }
942 }
Jack He16eeac32017-08-17 12:11:18 -0700943 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530944 return false;
945 }
946
947 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530948 * Returns current audio state of Audio Gateway.
949 *
950 * Note: This is an internal function and shouldn't be exposed
951 */
Mathew Inwood7acad5e2018-08-01 15:00:35 +0100952 @UnsupportedAppUsage
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530953 public int getAudioState(BluetoothDevice device) {
954 if (VDBG) log("getAudioState");
Jack He16eeac32017-08-17 12:11:18 -0700955 final IBluetoothHeadsetClient service = mService;
956 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530957 try {
Jack He16eeac32017-08-17 12:11:18 -0700958 return service.getAudioState(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700959 } catch (RemoteException e) {
960 Log.e(TAG, e.toString());
961 }
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530962 } else {
963 Log.w(TAG, "Proxy not attached to service");
964 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
965 }
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700966 return BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530967 }
968
969 /**
Bryce Leedc133822015-10-28 22:21:54 -0700970 * Sets whether audio routing is allowed.
971 *
Jack Hea355e5e2017-08-22 16:06:54 -0700972 * @param device remote device
973 * @param allowed if routing is allowed to the device Note: This is an internal function and
974 * shouldn't be exposed
Bryce Leedc133822015-10-28 22:21:54 -0700975 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -0800976 public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) {
Bryce Leedc133822015-10-28 22:21:54 -0700977 if (VDBG) log("setAudioRouteAllowed");
Jack He16eeac32017-08-17 12:11:18 -0700978 final IBluetoothHeadsetClient service = mService;
979 if (service != null && isEnabled()) {
Bryce Leedc133822015-10-28 22:21:54 -0700980 try {
Jack He16eeac32017-08-17 12:11:18 -0700981 service.setAudioRouteAllowed(device, allowed);
Jack Hea355e5e2017-08-22 16:06:54 -0700982 } catch (RemoteException e) {
983 Log.e(TAG, e.toString());
984 }
Bryce Leedc133822015-10-28 22:21:54 -0700985 } else {
986 Log.w(TAG, "Proxy not attached to service");
987 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
988 }
989 }
990
991 /**
992 * Returns whether audio routing is allowed.
Jack Hea355e5e2017-08-22 16:06:54 -0700993 *
994 * @param device remote device
995 * @return whether the command succeeded Note: This is an internal function and shouldn't be
996 * exposed
Bryce Leedc133822015-10-28 22:21:54 -0700997 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -0800998 public boolean getAudioRouteAllowed(BluetoothDevice device) {
Bryce Leedc133822015-10-28 22:21:54 -0700999 if (VDBG) log("getAudioRouteAllowed");
Jack He16eeac32017-08-17 12:11:18 -07001000 final IBluetoothHeadsetClient service = mService;
1001 if (service != null && isEnabled()) {
Bryce Leedc133822015-10-28 22:21:54 -07001002 try {
Jack He16eeac32017-08-17 12:11:18 -07001003 return service.getAudioRouteAllowed(device);
Jack Hea355e5e2017-08-22 16:06:54 -07001004 } catch (RemoteException e) {
1005 Log.e(TAG, e.toString());
1006 }
Bryce Leedc133822015-10-28 22:21:54 -07001007 } else {
1008 Log.w(TAG, "Proxy not attached to service");
1009 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1010 }
1011 return false;
1012 }
1013
1014 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301015 * Initiates a connection of audio channel.
1016 *
1017 * It setup SCO channel with remote connected Handsfree AG device.
1018 *
Jack Hea355e5e2017-08-22 16:06:54 -07001019 * @param device remote device
1020 * @return <code>true</code> if command has been issued successfully; <code>false</code>
1021 * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301022 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -08001023 public boolean connectAudio(BluetoothDevice device) {
Jack He16eeac32017-08-17 12:11:18 -07001024 final IBluetoothHeadsetClient service = mService;
1025 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301026 try {
Jack He16eeac32017-08-17 12:11:18 -07001027 return service.connectAudio(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301028 } catch (RemoteException e) {
1029 Log.e(TAG, e.toString());
1030 }
1031 } else {
1032 Log.w(TAG, "Proxy not attached to service");
1033 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1034 }
1035 return false;
1036 }
1037
1038 /**
1039 * Disconnects audio channel.
1040 *
1041 * It tears down the SCO channel from remote AG device.
1042 *
Jack Hea355e5e2017-08-22 16:06:54 -07001043 * @param device remote device
1044 * @return <code>true</code> if command has been issued successfully; <code>false</code>
1045 * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301046 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -08001047 public boolean disconnectAudio(BluetoothDevice device) {
Jack He16eeac32017-08-17 12:11:18 -07001048 final IBluetoothHeadsetClient service = mService;
1049 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301050 try {
Jack He16eeac32017-08-17 12:11:18 -07001051 return service.disconnectAudio(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301052 } catch (RemoteException e) {
1053 Log.e(TAG, e.toString());
1054 }
1055 } else {
1056 Log.w(TAG, "Proxy not attached to service");
1057 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1058 }
1059 return false;
1060 }
1061
1062 /**
1063 * Get Audio Gateway features
1064 *
Jack Hea355e5e2017-08-22 16:06:54 -07001065 * @param device remote device
1066 * @return bundle of AG features; null if no service or AG not connected
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301067 */
1068 public Bundle getCurrentAgFeatures(BluetoothDevice device) {
Jack He16eeac32017-08-17 12:11:18 -07001069 final IBluetoothHeadsetClient service = mService;
1070 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301071 try {
Jack He16eeac32017-08-17 12:11:18 -07001072 return service.getCurrentAgFeatures(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301073 } catch (RemoteException e) {
1074 Log.e(TAG, e.toString());
1075 }
1076 } else {
1077 Log.w(TAG, "Proxy not attached to service");
1078 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1079 }
1080 return null;
1081 }
1082
1083
Jack He16eeac32017-08-17 12:11:18 -07001084 private final ServiceConnection mConnection = new ServiceConnection() {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301085 @Override
1086 public void onServiceConnected(ComponentName className, IBinder service) {
1087 if (DBG) Log.d(TAG, "Proxy object connected");
Jeff Sharkey0a17db12016-11-04 11:23:46 -06001088 mService = IBluetoothHeadsetClient.Stub.asInterface(Binder.allowBlocking(service));
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301089
1090 if (mServiceListener != null) {
Mike Lockwoodcf916d32014-06-12 11:23:40 -07001091 mServiceListener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT,
1092 BluetoothHeadsetClient.this);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301093 }
1094 }
Jack Hea355e5e2017-08-22 16:06:54 -07001095
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301096 @Override
1097 public void onServiceDisconnected(ComponentName className) {
1098 if (DBG) Log.d(TAG, "Proxy object disconnected");
1099 mService = null;
1100 if (mServiceListener != null) {
Mike Lockwoodcf916d32014-06-12 11:23:40 -07001101 mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET_CLIENT);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301102 }
1103 }
1104 };
1105
1106 private boolean isEnabled() {
Jack He16eeac32017-08-17 12:11:18 -07001107 return mAdapter.getState() == BluetoothAdapter.STATE_ON;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301108 }
1109
Jack He16eeac32017-08-17 12:11:18 -07001110 private static boolean isValidDevice(BluetoothDevice device) {
1111 return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301112 }
1113
1114 private static void log(String msg) {
1115 Log.d(TAG, msg);
1116 }
1117}