blob: 031287f5ee1236d09cf39d5884aa87cd7d070b2f [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
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Jeff Sharkey0a17db12016-11-04 11:23:46 -060023import android.os.Binder;
Hemant Gupta7aca90f2013-08-19 19:03:51 +053024import android.os.Bundle;
25import android.os.IBinder;
26import android.os.RemoteException;
27import android.util.Log;
28
29import java.util.ArrayList;
30import java.util.List;
31
32/**
33 * Public API to control Hands Free Profile (HFP role only).
34 * <p>
35 * This class defines methods that shall be used by application to manage profile
36 * connection, calls states and calls actions.
37 * <p>
38 *
39 * @hide
Jack Hea355e5e2017-08-22 16:06:54 -070040 */
Mike Lockwoodcf916d32014-06-12 11:23:40 -070041public final class BluetoothHeadsetClient implements BluetoothProfile {
42 private static final String TAG = "BluetoothHeadsetClient";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053043 private static final boolean DBG = true;
44 private static final boolean VDBG = false;
45
46 /**
47 * Intent sent whenever connection to remote changes.
48 *
49 * <p>It includes two extras:
50 * <code>BluetoothProfile.EXTRA_PREVIOUS_STATE</code>
51 * and <code>BluetoothProfile.EXTRA_STATE</code>, which
52 * are mandatory.
53 * <p>There are also non mandatory feature extras:
54 * {@link #EXTRA_AG_FEATURE_3WAY_CALLING},
55 * {@link #EXTRA_AG_FEATURE_VOICE_RECOGNITION},
56 * {@link #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT},
57 * {@link #EXTRA_AG_FEATURE_REJECT_CALL},
58 * {@link #EXTRA_AG_FEATURE_ECC},
59 * {@link #EXTRA_AG_FEATURE_RESPONSE_AND_HOLD},
60 * {@link #EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL},
61 * {@link #EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL},
62 * {@link #EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT},
63 * {@link #EXTRA_AG_FEATURE_MERGE},
64 * {@link #EXTRA_AG_FEATURE_MERGE_AND_DETACH},
65 * sent as boolean values only when <code>EXTRA_STATE</code>
66 * is set to <code>STATE_CONNECTED</code>.</p>
67 *
68 * <p>Note that features supported by AG are being sent as
69 * booleans with value <code>true</code>,
70 * and not supported ones are <strong>not</strong> being sent at all.</p>
71 */
72 public static final String ACTION_CONNECTION_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070073 "android.bluetooth.headsetclient.profile.action.CONNECTION_STATE_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053074
75 /**
76 * Intent sent whenever audio state changes.
77 *
78 * <p>It includes two mandatory extras:
Jack He16eeac32017-08-17 12:11:18 -070079 * {@link BluetoothProfile#EXTRA_STATE},
80 * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE},
Hemant Gupta7aca90f2013-08-19 19:03:51 +053081 * with possible values:
82 * {@link #STATE_AUDIO_CONNECTING},
83 * {@link #STATE_AUDIO_CONNECTED},
84 * {@link #STATE_AUDIO_DISCONNECTED}</p>
85 * <p>When <code>EXTRA_STATE</code> is set
86 * to </code>STATE_AUDIO_CONNECTED</code>,
87 * it also includes {@link #EXTRA_AUDIO_WBS}
88 * indicating wide band speech support.</p>
89 */
90 public static final String ACTION_AUDIO_STATE_CHANGED =
Jack Hea355e5e2017-08-22 16:06:54 -070091 "android.bluetooth.headsetclient.profile.action.AUDIO_STATE_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +053092
93 /**
94 * Intent sending updates of the Audio Gateway state.
95 * Each extra is being sent only when value it
96 * represents has been changed recently on AG.
97 * <p>It can contain one or more of the following extras:
98 * {@link #EXTRA_NETWORK_STATUS},
99 * {@link #EXTRA_NETWORK_SIGNAL_STRENGTH},
100 * {@link #EXTRA_NETWORK_ROAMING},
101 * {@link #EXTRA_BATTERY_LEVEL},
102 * {@link #EXTRA_OPERATOR_NAME},
103 * {@link #EXTRA_VOICE_RECOGNITION},
104 * {@link #EXTRA_IN_BAND_RING}</p>
105 */
106 public static final String ACTION_AG_EVENT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700107 "android.bluetooth.headsetclient.profile.action.AG_EVENT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530108
109 /**
110 * Intent sent whenever state of a call changes.
111 *
112 * <p>It includes:
113 * {@link #EXTRA_CALL},
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700114 * with value of {@link BluetoothHeadsetClientCall} instance,
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530115 * representing actual call state.</p>
116 */
117 public static final String ACTION_CALL_CHANGED =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700118 "android.bluetooth.headsetclient.profile.action.AG_CALL_CHANGED";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530119
120 /**
121 * Intent that notifies about the result of the last issued action.
122 * Please note that not every action results in explicit action result code being sent.
123 * Instead other notifications about new Audio Gateway state might be sent,
124 * like <code>ACTION_AG_EVENT</code> with <code>EXTRA_VOICE_RECOGNITION</code> value
125 * when for example user started voice recognition from HF unit.
126 */
127 public static final String ACTION_RESULT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700128 "android.bluetooth.headsetclient.profile.action.RESULT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530129
130 /**
131 * Intent that notifies about the number attached to the last voice tag
132 * recorded on AG.
133 *
134 * <p>It contains:
135 * {@link #EXTRA_NUMBER},
136 * with a <code>String</code> value representing phone number.</p>
137 */
138 public static final String ACTION_LAST_VTAG =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700139 "android.bluetooth.headsetclient.profile.action.LAST_VTAG";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530140
141 public static final int STATE_AUDIO_DISCONNECTED = 0;
142 public static final int STATE_AUDIO_CONNECTING = 1;
143 public static final int STATE_AUDIO_CONNECTED = 2;
144
145 /**
146 * Extra with information if connected audio is WBS.
147 * <p>Possible values: <code>true</code>,
Jack Hea355e5e2017-08-22 16:06:54 -0700148 * <code>false</code>.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530149 */
150 public static final String EXTRA_AUDIO_WBS =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700151 "android.bluetooth.headsetclient.extra.AUDIO_WBS";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530152
153 /**
154 * Extra for AG_EVENT indicates network status.
155 * <p>Value: 0 - network unavailable,
Jack Hea355e5e2017-08-22 16:06:54 -0700156 * 1 - network available </p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530157 */
158 public static final String EXTRA_NETWORK_STATUS =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700159 "android.bluetooth.headsetclient.extra.NETWORK_STATUS";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530160 /**
161 * Extra for AG_EVENT intent indicates network signal strength.
162 * <p>Value: <code>Integer</code> representing signal strength.</p>
163 */
164 public static final String EXTRA_NETWORK_SIGNAL_STRENGTH =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700165 "android.bluetooth.headsetclient.extra.NETWORK_SIGNAL_STRENGTH";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530166 /**
167 * Extra for AG_EVENT intent indicates roaming state.
168 * <p>Value: 0 - no roaming
Jack Hea355e5e2017-08-22 16:06:54 -0700169 * 1 - active roaming</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530170 */
171 public static final String EXTRA_NETWORK_ROAMING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700172 "android.bluetooth.headsetclient.extra.NETWORK_ROAMING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530173 /**
174 * Extra for AG_EVENT intent indicates the battery level.
175 * <p>Value: <code>Integer</code> representing signal strength.</p>
176 */
177 public static final String EXTRA_BATTERY_LEVEL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700178 "android.bluetooth.headsetclient.extra.BATTERY_LEVEL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530179 /**
180 * Extra for AG_EVENT intent indicates operator name.
181 * <p>Value: <code>String</code> representing operator name.</p>
182 */
183 public static final String EXTRA_OPERATOR_NAME =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700184 "android.bluetooth.headsetclient.extra.OPERATOR_NAME";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530185 /**
186 * Extra for AG_EVENT intent indicates voice recognition state.
187 * <p>Value:
Jack Hea355e5e2017-08-22 16:06:54 -0700188 * 0 - voice recognition stopped,
189 * 1 - voice recognition started.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530190 */
191 public static final String EXTRA_VOICE_RECOGNITION =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700192 "android.bluetooth.headsetclient.extra.VOICE_RECOGNITION";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530193 /**
194 * Extra for AG_EVENT intent indicates in band ring state.
195 * <p>Value:
Jack Hea355e5e2017-08-22 16:06:54 -0700196 * 0 - in band ring tone not supported, or
197 * 1 - in band ring tone supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530198 */
199 public static final String EXTRA_IN_BAND_RING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700200 "android.bluetooth.headsetclient.extra.IN_BAND_RING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530201
202 /**
203 * Extra for AG_EVENT intent indicates subscriber info.
204 * <p>Value: <code>String</code> containing subscriber information.</p>
205 */
206 public static final String EXTRA_SUBSCRIBER_INFO =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700207 "android.bluetooth.headsetclient.extra.SUBSCRIBER_INFO";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530208
209 /**
Jack Hea355e5e2017-08-22 16:06:54 -0700210 * Extra for AG_CALL_CHANGED intent indicates the
211 * {@link BluetoothHeadsetClientCall} object that has changed.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530212 */
213 public static final String EXTRA_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700214 "android.bluetooth.headsetclient.extra.CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530215
216 /**
217 * Extra for ACTION_LAST_VTAG intent.
218 * <p>Value: <code>String</code> representing phone number
219 * corresponding to last voice tag recorded on AG</p>
220 */
221 public static final String EXTRA_NUMBER =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700222 "android.bluetooth.headsetclient.extra.NUMBER";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530223
224 /**
225 * Extra for ACTION_RESULT intent that shows the result code of
226 * last issued action.
227 * <p>Possible results:
228 * {@link #ACTION_RESULT_OK},
229 * {@link #ACTION_RESULT_ERROR},
230 * {@link #ACTION_RESULT_ERROR_NO_CARRIER},
231 * {@link #ACTION_RESULT_ERROR_BUSY},
232 * {@link #ACTION_RESULT_ERROR_NO_ANSWER},
233 * {@link #ACTION_RESULT_ERROR_DELAYED},
234 * {@link #ACTION_RESULT_ERROR_BLACKLISTED},
235 * {@link #ACTION_RESULT_ERROR_CME}</p>
236 */
237 public static final String EXTRA_RESULT_CODE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700238 "android.bluetooth.headsetclient.extra.RESULT_CODE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530239
240 /**
241 * Extra for ACTION_RESULT intent that shows the extended result code of
242 * last issued action.
243 * <p>Value: <code>Integer</code> - error code.</p>
244 */
245 public static final String EXTRA_CME_CODE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700246 "android.bluetooth.headsetclient.extra.CME_CODE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530247
248 /* Extras for AG_FEATURES, extras type is boolean */
249 // TODO verify if all of those are actually useful
250 /**
251 * AG feature: three way calling.
252 */
Jack He2992cd02017-08-22 21:21:23 -0700253 public static final String EXTRA_AG_FEATURE_3WAY_CALLING =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700254 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_3WAY_CALLING";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530255 /**
256 * AG feature: voice recognition.
257 */
Jack He2992cd02017-08-22 21:21:23 -0700258 public static final String EXTRA_AG_FEATURE_VOICE_RECOGNITION =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700259 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_VOICE_RECOGNITION";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530260 /**
261 * AG feature: fetching phone number for voice tagging procedure.
262 */
Jack He2992cd02017-08-22 21:21:23 -0700263 public static final String EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700264 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530265 /**
266 * AG feature: ability to reject incoming call.
267 */
Jack He2992cd02017-08-22 21:21:23 -0700268 public static final String EXTRA_AG_FEATURE_REJECT_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700269 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_REJECT_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530270 /**
271 * AG feature: enhanced call handling (terminate specific call, private consultation).
272 */
Jack He2992cd02017-08-22 21:21:23 -0700273 public static final String EXTRA_AG_FEATURE_ECC =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700274 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ECC";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530275 /**
276 * AG feature: response and hold.
277 */
Jack He2992cd02017-08-22 21:21:23 -0700278 public static final String EXTRA_AG_FEATURE_RESPONSE_AND_HOLD =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700279 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RESPONSE_AND_HOLD";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530280 /**
281 * AG call handling feature: accept held or waiting call in three way calling scenarios.
282 */
Jack He2992cd02017-08-22 21:21:23 -0700283 public static final String EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700284 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_ACCEPT_HELD_OR_WAITING_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530285 /**
286 * AG call handling feature: release held or waiting call in three way calling scenarios.
287 */
Jack He2992cd02017-08-22 21:21:23 -0700288 public static final String EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700289 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_HELD_OR_WAITING_CALL";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530290 /**
291 * AG call handling feature: release active call and accept held or waiting call in three way
292 * calling scenarios.
293 */
Jack He2992cd02017-08-22 21:21:23 -0700294 public static final String EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700295 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_RELEASE_AND_ACCEPT";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530296 /**
297 * AG call handling feature: merge two calls, held and active - multi party conference mode.
298 */
Jack He2992cd02017-08-22 21:21:23 -0700299 public static final String EXTRA_AG_FEATURE_MERGE =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700300 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530301 /**
302 * AG call handling feature: merge calls and disconnect from multi party
303 * conversation leaving peers connected to each other.
304 * Note that this feature needs to be supported by mobile network operator
305 * as it requires connection and billing transfer.
306 */
Jack He2992cd02017-08-22 21:21:23 -0700307 public static final String EXTRA_AG_FEATURE_MERGE_AND_DETACH =
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700308 "android.bluetooth.headsetclient.extra.EXTRA_AG_FEATURE_MERGE_AND_DETACH";
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530309
310 /* Action result codes */
Jack He2992cd02017-08-22 21:21:23 -0700311 public static final int ACTION_RESULT_OK = 0;
312 public static final int ACTION_RESULT_ERROR = 1;
313 public static final int ACTION_RESULT_ERROR_NO_CARRIER = 2;
314 public static final int ACTION_RESULT_ERROR_BUSY = 3;
315 public static final int ACTION_RESULT_ERROR_NO_ANSWER = 4;
316 public static final int ACTION_RESULT_ERROR_DELAYED = 5;
317 public static final int ACTION_RESULT_ERROR_BLACKLISTED = 6;
318 public static final int ACTION_RESULT_ERROR_CME = 7;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530319
320 /* Detailed CME error codes */
Jack He2992cd02017-08-22 21:21:23 -0700321 public static final int CME_PHONE_FAILURE = 0;
322 public static final int CME_NO_CONNECTION_TO_PHONE = 1;
323 public static final int CME_OPERATION_NOT_ALLOWED = 3;
324 public static final int CME_OPERATION_NOT_SUPPORTED = 4;
325 public static final int CME_PHSIM_PIN_REQUIRED = 5;
326 public static final int CME_PHFSIM_PIN_REQUIRED = 6;
327 public static final int CME_PHFSIM_PUK_REQUIRED = 7;
328 public static final int CME_SIM_NOT_INSERTED = 10;
329 public static final int CME_SIM_PIN_REQUIRED = 11;
330 public static final int CME_SIM_PUK_REQUIRED = 12;
331 public static final int CME_SIM_FAILURE = 13;
332 public static final int CME_SIM_BUSY = 14;
333 public static final int CME_SIM_WRONG = 15;
334 public static final int CME_INCORRECT_PASSWORD = 16;
335 public static final int CME_SIM_PIN2_REQUIRED = 17;
336 public static final int CME_SIM_PUK2_REQUIRED = 18;
337 public static final int CME_MEMORY_FULL = 20;
338 public static final int CME_INVALID_INDEX = 21;
339 public static final int CME_NOT_FOUND = 22;
340 public static final int CME_MEMORY_FAILURE = 23;
341 public static final int CME_TEXT_STRING_TOO_LONG = 24;
342 public static final int CME_INVALID_CHARACTER_IN_TEXT_STRING = 25;
343 public static final int CME_DIAL_STRING_TOO_LONG = 26;
344 public static final int CME_INVALID_CHARACTER_IN_DIAL_STRING = 27;
345 public static final int CME_NO_NETWORK_SERVICE = 30;
346 public static final int CME_NETWORK_TIMEOUT = 31;
347 public static final int CME_EMERGENCY_SERVICE_ONLY = 32;
348 public static final int CME_NO_SIMULTANOUS_VOIP_CS_CALLS = 33;
349 public static final int CME_NOT_SUPPORTED_FOR_VOIP = 34;
350 public static final int CME_SIP_RESPONSE_CODE = 35;
351 public static final int CME_NETWORK_PERSONALIZATION_PIN_REQUIRED = 40;
352 public static final int CME_NETWORK_PERSONALIZATION_PUK_REQUIRED = 41;
353 public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PIN_REQUIRED = 42;
354 public static final int CME_NETWORK_SUBSET_PERSONALIZATION_PUK_REQUIRED = 43;
355 public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PIN_REQUIRED = 44;
356 public static final int CME_SERVICE_PROVIDER_PERSONALIZATION_PUK_REQUIRED = 45;
357 public static final int CME_CORPORATE_PERSONALIZATION_PIN_REQUIRED = 46;
358 public static final int CME_CORPORATE_PERSONALIZATION_PUK_REQUIRED = 47;
359 public static final int CME_HIDDEN_KEY_REQUIRED = 48;
360 public static final int CME_EAP_NOT_SUPPORTED = 49;
361 public static final int CME_INCORRECT_PARAMETERS = 50;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530362
363 /* Action policy for other calls when accepting call */
364 public static final int CALL_ACCEPT_NONE = 0;
365 public static final int CALL_ACCEPT_HOLD = 1;
366 public static final int CALL_ACCEPT_TERMINATE = 2;
367
368 private Context mContext;
369 private ServiceListener mServiceListener;
Jack He16eeac32017-08-17 12:11:18 -0700370 private volatile IBluetoothHeadsetClient mService;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530371 private BluetoothAdapter mAdapter;
372
Jack He2992cd02017-08-22 21:21:23 -0700373 private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530374 new IBluetoothStateChangeCallback.Stub() {
375 @Override
376 public void onBluetoothStateChange(boolean up) {
377 if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
378 if (!up) {
Jack Hea355e5e2017-08-22 16:06:54 -0700379 if (VDBG) Log.d(TAG, "Unbinding service...");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530380 synchronized (mConnection) {
381 try {
382 mService = null;
383 mContext.unbindService(mConnection);
384 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700385 Log.e(TAG, "", re);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530386 }
387 }
388 } else {
389 synchronized (mConnection) {
390 try {
391 if (mService == null) {
Jack Hea355e5e2017-08-22 16:06:54 -0700392 if (VDBG) Log.d(TAG, "Binding service...");
393 Intent intent = new Intent(
394 IBluetoothHeadsetClient.class.getName());
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700395 doBind();
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530396 }
397 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700398 Log.e(TAG, "", re);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530399 }
400 }
401 }
402 }
Jack Hea355e5e2017-08-22 16:06:54 -0700403 };
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530404
405 /**
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700406 * Create a BluetoothHeadsetClient proxy object.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530407 */
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700408 /*package*/ BluetoothHeadsetClient(Context context, ServiceListener l) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530409 mContext = context;
410 mServiceListener = l;
411 mAdapter = BluetoothAdapter.getDefaultAdapter();
412
413 IBluetoothManager mgr = mAdapter.getBluetoothManager();
414 if (mgr != null) {
415 try {
416 mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
417 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700418 Log.e(TAG, "", e);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530419 }
420 }
421
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700422 doBind();
423 }
424
425 boolean doBind() {
426 Intent intent = new Intent(IBluetoothHeadsetClient.class.getName());
427 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
428 intent.setComponent(comp);
429 if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
Jack Hea355e5e2017-08-22 16:06:54 -0700430 android.os.Process.myUserHandle())) {
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700431 Log.e(TAG, "Could not bind to Bluetooth Headset Client Service with " + intent);
432 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530433 }
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700434 return true;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530435 }
436
437 /**
438 * Close the connection to the backing service.
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700439 * Other public functions of BluetoothHeadsetClient will return default error
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530440 * results once close() has been called. Multiple invocations of close()
441 * are ok.
442 */
443 /*package*/ void close() {
444 if (VDBG) log("close()");
445
446 IBluetoothManager mgr = mAdapter.getBluetoothManager();
447 if (mgr != null) {
448 try {
449 mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
450 } catch (Exception e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700451 Log.e(TAG, "", e);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530452 }
453 }
454
455 synchronized (mConnection) {
456 if (mService != null) {
457 try {
458 mService = null;
459 mContext.unbindService(mConnection);
460 } catch (Exception re) {
Jack Hea355e5e2017-08-22 16:06:54 -0700461 Log.e(TAG, "", re);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530462 }
463 }
464 }
465 mServiceListener = null;
466 }
467
468 /**
469 * Connects to remote device.
470 *
471 * Currently, the system supports only 1 connection. So, in case of the
472 * second connection, this implementation will disconnect already connected
473 * device automatically and will process the new one.
474 *
Jack Hea355e5e2017-08-22 16:06:54 -0700475 * @param device a remote device we want connect to
476 * @return <code>true</code> if command has been issued successfully; <code>false</code>
477 * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530478 */
479 public boolean connect(BluetoothDevice device) {
480 if (DBG) log("connect(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700481 final IBluetoothHeadsetClient service = mService;
482 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530483 try {
Jack He16eeac32017-08-17 12:11:18 -0700484 return service.connect(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530485 } catch (RemoteException e) {
486 Log.e(TAG, Log.getStackTraceString(new Throwable()));
487 return false;
488 }
489 }
Jack He16eeac32017-08-17 12:11:18 -0700490 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530491 return false;
492 }
493
494 /**
495 * Disconnects remote device
496 *
Jack Hea355e5e2017-08-22 16:06:54 -0700497 * @param device a remote device we want disconnect
498 * @return <code>true</code> if command has been issued successfully; <code>false</code>
499 * otherwise; upon completion HFP sends {@link #ACTION_CONNECTION_STATE_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530500 */
501 public boolean disconnect(BluetoothDevice device) {
502 if (DBG) log("disconnect(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700503 final IBluetoothHeadsetClient service = mService;
504 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530505 try {
Jack He16eeac32017-08-17 12:11:18 -0700506 return service.disconnect(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530507 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700508 Log.e(TAG, Log.getStackTraceString(new Throwable()));
509 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530510 }
511 }
Jack He16eeac32017-08-17 12:11:18 -0700512 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530513 return false;
514 }
515
516 /**
517 * Return the list of connected remote devices
518 *
519 * @return list of connected devices; empty list if nothing is connected.
520 */
521 @Override
522 public List<BluetoothDevice> getConnectedDevices() {
523 if (VDBG) log("getConnectedDevices()");
Jack He16eeac32017-08-17 12:11:18 -0700524 final IBluetoothHeadsetClient service = mService;
525 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530526 try {
Jack He16eeac32017-08-17 12:11:18 -0700527 return service.getConnectedDevices();
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530528 } catch (RemoteException e) {
529 Log.e(TAG, Log.getStackTraceString(new Throwable()));
530 return new ArrayList<BluetoothDevice>();
531 }
532 }
Jack He16eeac32017-08-17 12:11:18 -0700533 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530534 return new ArrayList<BluetoothDevice>();
535 }
536
537 /**
538 * Returns list of remote devices in a particular state
539 *
Jack Hea355e5e2017-08-22 16:06:54 -0700540 * @param states collection of states
541 * @return list of devices that state matches the states listed in <code>states</code>; empty
542 * list if nothing matches the <code>states</code>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530543 */
544 @Override
545 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
546 if (VDBG) log("getDevicesMatchingStates()");
Jack He16eeac32017-08-17 12:11:18 -0700547 final IBluetoothHeadsetClient service = mService;
548 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530549 try {
Jack He16eeac32017-08-17 12:11:18 -0700550 return service.getDevicesMatchingConnectionStates(states);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530551 } catch (RemoteException e) {
552 Log.e(TAG, Log.getStackTraceString(new Throwable()));
553 return new ArrayList<BluetoothDevice>();
554 }
555 }
Jack He16eeac32017-08-17 12:11:18 -0700556 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530557 return new ArrayList<BluetoothDevice>();
558 }
559
560 /**
561 * Returns state of the <code>device</code>
562 *
Jack Hea355e5e2017-08-22 16:06:54 -0700563 * @param device a remote device
564 * @return the state of connection of the device
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530565 */
566 @Override
567 public int getConnectionState(BluetoothDevice device) {
568 if (VDBG) log("getConnectionState(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700569 final IBluetoothHeadsetClient service = mService;
570 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530571 try {
Jack He16eeac32017-08-17 12:11:18 -0700572 return service.getConnectionState(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530573 } catch (RemoteException e) {
574 Log.e(TAG, Log.getStackTraceString(new Throwable()));
575 return BluetoothProfile.STATE_DISCONNECTED;
576 }
577 }
Jack He16eeac32017-08-17 12:11:18 -0700578 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530579 return BluetoothProfile.STATE_DISCONNECTED;
580 }
581
582 /**
583 * Set priority of the profile
584 *
585 * The device should already be paired.
586 */
587 public boolean setPriority(BluetoothDevice device, int priority) {
588 if (DBG) log("setPriority(" + device + ", " + priority + ")");
Jack He16eeac32017-08-17 12:11:18 -0700589 final IBluetoothHeadsetClient service = mService;
590 if (service != null && isEnabled() && isValidDevice(device)) {
Jack He2992cd02017-08-22 21:21:23 -0700591 if (priority != BluetoothProfile.PRIORITY_OFF
592 && priority != BluetoothProfile.PRIORITY_ON) {
Jack Hea355e5e2017-08-22 16:06:54 -0700593 return false;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530594 }
595 try {
Jack He16eeac32017-08-17 12:11:18 -0700596 return service.setPriority(device, priority);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530597 } catch (RemoteException e) {
598 Log.e(TAG, Log.getStackTraceString(new Throwable()));
599 return false;
600 }
601 }
Jack He16eeac32017-08-17 12:11:18 -0700602 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530603 return false;
604 }
605
606 /**
607 * Get the priority of the profile.
608 */
609 public int getPriority(BluetoothDevice device) {
610 if (VDBG) log("getPriority(" + device + ")");
Jack He16eeac32017-08-17 12:11:18 -0700611 final IBluetoothHeadsetClient service = mService;
612 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.getPriority(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530615 } catch (RemoteException e) {
616 Log.e(TAG, Log.getStackTraceString(new Throwable()));
617 return PRIORITY_OFF;
618 }
619 }
Jack He16eeac32017-08-17 12:11:18 -0700620 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530621 return PRIORITY_OFF;
622 }
623
624 /**
625 * Starts voice recognition.
626 *
Jack Hea355e5e2017-08-22 16:06:54 -0700627 * @param device remote device
628 * @return <code>true</code> if command has been issued successfully; <code>false</code>
629 * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530630 *
Jack Hea355e5e2017-08-22 16:06:54 -0700631 * <p>Feature required for successful execution is being reported by: {@link
632 * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
633 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530634 */
635 public boolean startVoiceRecognition(BluetoothDevice device) {
636 if (DBG) log("startVoiceRecognition()");
Jack He16eeac32017-08-17 12:11:18 -0700637 final IBluetoothHeadsetClient service = mService;
638 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530639 try {
Jack He16eeac32017-08-17 12:11:18 -0700640 return service.startVoiceRecognition(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530641 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700642 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530643 }
644 }
Jack He16eeac32017-08-17 12:11:18 -0700645 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530646 return false;
647 }
648
649 /**
650 * Stops voice recognition.
651 *
Jack Hea355e5e2017-08-22 16:06:54 -0700652 * @param device remote device
653 * @return <code>true</code> if command has been issued successfully; <code>false</code>
654 * otherwise; upon completion HFP sends {@link #ACTION_AG_EVENT} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530655 *
Jack Hea355e5e2017-08-22 16:06:54 -0700656 * <p>Feature required for successful execution is being reported by: {@link
657 * #EXTRA_AG_FEATURE_VOICE_RECOGNITION}. This method invocation will fail silently when feature
658 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530659 */
660 public boolean stopVoiceRecognition(BluetoothDevice device) {
661 if (DBG) log("stopVoiceRecognition()");
Jack He16eeac32017-08-17 12:11:18 -0700662 final IBluetoothHeadsetClient service = mService;
663 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()");
Jack He16eeac32017-08-17 12:11:18 -0700682 final IBluetoothHeadsetClient service = mService;
683 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530684 try {
Jack He16eeac32017-08-17 12:11:18 -0700685 return service.getCurrentCalls(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530686 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700687 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530688 }
689 }
Jack He16eeac32017-08-17 12:11:18 -0700690 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530691 return null;
692 }
693
694 /**
695 * Returns list of current values of AG indicators.
696 *
Jack Hea355e5e2017-08-22 16:06:54 -0700697 * @param device remote device
698 * @return bundle of AG indicators; null if device is not in CONNECTED state
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530699 */
700 public Bundle getCurrentAgEvents(BluetoothDevice device) {
701 if (DBG) log("getCurrentCalls()");
Jack He16eeac32017-08-17 12:11:18 -0700702 final IBluetoothHeadsetClient service = mService;
703 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530704 try {
Jack He16eeac32017-08-17 12:11:18 -0700705 return service.getCurrentAgEvents(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530706 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700707 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530708 }
709 }
Jack He16eeac32017-08-17 12:11:18 -0700710 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530711 return null;
712 }
713
714 /**
715 * Accepts a call
716 *
Jack Hea355e5e2017-08-22 16:06:54 -0700717 * @param device remote device
718 * @param flag action policy while accepting a call. Possible values {@link #CALL_ACCEPT_NONE},
719 * {@link #CALL_ACCEPT_HOLD}, {@link #CALL_ACCEPT_TERMINATE}
720 * @return <code>true</code> if command has been issued successfully; <code>false</code>
721 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530722 */
723 public boolean acceptCall(BluetoothDevice device, int flag) {
724 if (DBG) log("acceptCall()");
Jack He16eeac32017-08-17 12:11:18 -0700725 final IBluetoothHeadsetClient service = mService;
726 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530727 try {
Jack He16eeac32017-08-17 12:11:18 -0700728 return service.acceptCall(device, flag);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530729 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700730 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530731 }
732 }
Jack He16eeac32017-08-17 12:11:18 -0700733 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530734 return false;
735 }
736
737 /**
738 * Holds a call.
739 *
Jack Hea355e5e2017-08-22 16:06:54 -0700740 * @param device remote device
741 * @return <code>true</code> if command has been issued successfully; <code>false</code>
742 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530743 */
744 public boolean holdCall(BluetoothDevice device) {
745 if (DBG) log("holdCall()");
Jack He16eeac32017-08-17 12:11:18 -0700746 final IBluetoothHeadsetClient service = mService;
747 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530748 try {
Jack He16eeac32017-08-17 12:11:18 -0700749 return service.holdCall(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530750 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700751 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530752 }
753 }
Jack He16eeac32017-08-17 12:11:18 -0700754 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530755 return false;
756 }
757
758 /**
759 * Rejects a call.
760 *
Jack Hea355e5e2017-08-22 16:06:54 -0700761 * @param device remote device
762 * @return <code>true</code> if command has been issued successfully; <code>false</code>
763 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530764 *
Jack Hea355e5e2017-08-22 16:06:54 -0700765 * <p>Feature required for successful execution is being reported by: {@link
766 * #EXTRA_AG_FEATURE_REJECT_CALL}. This method invocation will fail silently when feature is not
767 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530768 */
769 public boolean rejectCall(BluetoothDevice device) {
770 if (DBG) log("rejectCall()");
Jack He16eeac32017-08-17 12:11:18 -0700771 final IBluetoothHeadsetClient service = mService;
772 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530773 try {
Jack He16eeac32017-08-17 12:11:18 -0700774 return service.rejectCall(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530775 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700776 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530777 }
778 }
Jack He16eeac32017-08-17 12:11:18 -0700779 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530780 return false;
781 }
782
783 /**
784 * Terminates a specified call.
785 *
786 * Works only when Extended Call Control is supported by Audio Gateway.
787 *
Jack Hea355e5e2017-08-22 16:06:54 -0700788 * @param device remote device
Jack He2992cd02017-08-22 21:21:23 -0700789 * @param call Handle of call obtained in {@link #dial(BluetoothDevice, String)} or obtained via
790 * {@link #ACTION_CALL_CHANGED}. {@code call} may be null in which case we will hangup all active
Jack Hea355e5e2017-08-22 16:06:54 -0700791 * calls.
792 * @return <code>true</code> if command has been issued successfully; <code>false</code>
793 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530794 *
Jack Hea355e5e2017-08-22 16:06:54 -0700795 * <p>Feature required for successful execution is being reported by: {@link
796 * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
797 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530798 */
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700799 public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530800 if (DBG) log("terminateCall()");
Jack He16eeac32017-08-17 12:11:18 -0700801 final IBluetoothHeadsetClient service = mService;
802 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530803 try {
Jack He16eeac32017-08-17 12:11:18 -0700804 return service.terminateCall(device, call);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530805 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700806 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530807 }
808 }
Jack He16eeac32017-08-17 12:11:18 -0700809 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530810 return false;
811 }
812
813 /**
814 * Enters private mode with a specified call.
815 *
816 * Works only when Extended Call Control is supported by Audio Gateway.
817 *
Jack Hea355e5e2017-08-22 16:06:54 -0700818 * @param device remote device
819 * @param index index of the call to connect in private mode
820 * @return <code>true</code> if command has been issued successfully; <code>false</code>
821 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530822 *
Jack Hea355e5e2017-08-22 16:06:54 -0700823 * <p>Feature required for successful execution is being reported by: {@link
824 * #EXTRA_AG_FEATURE_ECC}. This method invocation will fail silently when feature is not
825 * supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530826 */
827 public boolean enterPrivateMode(BluetoothDevice device, int index) {
828 if (DBG) log("enterPrivateMode()");
Jack He16eeac32017-08-17 12:11:18 -0700829 final IBluetoothHeadsetClient service = mService;
830 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530831 try {
Jack He16eeac32017-08-17 12:11:18 -0700832 return service.enterPrivateMode(device, index);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530833 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700834 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530835 }
836 }
Jack He16eeac32017-08-17 12:11:18 -0700837 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530838 return false;
839 }
840
841 /**
842 * Performs explicit call transfer.
843 *
844 * That means connect other calls and disconnect.
845 *
Jack Hea355e5e2017-08-22 16:06:54 -0700846 * @param device remote device
847 * @return <code>true</code> if command has been issued successfully; <code>false</code>
848 * otherwise; upon completion HFP sends {@link #ACTION_CALL_CHANGED} intent.
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530849 *
Jack Hea355e5e2017-08-22 16:06:54 -0700850 * <p>Feature required for successful execution is being reported by: {@link
851 * #EXTRA_AG_FEATURE_MERGE_AND_DETACH}. This method invocation will fail silently when feature
852 * is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530853 */
854 public boolean explicitCallTransfer(BluetoothDevice device) {
855 if (DBG) log("explicitCallTransfer()");
Jack He16eeac32017-08-17 12:11:18 -0700856 final IBluetoothHeadsetClient service = mService;
857 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530858 try {
Jack He16eeac32017-08-17 12:11:18 -0700859 return service.explicitCallTransfer(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530860 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700861 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530862 }
863 }
Jack He16eeac32017-08-17 12:11:18 -0700864 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530865 return false;
866 }
867
868 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530869 * Places a call with specified number.
870 *
Jack Hea355e5e2017-08-22 16:06:54 -0700871 * @param device remote device
872 * @param number valid phone number
873 * @return <code>{@link BluetoothHeadsetClientCall} call</code> if command has been issued
874 * successfully; <code>{@link null}</code> otherwise; upon completion HFP sends {@link
875 * #ACTION_CALL_CHANGED} intent in case of success; {@link #ACTION_RESULT} is sent otherwise;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530876 */
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700877 public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530878 if (DBG) log("dial()");
Jack He16eeac32017-08-17 12:11:18 -0700879 final IBluetoothHeadsetClient service = mService;
880 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530881 try {
Jack He16eeac32017-08-17 12:11:18 -0700882 return service.dial(device, number);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530883 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700884 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530885 }
886 }
Jack He16eeac32017-08-17 12:11:18 -0700887 if (service == null) Log.w(TAG, "Proxy not attached to service");
Sanket Agarwal40bb6f32016-06-27 20:13:54 -0700888 return null;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530889 }
890
891 /**
892 * Sends DTMF code.
893 *
894 * Possible code values : 0,1,2,3,4,5,6,7,8,9,A,B,C,D,*,#
895 *
Jack Hea355e5e2017-08-22 16:06:54 -0700896 * @param device remote device
897 * @param code ASCII code
898 * @return <code>true</code> if command has been issued successfully; <code>false</code>
899 * otherwise; upon completion HFP sends {@link #ACTION_RESULT} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530900 */
901 public boolean sendDTMF(BluetoothDevice device, byte code) {
902 if (DBG) log("sendDTMF()");
Jack He16eeac32017-08-17 12:11:18 -0700903 final IBluetoothHeadsetClient service = mService;
904 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530905 try {
Jack He16eeac32017-08-17 12:11:18 -0700906 return service.sendDTMF(device, code);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530907 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700908 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530909 }
910 }
Jack He16eeac32017-08-17 12:11:18 -0700911 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530912 return false;
913 }
914
915 /**
916 * Get a number corresponding to last voice tag recorded on AG.
917 *
Jack Hea355e5e2017-08-22 16:06:54 -0700918 * @param device remote device
919 * @return <code>true</code> if command has been issued successfully; <code>false</code>
920 * otherwise; upon completion HFP sends {@link #ACTION_LAST_VTAG} or {@link #ACTION_RESULT}
921 * intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530922 *
Jack Hea355e5e2017-08-22 16:06:54 -0700923 * <p>Feature required for successful execution is being reported by: {@link
924 * #EXTRA_AG_FEATURE_ATTACH_NUMBER_TO_VT}. This method invocation will fail silently when
925 * feature is not supported.</p>
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530926 */
927 public boolean getLastVoiceTagNumber(BluetoothDevice device) {
928 if (DBG) log("getLastVoiceTagNumber()");
Jack He16eeac32017-08-17 12:11:18 -0700929 final IBluetoothHeadsetClient service = mService;
930 if (service != null && isEnabled() && isValidDevice(device)) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530931 try {
Jack He16eeac32017-08-17 12:11:18 -0700932 return service.getLastVoiceTagNumber(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530933 } catch (RemoteException e) {
Jack Hea355e5e2017-08-22 16:06:54 -0700934 Log.e(TAG, Log.getStackTraceString(new Throwable()));
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530935 }
936 }
Jack He16eeac32017-08-17 12:11:18 -0700937 if (service == null) Log.w(TAG, "Proxy not attached to service");
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530938 return false;
939 }
940
941 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530942 * Returns current audio state of Audio Gateway.
943 *
944 * Note: This is an internal function and shouldn't be exposed
945 */
946 public int getAudioState(BluetoothDevice device) {
947 if (VDBG) log("getAudioState");
Jack He16eeac32017-08-17 12:11:18 -0700948 final IBluetoothHeadsetClient service = mService;
949 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530950 try {
Jack He16eeac32017-08-17 12:11:18 -0700951 return service.getAudioState(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700952 } catch (RemoteException e) {
953 Log.e(TAG, e.toString());
954 }
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530955 } else {
956 Log.w(TAG, "Proxy not attached to service");
957 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
958 }
Mike Lockwoodcf916d32014-06-12 11:23:40 -0700959 return BluetoothHeadsetClient.STATE_AUDIO_DISCONNECTED;
Hemant Gupta7aca90f2013-08-19 19:03:51 +0530960 }
961
962 /**
Bryce Leedc133822015-10-28 22:21:54 -0700963 * Sets whether audio routing is allowed.
964 *
Jack Hea355e5e2017-08-22 16:06:54 -0700965 * @param device remote device
966 * @param allowed if routing is allowed to the device Note: This is an internal function and
967 * shouldn't be exposed
Bryce Leedc133822015-10-28 22:21:54 -0700968 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -0800969 public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) {
Bryce Leedc133822015-10-28 22:21:54 -0700970 if (VDBG) log("setAudioRouteAllowed");
Jack He16eeac32017-08-17 12:11:18 -0700971 final IBluetoothHeadsetClient service = mService;
972 if (service != null && isEnabled()) {
Bryce Leedc133822015-10-28 22:21:54 -0700973 try {
Jack He16eeac32017-08-17 12:11:18 -0700974 service.setAudioRouteAllowed(device, allowed);
Jack Hea355e5e2017-08-22 16:06:54 -0700975 } catch (RemoteException e) {
976 Log.e(TAG, e.toString());
977 }
Bryce Leedc133822015-10-28 22:21:54 -0700978 } else {
979 Log.w(TAG, "Proxy not attached to service");
980 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
981 }
982 }
983
984 /**
985 * Returns whether audio routing is allowed.
Jack Hea355e5e2017-08-22 16:06:54 -0700986 *
987 * @param device remote device
988 * @return whether the command succeeded Note: This is an internal function and shouldn't be
989 * exposed
Bryce Leedc133822015-10-28 22:21:54 -0700990 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -0800991 public boolean getAudioRouteAllowed(BluetoothDevice device) {
Bryce Leedc133822015-10-28 22:21:54 -0700992 if (VDBG) log("getAudioRouteAllowed");
Jack He16eeac32017-08-17 12:11:18 -0700993 final IBluetoothHeadsetClient service = mService;
994 if (service != null && isEnabled()) {
Bryce Leedc133822015-10-28 22:21:54 -0700995 try {
Jack He16eeac32017-08-17 12:11:18 -0700996 return service.getAudioRouteAllowed(device);
Jack Hea355e5e2017-08-22 16:06:54 -0700997 } catch (RemoteException e) {
998 Log.e(TAG, e.toString());
999 }
Bryce Leedc133822015-10-28 22:21:54 -07001000 } else {
1001 Log.w(TAG, "Proxy not attached to service");
1002 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1003 }
1004 return false;
1005 }
1006
1007 /**
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301008 * Initiates a connection of audio channel.
1009 *
1010 * It setup SCO channel with remote connected Handsfree AG device.
1011 *
Jack Hea355e5e2017-08-22 16:06:54 -07001012 * @param device remote device
1013 * @return <code>true</code> if command has been issued successfully; <code>false</code>
1014 * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301015 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -08001016 public boolean connectAudio(BluetoothDevice device) {
Jack He16eeac32017-08-17 12:11:18 -07001017 final IBluetoothHeadsetClient service = mService;
1018 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301019 try {
Jack He16eeac32017-08-17 12:11:18 -07001020 return service.connectAudio(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301021 } catch (RemoteException e) {
1022 Log.e(TAG, e.toString());
1023 }
1024 } else {
1025 Log.w(TAG, "Proxy not attached to service");
1026 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1027 }
1028 return false;
1029 }
1030
1031 /**
1032 * Disconnects audio channel.
1033 *
1034 * It tears down the SCO channel from remote AG device.
1035 *
Jack Hea355e5e2017-08-22 16:06:54 -07001036 * @param device remote device
1037 * @return <code>true</code> if command has been issued successfully; <code>false</code>
1038 * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301039 */
Sanket Agarwal039eeb82017-01-20 14:55:15 -08001040 public boolean disconnectAudio(BluetoothDevice device) {
Jack He16eeac32017-08-17 12:11:18 -07001041 final IBluetoothHeadsetClient service = mService;
1042 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301043 try {
Jack He16eeac32017-08-17 12:11:18 -07001044 return service.disconnectAudio(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301045 } catch (RemoteException e) {
1046 Log.e(TAG, e.toString());
1047 }
1048 } else {
1049 Log.w(TAG, "Proxy not attached to service");
1050 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1051 }
1052 return false;
1053 }
1054
1055 /**
1056 * Get Audio Gateway features
1057 *
Jack Hea355e5e2017-08-22 16:06:54 -07001058 * @param device remote device
1059 * @return bundle of AG features; null if no service or AG not connected
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301060 */
1061 public Bundle getCurrentAgFeatures(BluetoothDevice device) {
Jack He16eeac32017-08-17 12:11:18 -07001062 final IBluetoothHeadsetClient service = mService;
1063 if (service != null && isEnabled()) {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301064 try {
Jack He16eeac32017-08-17 12:11:18 -07001065 return service.getCurrentAgFeatures(device);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301066 } catch (RemoteException e) {
1067 Log.e(TAG, e.toString());
1068 }
1069 } else {
1070 Log.w(TAG, "Proxy not attached to service");
1071 if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable()));
1072 }
1073 return null;
1074 }
1075
1076
Jack He16eeac32017-08-17 12:11:18 -07001077 private final ServiceConnection mConnection = new ServiceConnection() {
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301078 @Override
1079 public void onServiceConnected(ComponentName className, IBinder service) {
1080 if (DBG) Log.d(TAG, "Proxy object connected");
Jeff Sharkey0a17db12016-11-04 11:23:46 -06001081 mService = IBluetoothHeadsetClient.Stub.asInterface(Binder.allowBlocking(service));
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301082
1083 if (mServiceListener != null) {
Mike Lockwoodcf916d32014-06-12 11:23:40 -07001084 mServiceListener.onServiceConnected(BluetoothProfile.HEADSET_CLIENT,
1085 BluetoothHeadsetClient.this);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301086 }
1087 }
Jack Hea355e5e2017-08-22 16:06:54 -07001088
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301089 @Override
1090 public void onServiceDisconnected(ComponentName className) {
1091 if (DBG) Log.d(TAG, "Proxy object disconnected");
1092 mService = null;
1093 if (mServiceListener != null) {
Mike Lockwoodcf916d32014-06-12 11:23:40 -07001094 mServiceListener.onServiceDisconnected(BluetoothProfile.HEADSET_CLIENT);
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301095 }
1096 }
1097 };
1098
1099 private boolean isEnabled() {
Jack He16eeac32017-08-17 12:11:18 -07001100 return mAdapter.getState() == BluetoothAdapter.STATE_ON;
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301101 }
1102
Jack He16eeac32017-08-17 12:11:18 -07001103 private static boolean isValidDevice(BluetoothDevice device) {
1104 return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
Hemant Gupta7aca90f2013-08-19 19:03:51 +05301105 }
1106
1107 private static void log(String msg) {
1108 Log.d(TAG, msg);
1109 }
1110}