blob: 606f00a8239d768db5f19bce0d9636a47cf9a168 [file] [log] [blame]
Jakub Pawlowskiea580fa2017-11-22 11:02:34 -08001/*
2 * Copyright 2018 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.Manifest;
Hansong Zhang7ca303c2018-03-16 09:15:48 -070020import android.annotation.Nullable;
Jakub Pawlowskiea580fa2017-11-22 11:02:34 -080021import android.annotation.RequiresPermission;
22import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Mathew Inwood4dc66d32018-08-01 15:07:20 +010024import android.annotation.UnsupportedAppUsage;
Jakub Pawlowskiea580fa2017-11-22 11:02:34 -080025import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.ServiceConnection;
29import android.os.Binder;
30import android.os.IBinder;
31import android.os.RemoteException;
32import android.util.Log;
33
34import com.android.internal.annotations.GuardedBy;
35
36import java.util.ArrayList;
37import java.util.List;
38import java.util.concurrent.locks.ReentrantReadWriteLock;
39
40/**
41 * This class provides the public APIs to control the Bluetooth Hearing Aid
42 * profile.
43 *
44 * <p>BluetoothHearingAid is a proxy object for controlling the Bluetooth Hearing Aid
45 * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get
46 * the BluetoothHearingAid proxy object.
47 *
48 * <p> Each method is protected with its appropriate permission.
49 * @hide
50 */
51public final class BluetoothHearingAid implements BluetoothProfile {
52 private static final String TAG = "BluetoothHearingAid";
53 private static final boolean DBG = false;
54 private static final boolean VDBG = false;
55
56 /**
57 * Intent used to broadcast the change in connection state of the Hearing Aid
58 * profile.
59 *
60 * <p>This intent will have 3 extras:
61 * <ul>
62 * <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
63 * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li>
64 * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
65 * </ul>
66 *
67 * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
68 * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
69 * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
70 *
71 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
72 * receive.
73 */
74 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
75 public static final String ACTION_CONNECTION_STATE_CHANGED =
76 "android.bluetooth.hearingaid.profile.action.CONNECTION_STATE_CHANGED";
77
78 /**
79 * Intent used to broadcast the change in the Playing state of the Hearing Aid
80 * profile.
81 *
82 * <p>This intent will have 3 extras:
83 * <ul>
84 * <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
85 * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. </li>
86 * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
87 * </ul>
88 *
89 * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
90 * {@link #STATE_PLAYING}, {@link #STATE_NOT_PLAYING},
91 *
92 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
93 * receive.
94 */
95 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
96 public static final String ACTION_PLAYING_STATE_CHANGED =
97 "android.bluetooth.hearingaid.profile.action.PLAYING_STATE_CHANGED";
98
99 /**
100 * Intent used to broadcast the selection of a connected device as active.
101 *
102 * <p>This intent will have one extra:
103 * <ul>
104 * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. It can
105 * be null if no device is active. </li>
106 * </ul>
107 *
108 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to
109 * receive.
110 */
111 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100112 @UnsupportedAppUsage
Jakub Pawlowskiea580fa2017-11-22 11:02:34 -0800113 public static final String ACTION_ACTIVE_DEVICE_CHANGED =
114 "android.bluetooth.hearingaid.profile.action.ACTIVE_DEVICE_CHANGED";
115
116 /**
117 * Hearing Aid device is streaming music. This state can be one of
118 * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
119 * {@link #ACTION_PLAYING_STATE_CHANGED} intent.
120 */
121 public static final int STATE_PLAYING = 10;
122
123 /**
124 * Hearing Aid device is NOT streaming music. This state can be one of
125 * {@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} of
126 * {@link #ACTION_PLAYING_STATE_CHANGED} intent.
127 */
128 public static final int STATE_NOT_PLAYING = 11;
129
130 /** This device represents Left Hearing Aid. */
131 public static final int SIDE_LEFT = IBluetoothHearingAid.SIDE_LEFT;
132
133 /** This device represents Right Hearing Aid. */
134 public static final int SIDE_RIGHT = IBluetoothHearingAid.SIDE_RIGHT;
135
136 /** This device is Monaural. */
137 public static final int MODE_MONAURAL = IBluetoothHearingAid.MODE_MONAURAL;
138
139 /** This device is Binaural (should receive only left or right audio). */
140 public static final int MODE_BINAURAL = IBluetoothHearingAid.MODE_BINAURAL;
141
142 /** Can't read ClientID for this device */
143 public static final long HI_SYNC_ID_INVALID = IBluetoothHearingAid.HI_SYNC_ID_INVALID;
144
145 private Context mContext;
146 private ServiceListener mServiceListener;
147 private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock();
148 @GuardedBy("mServiceLock")
149 private IBluetoothHearingAid mService;
150 private BluetoothAdapter mAdapter;
151
152 private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
153 new IBluetoothStateChangeCallback.Stub() {
154 public void onBluetoothStateChange(boolean up) {
155 if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
156 if (!up) {
157 if (VDBG) Log.d(TAG, "Unbinding service...");
158 try {
159 mServiceLock.writeLock().lock();
160 mService = null;
161 mContext.unbindService(mConnection);
162 } catch (Exception re) {
163 Log.e(TAG, "", re);
164 } finally {
165 mServiceLock.writeLock().unlock();
166 }
167 } else {
168 try {
169 mServiceLock.readLock().lock();
170 if (mService == null) {
171 if (VDBG) Log.d(TAG, "Binding service...");
172 doBind();
173 }
174 } catch (Exception re) {
175 Log.e(TAG, "", re);
176 } finally {
177 mServiceLock.readLock().unlock();
178 }
179 }
180 }
181 };
182
183 /**
184 * Create a BluetoothHearingAid proxy object for interacting with the local
185 * Bluetooth Hearing Aid service.
186 */
187 /*package*/ BluetoothHearingAid(Context context, ServiceListener l) {
188 mContext = context;
189 mServiceListener = l;
190 mAdapter = BluetoothAdapter.getDefaultAdapter();
191 IBluetoothManager mgr = mAdapter.getBluetoothManager();
192 if (mgr != null) {
193 try {
194 mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
195 } catch (RemoteException e) {
196 Log.e(TAG, "", e);
197 }
198 }
199
200 doBind();
201 }
202
203 void doBind() {
204 Intent intent = new Intent(IBluetoothHearingAid.class.getName());
205 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
206 intent.setComponent(comp);
207 if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
208 android.os.Process.myUserHandle())) {
209 Log.e(TAG, "Could not bind to Bluetooth Hearing Aid Service with " + intent);
210 return;
211 }
212 }
213
214 /*package*/ void close() {
215 mServiceListener = null;
216 IBluetoothManager mgr = mAdapter.getBluetoothManager();
217 if (mgr != null) {
218 try {
219 mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
220 } catch (Exception e) {
221 Log.e(TAG, "", e);
222 }
223 }
224
225 try {
226 mServiceLock.writeLock().lock();
227 if (mService != null) {
228 mService = null;
229 mContext.unbindService(mConnection);
230 }
231 } catch (Exception re) {
232 Log.e(TAG, "", re);
233 } finally {
234 mServiceLock.writeLock().unlock();
235 }
236 }
237
238 @Override
239 public void finalize() {
240 // The empty finalize needs to be kept or the
241 // cts signature tests would fail.
242 }
243
244 /**
245 * Initiate connection to a profile of the remote bluetooth device.
246 *
247 * <p> This API returns false in scenarios like the profile on the
248 * device is already connected or Bluetooth is not turned on.
249 * When this API returns true, it is guaranteed that
250 * connection state intent for the profile will be broadcasted with
251 * the state. Users can get the connection state of the profile
252 * from this intent.
253 *
254 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
255 * permission.
256 *
257 * @param device Remote Bluetooth Device
258 * @return false on immediate error, true otherwise
259 * @hide
260 */
261 public boolean connect(BluetoothDevice device) {
262 if (DBG) log("connect(" + device + ")");
263 try {
264 mServiceLock.readLock().lock();
265 if (mService != null && isEnabled() && isValidDevice(device)) {
266 return mService.connect(device);
267 }
268 if (mService == null) Log.w(TAG, "Proxy not attached to service");
269 return false;
270 } catch (RemoteException e) {
271 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
272 return false;
273 } finally {
274 mServiceLock.readLock().unlock();
275 }
276 }
277
278 /**
279 * Initiate disconnection from a profile
280 *
281 * <p> This API will return false in scenarios like the profile on the
282 * Bluetooth device is not in connected state etc. When this API returns,
283 * true, it is guaranteed that the connection state change
284 * intent will be broadcasted with the state. Users can get the
285 * disconnection state of the profile from this intent.
286 *
287 * <p> If the disconnection is initiated by a remote device, the state
288 * will transition from {@link #STATE_CONNECTED} to
289 * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the
290 * host (local) device the state will transition from
291 * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to
292 * state {@link #STATE_DISCONNECTED}. The transition to
293 * {@link #STATE_DISCONNECTING} can be used to distinguish between the
294 * two scenarios.
295 *
296 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
297 * permission.
298 *
299 * @param device Remote Bluetooth Device
300 * @return false on immediate error, true otherwise
301 * @hide
302 */
303 public boolean disconnect(BluetoothDevice device) {
304 if (DBG) log("disconnect(" + device + ")");
305 try {
306 mServiceLock.readLock().lock();
307 if (mService != null && isEnabled() && isValidDevice(device)) {
308 return mService.disconnect(device);
309 }
310 if (mService == null) Log.w(TAG, "Proxy not attached to service");
311 return false;
312 } catch (RemoteException e) {
313 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
314 return false;
315 } finally {
316 mServiceLock.readLock().unlock();
317 }
318 }
319
320 /**
321 * {@inheritDoc}
322 */
323 @Override
324 public List<BluetoothDevice> getConnectedDevices() {
325 if (VDBG) log("getConnectedDevices()");
326 try {
327 mServiceLock.readLock().lock();
328 if (mService != null && isEnabled()) {
329 return mService.getConnectedDevices();
330 }
331 if (mService == null) Log.w(TAG, "Proxy not attached to service");
332 return new ArrayList<BluetoothDevice>();
333 } catch (RemoteException e) {
334 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
335 return new ArrayList<BluetoothDevice>();
336 } finally {
337 mServiceLock.readLock().unlock();
338 }
339 }
340
341 /**
342 * {@inheritDoc}
343 */
344 @Override
345 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
346 if (VDBG) log("getDevicesMatchingStates()");
347 try {
348 mServiceLock.readLock().lock();
349 if (mService != null && isEnabled()) {
350 return mService.getDevicesMatchingConnectionStates(states);
351 }
352 if (mService == null) Log.w(TAG, "Proxy not attached to service");
353 return new ArrayList<BluetoothDevice>();
354 } catch (RemoteException e) {
355 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
356 return new ArrayList<BluetoothDevice>();
357 } finally {
358 mServiceLock.readLock().unlock();
359 }
360 }
361
362 /**
363 * {@inheritDoc}
364 */
365 @Override
366 public int getConnectionState(BluetoothDevice device) {
367 if (VDBG) log("getState(" + device + ")");
368 try {
369 mServiceLock.readLock().lock();
370 if (mService != null && isEnabled()
371 && isValidDevice(device)) {
372 return mService.getConnectionState(device);
373 }
374 if (mService == null) Log.w(TAG, "Proxy not attached to service");
375 return BluetoothProfile.STATE_DISCONNECTED;
376 } catch (RemoteException e) {
377 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
378 return BluetoothProfile.STATE_DISCONNECTED;
379 } finally {
380 mServiceLock.readLock().unlock();
381 }
382 }
383
384 /**
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700385 * Select a connected device as active.
386 *
387 * The active device selection is per profile. An active device's
388 * purpose is profile-specific. For example, Hearing Aid audio
389 * streaming is to the active Hearing Aid device. If a remote device
390 * is not connected, it cannot be selected as active.
391 *
392 * <p> This API returns false in scenarios like the profile on the
393 * device is not connected or Bluetooth is not turned on.
394 * When this API returns true, it is guaranteed that the
395 * {@link #ACTION_ACTIVE_DEVICE_CHANGED} intent will be broadcasted
396 * with the active device.
397 *
398 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
399 * permission.
400 *
401 * @param device the remote Bluetooth device. Could be null to clear
402 * the active device and stop streaming audio to a Bluetooth device.
403 * @return false on immediate error, true otherwise
404 * @hide
405 */
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100406 @UnsupportedAppUsage
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700407 public boolean setActiveDevice(@Nullable BluetoothDevice device) {
408 if (DBG) log("setActiveDevice(" + device + ")");
409 try {
410 mServiceLock.readLock().lock();
411 if (mService != null && isEnabled()
412 && ((device == null) || isValidDevice(device))) {
413 mService.setActiveDevice(device);
414 return true;
415 }
416 if (mService == null) Log.w(TAG, "Proxy not attached to service");
417 return false;
418 } catch (RemoteException e) {
419 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
420 return false;
421 } finally {
422 mServiceLock.readLock().unlock();
423 }
424 }
425
426 /**
Hansong Zhang8d799f82018-03-28 16:53:10 -0700427 * Get the connected physical Hearing Aid devices that are active
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700428 *
429 * <p>Requires {@link android.Manifest.permission#BLUETOOTH}
430 * permission.
431 *
Hansong Zhang8d799f82018-03-28 16:53:10 -0700432 * @return the list of active devices. The first element is the left active
433 * device; the second element is the right active device. If either or both side
434 * is not active, it will be null on that position. Returns empty list on error.
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700435 * @hide
436 */
437 @RequiresPermission(Manifest.permission.BLUETOOTH)
Mathew Inwood4dc66d32018-08-01 15:07:20 +0100438 @UnsupportedAppUsage
Hansong Zhang8d799f82018-03-28 16:53:10 -0700439 public List<BluetoothDevice> getActiveDevices() {
440 if (VDBG) log("getActiveDevices()");
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700441 try {
442 mServiceLock.readLock().lock();
Hansong Zhang8d799f82018-03-28 16:53:10 -0700443 if (mService != null && isEnabled()) {
444 return mService.getActiveDevices();
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700445 }
446 if (mService == null) Log.w(TAG, "Proxy not attached to service");
Hansong Zhang8d799f82018-03-28 16:53:10 -0700447 return new ArrayList<>();
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700448 } catch (RemoteException e) {
449 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
Hansong Zhang8d799f82018-03-28 16:53:10 -0700450 return new ArrayList<>();
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700451 } finally {
452 mServiceLock.readLock().unlock();
453 }
454 }
455
456 /**
Jakub Pawlowskiea580fa2017-11-22 11:02:34 -0800457 * Set priority of the profile
458 *
459 * <p> The device should already be paired.
460 * Priority can be one of {@link #PRIORITY_ON} orgetBluetoothManager
461 * {@link #PRIORITY_OFF},
462 *
463 * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN}
464 * permission.
465 *
466 * @param device Paired bluetooth device
467 * @param priority
468 * @return true if priority is set, false on error
469 * @hide
470 */
471 public boolean setPriority(BluetoothDevice device, int priority) {
472 if (DBG) log("setPriority(" + device + ", " + priority + ")");
473 try {
474 mServiceLock.readLock().lock();
475 if (mService != null && isEnabled()
476 && isValidDevice(device)) {
477 if (priority != BluetoothProfile.PRIORITY_OFF
478 && priority != BluetoothProfile.PRIORITY_ON) {
479 return false;
480 }
481 return mService.setPriority(device, priority);
482 }
483 if (mService == null) Log.w(TAG, "Proxy not attached to service");
484 return false;
485 } catch (RemoteException e) {
486 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
487 return false;
488 } finally {
489 mServiceLock.readLock().unlock();
490 }
491 }
492
493 /**
494 * Get the priority of the profile.
495 *
496 * <p> The priority can be any of:
497 * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF},
498 * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED}
499 *
500 * @param device Bluetooth device
501 * @return priority of the device
502 * @hide
503 */
504 @RequiresPermission(Manifest.permission.BLUETOOTH)
505 public int getPriority(BluetoothDevice device) {
506 if (VDBG) log("getPriority(" + device + ")");
507 try {
508 mServiceLock.readLock().lock();
509 if (mService != null && isEnabled()
510 && isValidDevice(device)) {
511 return mService.getPriority(device);
512 }
513 if (mService == null) Log.w(TAG, "Proxy not attached to service");
514 return BluetoothProfile.PRIORITY_OFF;
515 } catch (RemoteException e) {
516 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
517 return BluetoothProfile.PRIORITY_OFF;
518 } finally {
519 mServiceLock.readLock().unlock();
520 }
521 }
522
523 /**
524 * Helper for converting a state to a string.
525 *
526 * For debug use only - strings are not internationalized.
527 *
528 * @hide
529 */
530 public static String stateToString(int state) {
531 switch (state) {
532 case STATE_DISCONNECTED:
533 return "disconnected";
534 case STATE_CONNECTING:
535 return "connecting";
536 case STATE_CONNECTED:
537 return "connected";
538 case STATE_DISCONNECTING:
539 return "disconnecting";
540 case STATE_PLAYING:
541 return "playing";
542 case STATE_NOT_PLAYING:
543 return "not playing";
544 default:
545 return "<unknown state " + state + ">";
546 }
547 }
548
549 /**
550 * Get the volume of the device.
551 *
552 * <p> The volume is between -128 dB (mute) to 0 dB.
553 *
554 * @return volume of the hearing aid device.
555 * @hide
556 */
557 @RequiresPermission(Manifest.permission.BLUETOOTH)
558 public int getVolume() {
559 if (VDBG) {
560 log("getVolume()");
561 }
562 try {
563 mServiceLock.readLock().lock();
564 if (mService != null && isEnabled()) {
565 return mService.getVolume();
566 }
567 if (mService == null) Log.w(TAG, "Proxy not attached to service");
568 return 0;
569 } catch (RemoteException e) {
570 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
571 return 0;
572 } finally {
573 mServiceLock.readLock().unlock();
574 }
575 }
576
577 /**
578 * Tells remote device to adjust volume. Uses the following values:
579 * <ul>
580 * <li>{@link AudioManager#ADJUST_LOWER}</li>
581 * <li>{@link AudioManager#ADJUST_RAISE}</li>
582 * <li>{@link AudioManager#ADJUST_MUTE}</li>
583 * <li>{@link AudioManager#ADJUST_UNMUTE}</li>
584 * </ul>
585 *
586 * @param direction One of the supported adjust values.
587 * @hide
588 */
589 @RequiresPermission(Manifest.permission.BLUETOOTH)
590 public void adjustVolume(int direction) {
591 if (DBG) log("adjustVolume(" + direction + ")");
592
593 try {
594 mServiceLock.readLock().lock();
595
596 if (mService == null) {
597 Log.w(TAG, "Proxy not attached to service");
598 return;
599 }
600
601 if (!isEnabled()) return;
602
603 mService.adjustVolume(direction);
604 } catch (RemoteException e) {
605 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
606 } finally {
607 mServiceLock.readLock().unlock();
608 }
609 }
610
611 /**
612 * Tells remote device to set an absolute volume.
613 *
614 * @param volume Absolute volume to be set on remote
615 * @hide
616 */
617 public void setVolume(int volume) {
618 if (DBG) Log.d(TAG, "setVolume(" + volume + ")");
619
620 try {
621 mServiceLock.readLock().lock();
622 if (mService == null) {
623 Log.w(TAG, "Proxy not attached to service");
624 return;
625 }
626
627 if (!isEnabled()) return;
628
629 mService.setVolume(volume);
630 } catch (RemoteException e) {
631 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
632 } finally {
633 mServiceLock.readLock().unlock();
634 }
635 }
636
637 /**
638 * Get the CustomerId of the device.
639 *
640 * @param device Bluetooth device
641 * @return the CustomerId of the device
642 * @hide
643 */
644 @RequiresPermission(Manifest.permission.BLUETOOTH)
645 public long getHiSyncId(BluetoothDevice device) {
646 if (VDBG) {
647 log("getCustomerId(" + device + ")");
648 }
649 try {
650 mServiceLock.readLock().lock();
651 if (mService == null) {
652 Log.w(TAG, "Proxy not attached to service");
653 return HI_SYNC_ID_INVALID;
654 }
655
656 if (!isEnabled() || !isValidDevice(device)) return HI_SYNC_ID_INVALID;
657
658 return mService.getHiSyncId(device);
659 } catch (RemoteException e) {
660 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
661 return HI_SYNC_ID_INVALID;
662 } finally {
663 mServiceLock.readLock().unlock();
664 }
665 }
666
667 /**
668 * Get the side of the device.
669 *
670 * @param device Bluetooth device.
671 * @return SIDE_LEFT or SIDE_RIGHT
672 * @hide
673 */
674 @RequiresPermission(Manifest.permission.BLUETOOTH)
675 public int getDeviceSide(BluetoothDevice device) {
676 if (VDBG) {
677 log("getDeviceSide(" + device + ")");
678 }
679 try {
680 mServiceLock.readLock().lock();
681 if (mService != null && isEnabled()
682 && isValidDevice(device)) {
683 return mService.getDeviceSide(device);
684 }
685 if (mService == null) Log.w(TAG, "Proxy not attached to service");
686 return SIDE_LEFT;
687 } catch (RemoteException e) {
688 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
689 return SIDE_LEFT;
690 } finally {
691 mServiceLock.readLock().unlock();
692 }
693 }
694
695 /**
696 * Get the mode of the device.
697 *
698 * @param device Bluetooth device
699 * @return MODE_MONAURAL or MODE_BINAURAL
700 * @hide
701 */
702 @RequiresPermission(Manifest.permission.BLUETOOTH)
703 public int getDeviceMode(BluetoothDevice device) {
704 if (VDBG) {
705 log("getDeviceMode(" + device + ")");
706 }
707 try {
708 mServiceLock.readLock().lock();
709 if (mService != null && isEnabled()
710 && isValidDevice(device)) {
711 return mService.getDeviceMode(device);
712 }
713 if (mService == null) Log.w(TAG, "Proxy not attached to service");
714 return MODE_MONAURAL;
715 } catch (RemoteException e) {
716 Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
717 return MODE_MONAURAL;
718 } finally {
719 mServiceLock.readLock().unlock();
720 }
721 }
722
723 private final ServiceConnection mConnection = new ServiceConnection() {
724 public void onServiceConnected(ComponentName className, IBinder service) {
725 if (DBG) Log.d(TAG, "Proxy object connected");
726 try {
727 mServiceLock.writeLock().lock();
728 mService = IBluetoothHearingAid.Stub.asInterface(Binder.allowBlocking(service));
729 } finally {
730 mServiceLock.writeLock().unlock();
731 }
732
733 if (mServiceListener != null) {
734 mServiceListener.onServiceConnected(BluetoothProfile.HEARING_AID,
735 BluetoothHearingAid.this);
736 }
737 }
738
739 public void onServiceDisconnected(ComponentName className) {
740 if (DBG) Log.d(TAG, "Proxy object disconnected");
741 try {
742 mServiceLock.writeLock().lock();
743 mService = null;
744 } finally {
745 mServiceLock.writeLock().unlock();
746 }
747 if (mServiceListener != null) {
748 mServiceListener.onServiceDisconnected(BluetoothProfile.HEARING_AID);
749 }
750 }
751 };
752
753 private boolean isEnabled() {
754 if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
755 return false;
756 }
757
758 private boolean isValidDevice(BluetoothDevice device) {
759 if (device == null) return false;
760
761 if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
762 return false;
763 }
764
765 private static void log(String msg) {
766 Log.d(TAG, msg);
767 }
768}