1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
|
/*
* Copyright 2021 HIMSA II K/S - www.himsa.com.
* Represented by EHIMA - www.ehima.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.bluetooth;
import static android.bluetooth.BluetoothUtils.getSyncTimeout;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
import android.content.AttributionSource;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.CloseGuard;
import android.util.Log;
import com.android.modules.utils.SynchronousResultReceiver;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* This class provides a public APIs to control the Bluetooth Hearing Access Profile client service.
*
* <p>BluetoothHapClient is a proxy object for controlling the Bluetooth HAP
* Service client via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get the
* BluetoothHapClient proxy object.
*/
public final class BluetoothHapClient implements BluetoothProfile, AutoCloseable {
private static final String TAG = "BluetoothHapClient";
private static final boolean DBG = false;
private static final boolean VDBG = false;
private CloseGuard mCloseGuard;
/**
* Intent used to broadcast the change in connection state of the Hearing Access Profile Client
* service. Please note that in the binaural case, there will be two different LE devices for
* the left and right side and each device will have their own connection state changes.
*
* <p>This intent will have 3 extras:
* <ul>
* <li> {@link #EXTRA_STATE} - The current state of the profile. </li>
* <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* </ul>
*
* <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of
* {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING},
* {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}.
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_CONNECTION_STATE_CHANGED =
"android.bluetooth.action.HAP_CONNECTION_STATE_CHANGED";
/**
* Intent used to broadcast the device availability change and the availability of its
* presets. Please note that in the binaural case, there will be two different LE devices for
* the left and right side and each device will have their own availability event.
*
* <p>This intent will have 2 extras:
* <ul>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* <li> {@link #EXTRA_HAP_FEATURES} - Supported features map. </li>
* </ul>
*
* @hide
*/
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_DEVICE_AVAILABLE =
"android.bluetooth.action.HAP_DEVICE_AVAILABLE";
/**
* Intent used to broadcast HA device's feature set.
*
* <p>This intent will have 2 extras:
* <ul>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* <li> {@link #EXTRA_HAP_FEATURES}- The feature set integer with these possible bit numbers
* set: {@link #FEATURE_BIT_NUM_TYPE_MONAURAL}, {@link #FEATURE_BIT_NUM_TYPE_BANDED},
* {@link #FEATURE_BIT_NUM_SYNCHRONIZATED_PRESETS},
* {@link #FEATURE_BIT_NUM_INDEPENDENT_PRESETS}, {@link #FEATURE_BIT_NUM_DYNAMIC_PRESETS},
* {@link #FEATURE_BIT_NUM_WRITABLE_PRESETS}.</li>
* </ul>
*
* @hide
*/
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_ON_DEVICE_FEATURES =
"android.bluetooth.action.HAP_ON_DEVICE_FEATURES";
/**
* Intent used to broadcast the change of a HA device's active preset.
*
* <p>This intent will have 2 extras:
* <ul>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* <li> {@link #EXTRA_HAP_PRESET_INDEX}- The currently active preset.</li>
* </ul>
*
* @hide
*/
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_ON_ACTIVE_PRESET =
"android.bluetooth.action.HAP_ON_ACTIVE_PRESET";
/**
* Intent used to broadcast the result of a failed preset change attempt.
*
* <p>This intent will have 2 extras:
* <ul>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* <li> {@link #EXTRA_HAP_STATUS_CODE}- Failure reason.</li>
* </ul>
*
* <p>{@link #EXTRA_HAP_STATUS_CODE} can be any of {@link #STATUS_INVALID_PRESET_INDEX},
* {@link #STATUS_OPERATION_NOT_POSSIBLE},{@link #STATUS_OPERATION_NOT_SUPPORTED}.
*
* @hide
*/
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_ON_ACTIVE_PRESET_SELECT_ERROR =
"android.bluetooth.action.HAP_ON_ACTIVE_PRESET_SELECT_ERROR";
/**
* Intent used to broadcast preset name change.
*
* <p>This intent will have 4 extras:
* <ul>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* <li> {@link #EXTRA_HAP_PRESET_INFO}- List of preset informations </li>
* <li> {@link #EXTRA_HAP_PRESET_INFO_REASON}- Why this preset info notification was sent </li>
* notifications or the user should expect more to come. </li>
* </ul>
*
* @hide
*/
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_ON_PRESET_INFO =
"android.bluetooth.action.HAP_ON_PRESET_INFO";
/**
* Intent used to broadcast result of a failed rename attempt.
*
* <p>This intent will have 3 extras:
* <ul>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* <li> {@link #EXTRA_HAP_PRESET_INDEX}- The currently active preset.</li>
* <li> {@link #EXTRA_HAP_STATUS_CODE}- Failure reason code.</li>
* </ul>
*
* <p>{@link #EXTRA_HAP_STATUS_CODE} can be any of {@link #STATUS_SET_NAME_NOT_ALLOWED},
* {@link #STATUS_INVALID_PRESET_INDEX}, {@link #STATUS_INVALID_PRESET_NAME_LENGTH}.
*
* @hide
*/
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_ON_PRESET_NAME_SET_ERROR =
"android.bluetooth.action.HAP_ON_PRESET_NAME_SET_ERROR";
/**
* Intent used to broadcast the result of a failed name get attempt.
*
* <p>This intent will have 3 extras:
* <ul>
* <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li>
* <li> {@link #EXTRA_HAP_PRESET_INDEX}- The currently active preset.</li>
* <li> {@link #EXTRA_HAP_STATUS_CODE}- Failure reason code.</li>
* </ul>
*
* <p>{@link #EXTRA_HAP_STATUS_CODE} can be any of {@link #STATUS_INVALID_PRESET_INDEX},
* {@link #STATUS_OPERATION_NOT_POSSIBLE}.
*
* @hide
*/
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
@SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_HAP_ON_PRESET_INFO_GET_ERROR =
"android.bluetooth.action.HAP_ON_PRESET_INFO_GET_ERROR";
/**
* Contains a list of all available presets
* @hide
*/
public static final String EXTRA_HAP_FEATURES = "android.bluetooth.extra.HAP_FEATURES";
/**
* Contains a preset identifier
* @hide
*/
public static final String EXTRA_HAP_PRESET_INDEX = "android.bluetooth.extra.HAP_PRESET_INDEX";
/**
* Used to report failure reasons.
* @hide
*/
public static final String EXTRA_HAP_STATUS_CODE = "android.bluetooth.extra.HAP_STATUS_CODE";
/**
* Used by group events.
* @hide
*/
public static final String EXTRA_HAP_GROUP_ID = "android.bluetooth.extra.HAP_GROUP_ID";
/**
* Preset Info reason.
* Possible values:
* {@link #PRESET_INFO_REASON_ALL_PRESET_INFO} or
* {@link #PRESET_INFO_REASON_PRESET_INFO_UPDATE} or
* {@link #PRESET_INFO_REASON_PRESET_DELETED} or
* {@link #PRESET_INFO_REASON_PRESET_AVAILABILITY_CHANGED} or
* {@link #PRESET_INFO_REASON_PRESET_INFO_REQUEST_RESPONSE}
* @hide
*/
public static final String EXTRA_HAP_PRESET_INFO_REASON =
"android.bluetooth.extra.HAP_PRESET_INFO_REASON";
/**
* Preset Info.
* @hide
*/
public static final String EXTRA_HAP_PRESET_INFO = "android.bluetooth.extra.HAP_PRESET_INFO";
/**
* Preset name change failure due to preset being read-only.
* @hide
*/
public static final int STATUS_SET_NAME_NOT_ALLOWED =
IBluetoothHapClient.STATUS_SET_NAME_NOT_ALLOWED;
/**
* Means that the requested operation is not supported by the HA device.
*
* <p> It could mean that the requested name change is not supported on
* a given preset or the device does not support presets at all.
* @hide
*/
public static final int STATUS_OPERATION_NOT_SUPPORTED =
IBluetoothHapClient.STATUS_OPERATION_NOT_SUPPORTED;
/**
* Usually means a temporary denial of certain operation. Peer device may report this
* status due to various implementation specific reasons. It's different than
* the {@link #STATUS_OPERATION_NOT_SUPPORTED} which represents more of a
* permanent inability to perform some of the operations.
* @hide
*/
public static final int STATUS_OPERATION_NOT_POSSIBLE =
IBluetoothHapClient.STATUS_OPERATION_NOT_POSSIBLE;
/**
* Used when preset name change failed due to the passed name parameter being to long.
* @hide
*/
public static final int STATUS_INVALID_PRESET_NAME_LENGTH =
IBluetoothHapClient.STATUS_INVALID_PRESET_NAME_LENGTH;
/**
* Group operations are not supported.
* @hide
*/
public static final int STATUS_GROUP_OPERATION_NOT_SUPPORTED =
IBluetoothHapClient.STATUS_GROUP_OPERATION_NOT_SUPPORTED;
/**
* Procedure is already in progress.
* @hide
*/
public static final int STATUS_PROCEDURE_ALREADY_IN_PROGRESS =
IBluetoothHapClient.STATUS_PROCEDURE_ALREADY_IN_PROGRESS;
/**
* Invalid preset index input parameter used in one of the API calls.
* @hide
*/
public static final int STATUS_INVALID_PRESET_INDEX =
IBluetoothHapClient.STATUS_INVALID_PRESET_INDEX;
/**
* Represets an invalid index value. This is usually value returned in a currently
* active preset request for a device which is not connected. This value shouldn't be used
* in the API calls.
* @hide
*/
public static final int PRESET_INDEX_UNAVAILABLE = IBluetoothHapClient.PRESET_INDEX_UNAVAILABLE;
/**
* Feature bit.
* @hide
*/
public static final int FEATURE_BIT_NUM_TYPE_MONAURAL =
IBluetoothHapClient.FEATURE_BIT_NUM_TYPE_MONAURAL;
/**
* Feature bit.
* @hide
*/
public static final int FEATURE_BIT_NUM_TYPE_BANDED =
IBluetoothHapClient.FEATURE_BIT_NUM_TYPE_BANDED;
/**
* Feature bit.
* @hide
*/
public static final int FEATURE_BIT_NUM_SYNCHRONIZATED_PRESETS =
IBluetoothHapClient.FEATURE_BIT_NUM_SYNCHRONIZATED_PRESETS;
/**
* Feature bit.
* @hide
*/
public static final int FEATURE_BIT_NUM_INDEPENDENT_PRESETS =
IBluetoothHapClient.FEATURE_BIT_NUM_INDEPENDENT_PRESETS;
/**
* Feature bit.
* @hide
*/
public static final int FEATURE_BIT_NUM_DYNAMIC_PRESETS =
IBluetoothHapClient.FEATURE_BIT_NUM_DYNAMIC_PRESETS;
/**
* Feature bit.
* @hide
*/
public static final int FEATURE_BIT_NUM_WRITABLE_PRESETS =
IBluetoothHapClient.FEATURE_BIT_NUM_WRITABLE_PRESETS;
/**
* Preset Info notification reason.
* @hide
*/
public static final int PRESET_INFO_REASON_ALL_PRESET_INFO =
IBluetoothHapClient.PRESET_INFO_REASON_ALL_PRESET_INFO;
/**
* Preset Info notification reason.
* @hide
*/
public static final int PRESET_INFO_REASON_PRESET_INFO_UPDATE =
IBluetoothHapClient.PRESET_INFO_REASON_PRESET_INFO_UPDATE;
/**
* Preset Info notification reason.
* @hide
*/
public static final int PRESET_INFO_REASON_PRESET_DELETED =
IBluetoothHapClient.PRESET_INFO_REASON_PRESET_DELETED;
/**
* Preset Info notification reason.
* @hide
*/
public static final int PRESET_INFO_REASON_PRESET_AVAILABILITY_CHANGED =
IBluetoothHapClient.PRESET_INFO_REASON_PRESET_AVAILABILITY_CHANGED;
/**
* Preset Info notification reason.
* @hide
*/
public static final int PRESET_INFO_REASON_PRESET_INFO_REQUEST_RESPONSE =
IBluetoothHapClient.PRESET_INFO_REASON_PRESET_INFO_REQUEST_RESPONSE;
/**
* Represents invalid group identifier. It's returned when user requests a group identifier
* for a device which is not part of any group. This value shouldn't be used in the API calls.
* @hide
*/
public static final int HAP_GROUP_UNAVAILABLE = IBluetoothHapClient.GROUP_ID_UNAVAILABLE;
private final BluetoothAdapter mAdapter;
private final AttributionSource mAttributionSource;
private final BluetoothProfileConnector<IBluetoothHapClient> mProfileConnector =
new BluetoothProfileConnector(this, BluetoothProfile.HAP_CLIENT, "BluetoothHapClient",
IBluetoothHapClient.class.getName()) {
@Override
public IBluetoothHapClient getServiceInterface(IBinder service) {
return IBluetoothHapClient.Stub.asInterface(service);
}
};
/**
* Create a BluetoothHapClient proxy object for interacting with the local
* Bluetooth Hearing Access Profile (HAP) client.
*/
/*package*/ BluetoothHapClient(Context context, ServiceListener listener) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mAttributionSource = mAdapter.getAttributionSource();
mProfileConnector.connect(context, listener);
mCloseGuard = new CloseGuard();
mCloseGuard.open("close");
}
/**
* @hide
*/
protected void finalize() {
if (mCloseGuard != null) {
mCloseGuard.warnIfOpen();
}
close();
}
/**
* @hide
*/
public void close() {
mProfileConnector.disconnect();
}
private IBluetoothHapClient getService() {
return mProfileConnector.getService();
}
/**
* Set connection policy of the profile
*
* <p> The device should already be paired.
* Connection policy can be one of {@link #CONNECTION_POLICY_ALLOWED},
* {@link #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN}
*
* @param device Paired bluetooth device
* @param connectionPolicy is the connection policy to set to for this profile
* @return true if connectionPolicy is set, false on error
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = {
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
})
public boolean setConnectionPolicy(@NonNull BluetoothDevice device,
@ConnectionPolicy int connectionPolicy) {
if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")");
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (mAdapter.isEnabled() && isValidDevice(device)
&& (connectionPolicy == BluetoothProfile.CONNECTION_POLICY_FORBIDDEN
|| connectionPolicy == BluetoothProfile.CONNECTION_POLICY_ALLOWED)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.setConnectionPolicy(device, connectionPolicy, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Get the connection policy of the profile.
*
* <p> The connection policy can be any of:
* {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN},
* {@link #CONNECTION_POLICY_UNKNOWN}
*
* @param device Bluetooth device
* @return connection policy of the device
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public @ConnectionPolicy int getConnectionPolicy(@Nullable BluetoothDevice device) {
if (VDBG) log("getConnectionPolicy(" + device + ")");
final IBluetoothHapClient service = getService();
final int defaultValue = BluetoothProfile.CONNECTION_POLICY_FORBIDDEN;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (mAdapter.isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver();
service.getConnectionPolicy(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* {@inheritDoc}
*/
@Override
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public @NonNull List<BluetoothDevice> getConnectedDevices() {
if (VDBG) Log.d(TAG, "getConnectedDevices()");
final IBluetoothHapClient service = getService();
final List defaultValue = new ArrayList<BluetoothDevice>();
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<List> recv = new SynchronousResultReceiver();
service.getConnectedDevices(mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* {@inheritDoc}
*/
@Override
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public @NonNull List<BluetoothDevice> getDevicesMatchingConnectionStates(
@NonNull int[] states) {
if (VDBG) Log.d(TAG, "getDevicesMatchingConnectionStates()");
final IBluetoothHapClient service = getService();
final List defaultValue = new ArrayList<BluetoothDevice>();
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<List> recv = new SynchronousResultReceiver();
service.getDevicesMatchingConnectionStates(states, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* {@inheritDoc}
*/
@Override
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public @BluetoothProfile.BtProfileState int getConnectionState(
@NonNull BluetoothDevice device) {
if (VDBG) Log.d(TAG, "getConnectionState(" + device + ")");
final IBluetoothHapClient service = getService();
final int defaultValue = BluetoothProfile.STATE_DISCONNECTED;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver();
service.getConnectionState(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Gets the group identifier, which can be used in the group related part of
* the API.
*
* <p>Users are expected to get group identifier for each of the connected
* device to discover the device grouping. This allows them to make an informed
* decision which devices can be controlled by single group API call and which
* require individual device calls.
*
* <p>Note that some binaural HA devices may not support group operations,
* therefore are not considered a valid HAP group. In such case the
* {@link #HAP_GROUP_UNAVAILABLE} is returned even when such
* device is a valid Le Audio Coordinated Set member.
*
* @param device
* @return valid group identifier or {@link #HAP_GROUP_UNAVAILABLE}
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public int getHapGroup(@NonNull BluetoothDevice device) {
final IBluetoothHapClient service = getService();
final int defaultValue = HAP_GROUP_UNAVAILABLE;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver();
service.getHapGroup(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Gets the currently active preset for a HA device
*
* @param device is the device for which we want to set the active preset
* @return active preset index
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public boolean getActivePresetIndex(@NonNull BluetoothDevice device) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.getActivePresetIndex(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Selects the currently active preset for a HA device
*
* @param device is the device for which we want to set the active preset
* @param presetIndex is an index of one of the available presets
* @return true if valid request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean selectActivePreset(@NonNull BluetoothDevice device, int presetIndex) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.selectActivePreset(device, presetIndex, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Selects the currently active preset for a HA device group.
*
* <p> This group call may replace multiple device calls if those are part of the
* valid HAS group. Note that binaural HA devices may or may not support group.
*
* @param groupId is the device group identifier for which want to set the active preset
* @param presetIndex is an index of one of the available presets
* @return true if valid group request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean groupSelectActivePreset(int groupId, int presetIndex) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.groupSelectActivePreset(groupId, presetIndex, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Sets the next preset as a currently active preset for a HA device
*
* <p> Note that the meaning of 'next' is HA device implementation specific and
* does not necessarily mean a higher preset index.
*
* @param device is the device for which we want to set the active preset
* @return true if valid request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean nextActivePreset(@NonNull BluetoothDevice device) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.nextActivePreset(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Sets the next preset as a currently active preset for a HA device group
*
* <p> Note that the meaning of 'next' is HA device implementation specific and
* does not necessarily mean a higher preset index.
* <p> This group call may replace multiple device calls if those are part of the
* valid HAS group. Note that binaural HA devices may or may not support group.
*
* @param groupId is the device group identifier for which want to set the active preset
* @return true if valid group request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean groupNextActivePreset(int groupId) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.groupNextActivePreset(groupId, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Sets the previous preset as a currently active preset for a HA device.
*
* <p> Note that the meaning of 'previous' is HA device implementation specific and
* does not necessarily mean a lower preset index.
*
* @param device is the device for which we want to set the active preset
* @return true if valid request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean previousActivePreset(@NonNull BluetoothDevice device) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.previousActivePreset(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Sets the previous preset as a currently active preset for a HA device group
*
* <p> Note the meaning of 'previous' is HA device implementation specific and
* does not necessarily mean a lower preset index.
* <p> This group call may replace multiple device calls if those are part of the
* valid HAS group. Note that binaural HA devices may or may not support group.
*
* @param groupId is the device group identifier for which want to set the active preset
* @return true if valid group request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean groupPreviousActivePreset(int groupId) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.groupPreviousActivePreset(groupId, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Requests the preset info
*
* @param device is the device for which we want to get the preset name
* @param presetIndex is an index of one of the available presets
* @return true if valid request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean getPresetInfo(@NonNull BluetoothDevice device, int presetIndex) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.getPresetInfo(device, presetIndex, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Requests all presets info
*
* @param device is the device for which we want to get all presets info
* @return true if request was processed, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean getAllPresetsInfo(@NonNull BluetoothDevice device) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.getAllPresetsInfo(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Requests HAP features
*
* @param device is the device for which we want to get features for
* @return true if request was processed, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean getFeatures(@NonNull BluetoothDevice device) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.getFeatures(device, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Sets the preset name
*
* <p> Note that the name length is restricted to 30 characters.
*
* @param device is the device for which we want to get the preset name
* @param presetIndex is an index of one of the available presets
* @param name is a new name for a preset
* @return true if valid request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean setPresetName(@NonNull BluetoothDevice device, int presetIndex,
@NonNull String name) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.setPresetName(device, presetIndex, name, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
/**
* Sets the preset name
*
* <p> Note that the name length is restricted to 30 characters.
*
* @param groupId is the device group identifier
* @param presetIndex is an index of one of the available presets
* @param name is a new name for a preset
* @return true if valid request was sent, false otherwise
* @hide
*/
@RequiresBluetoothConnectPermission
@RequiresPermission(allOf = { android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_PRIVILEGED })
public boolean groupSetPresetName(int groupId, int presetIndex, @NonNull String name) {
final IBluetoothHapClient service = getService();
final boolean defaultValue = false;
if (service == null) {
Log.w(TAG, "Proxy not attached to service");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled()) {
try {
final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
service.groupSetPresetName(groupId, presetIndex, name, mAttributionSource, recv);
return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
} catch (RemoteException | TimeoutException e) {
Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
}
}
return defaultValue;
}
private boolean isEnabled() {
if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
return false;
}
private boolean isValidDevice(BluetoothDevice device) {
if (device == null) return false;
if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
return false;
}
private static void log(String msg) {
Log.d(TAG, msg);
}
}
|