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
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* 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.
*
* Changes from Qualcomm Innovation Center are provided under the following license:
* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*
*/
package android.bluetooth;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.ParcelUuid;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.UUID;
/**
* Static helper methods and constants to decode the ParcelUuid of remote devices. Bluetooth service
* UUIDs are defined in the SDP section of the Bluetooth Assigned Numbers document. The constant
* 128 bit values in this class are calculated as: uuid * 2^96 + {@link #BASE_UUID}.
*
* @hide
*/
@SystemApi
@SuppressLint("AndroidFrameworkBluetoothPermission")
public final class BluetoothUuid {
/**
* UUID corresponding to the Audio sink role (also referred to as the A2DP sink role).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid A2DP_SINK =
ParcelUuid.fromString("0000110B-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Audio source role (also referred to as the A2DP source role).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid A2DP_SOURCE =
ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Advanced Audio Distribution Profile (A2DP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid ADV_AUDIO_DIST =
ParcelUuid.fromString("0000110D-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Headset Profile (HSP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HSP =
ParcelUuid.fromString("00001108-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Headset Profile (HSP) Audio Gateway role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HSP_AG =
ParcelUuid.fromString("00001112-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Hands-Free Profile (HFP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HFP =
ParcelUuid.fromString("0000111E-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Hands-Free Profile (HFP) Audio Gateway role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HFP_AG =
ParcelUuid.fromString("0000111F-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Audio Video Remote Control Profile (AVRCP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid AVRCP =
ParcelUuid.fromString("0000110E-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Audio Video Remote Control Profile (AVRCP) controller role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid AVRCP_CONTROLLER =
ParcelUuid.fromString("0000110F-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Audio Video Remote Control Profile (AVRCP) target role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid AVRCP_TARGET =
ParcelUuid.fromString("0000110C-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the OBject EXchange (OBEX) Object Push Profile (OPP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid OBEX_OBJECT_PUSH =
ParcelUuid.fromString("00001105-0000-1000-8000-00805f9b34fb");
/**
* UUID corresponding to the Human Interface Device (HID) profile.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HID =
ParcelUuid.fromString("00001124-0000-1000-8000-00805f9b34fb");
/**
* UUID corresponding to the Human Interface Device over GATT Profile (HOGP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HOGP =
ParcelUuid.fromString("00001812-0000-1000-8000-00805f9b34fb");
/**
* UUID corresponding to the Personal Area Network User (PANU) role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid PANU =
ParcelUuid.fromString("00001115-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Network Access Point (NAP) role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid NAP =
ParcelUuid.fromString("00001116-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Bluetooth Network Encapsulation Protocol (BNEP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid BNEP =
ParcelUuid.fromString("0000000f-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Phonebook Access Profile (PBAP) client role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid PBAP_PCE =
ParcelUuid.fromString("0000112e-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Phonebook Access Profile (PBAP) server role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid PBAP_PSE =
ParcelUuid.fromString("0000112f-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Message Access Profile (MAP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid MAP =
ParcelUuid.fromString("00001134-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Message Notification Server (MNS) role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid MNS =
ParcelUuid.fromString("00001133-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Message Access Server (MAS) role.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid MAS =
ParcelUuid.fromString("00001132-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Sim Access Profile (SAP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid SAP =
ParcelUuid.fromString("0000112D-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Hearing Aid Profile.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HEARING_AID =
ParcelUuid.fromString("0000FDF0-0000-1000-8000-00805f9b34fb");
/**
* UUID corresponding to the Hearing Access Service (HAS).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid HAS =
ParcelUuid.fromString("00001854-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to Audio Stream Control (also known as Bluetooth Low Energy Audio).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid LE_AUDIO =
ParcelUuid.fromString("0000184E-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Device Identification Profile (DIP).
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid DIP =
ParcelUuid.fromString("00001200-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Volume Control Service.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid VOLUME_CONTROL =
ParcelUuid.fromString("00001844-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Generic Media Control Service.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid GENERIC_MEDIA_CONTROL =
ParcelUuid.fromString("00001849-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Media Control Service.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid MEDIA_CONTROL =
ParcelUuid.fromString("00001848-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Coordinated Set Identification Service.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid COORDINATED_SET =
ParcelUuid.fromString("00001846-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Common Audio Service.
*
* @hide
*/
@NonNull
@SystemApi
public static final ParcelUuid CAP =
ParcelUuid.fromString("00001853-0000-1000-8000-00805F9B34FB");
/**
* UUID corresponding to the Broadcast Audio Scan Service (also known as LE Audio Broadcast
* Assistant).
*
* @hide
*/
@NonNull
public static final ParcelUuid BATTERY =
ParcelUuid.fromString("0000180F-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
@SystemApi
public static final ParcelUuid BASS =
ParcelUuid.fromString("0000184F-0000-1000-8000-00805F9B34FB");
/**
* Telephony and Media Audio Profile (TMAP) UUID
* @hide
*/
@NonNull
public static final ParcelUuid TMAP =
ParcelUuid.fromString("00001855-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
@SystemApi
public static final ParcelUuid BASE_UUID =
ParcelUuid.fromString("00000000-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
public static ParcelUuid ADVANCE_HEARINGAID_UUID =
ParcelUuid.fromString("00006AD2-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
public static ParcelUuid ADVANCE_MEDIA_T_UUID =
ParcelUuid.fromString("00006AD0-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
public static ParcelUuid ADVANCE_MEDIA_P_UUID =
ParcelUuid.fromString("00006AD1-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
public static ParcelUuid ADVANCE_MEDIA_G_UUID =
ParcelUuid.fromString("00006AD3-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
public static ParcelUuid ADVANCE_MEDIA_W_UUID =
ParcelUuid.fromString("2587db3c-ce70-4fc9-935f-777ab4188fd7");
/** @hide */
@NonNull
public static ParcelUuid ADVANCE_VOICE_P_UUID =
ParcelUuid.fromString("00006AD4-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
public static ParcelUuid ADVANCE_VOICE_T_UUID =
ParcelUuid.fromString("00006AD5-0000-1000-8000-00805F9B34FB");
/** @hide */
@NonNull
public static ParcelUuid CONSTANT_TONE_EXT_UUID =
ParcelUuid.fromString("0000184A-0000-1000-8000-00805F9B34FB");
/**
* Length of bytes for 16 bit UUID
*
* @hide
*/
@SystemApi
public static final int UUID_BYTES_16_BIT = 2;
/**
* Length of bytes for 32 bit UUID
*
* @hide
*/
@SystemApi
public static final int UUID_BYTES_32_BIT = 4;
/**
* Length of bytes for 128 bit UUID
*
* @hide
*/
@SystemApi
public static final int UUID_BYTES_128_BIT = 16;
/**
* Returns true if there any common ParcelUuids in uuidA and uuidB.
*
* @param uuidA - List of ParcelUuids
* @param uuidB - List of ParcelUuids
*
* @hide
*/
@SystemApi
public static boolean containsAnyUuid(@Nullable ParcelUuid[] uuidA,
@Nullable ParcelUuid[] uuidB) {
if (uuidA == null && uuidB == null) return true;
if (uuidA == null) {
return uuidB.length == 0;
}
if (uuidB == null) {
return uuidA.length == 0;
}
HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
for (ParcelUuid uuid : uuidB) {
if (uuidSet.contains(uuid)) return true;
}
return false;
}
/**
* Extract the Service Identifier or the actual uuid from the Parcel Uuid.
* For example, if 0000110B-0000-1000-8000-00805F9B34FB is the parcel Uuid,
* this function will return 110B
*
* @param parcelUuid
* @return the service identifier.
*/
private static int getServiceIdentifierFromParcelUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
long value = (uuid.getMostSignificantBits() & 0xFFFFFFFF00000000L) >>> 32;
return (int) value;
}
/**
* Parse UUID from bytes. The {@code uuidBytes} can represent a 16-bit, 32-bit or 128-bit UUID,
* but the returned UUID is always in 128-bit format.
* Note UUID is little endian in Bluetooth.
*
* @param uuidBytes Byte representation of uuid.
* @return {@link ParcelUuid} parsed from bytes.
* @throws IllegalArgumentException If the {@code uuidBytes} cannot be parsed.
*
* @hide
*/
@NonNull
@SystemApi
public static ParcelUuid parseUuidFrom(@Nullable byte[] uuidBytes) {
if (uuidBytes == null) {
throw new IllegalArgumentException("uuidBytes cannot be null");
}
int length = uuidBytes.length;
if (length != UUID_BYTES_16_BIT && length != UUID_BYTES_32_BIT
&& length != UUID_BYTES_128_BIT) {
throw new IllegalArgumentException("uuidBytes length invalid - " + length);
}
// Construct a 128 bit UUID.
if (length == UUID_BYTES_128_BIT) {
ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
long msb = buf.getLong(8);
long lsb = buf.getLong(0);
return new ParcelUuid(new UUID(msb, lsb));
}
// For 16 bit and 32 bit UUID we need to convert them to 128 bit value.
// 128_bit_value = uuid * 2^96 + BASE_UUID
long shortUuid;
if (length == UUID_BYTES_16_BIT) {
shortUuid = uuidBytes[0] & 0xFF;
shortUuid += (uuidBytes[1] & 0xFF) << 8;
} else {
shortUuid = uuidBytes[0] & 0xFF;
shortUuid += (uuidBytes[1] & 0xFF) << 8;
shortUuid += (uuidBytes[2] & 0xFF) << 16;
shortUuid += (uuidBytes[3] & 0xFF) << 24;
}
long msb = BASE_UUID.getUuid().getMostSignificantBits() + (shortUuid << 32);
long lsb = BASE_UUID.getUuid().getLeastSignificantBits();
return new ParcelUuid(new UUID(msb, lsb));
}
/**
* Parse UUID to bytes. The returned value is shortest representation, a 16-bit, 32-bit or
* 128-bit UUID, Note returned value is little endian (Bluetooth).
*
* @param uuid uuid to parse.
* @return shortest representation of {@code uuid} as bytes.
* @throws IllegalArgumentException If the {@code uuid} is null.
*
* @hide
*/
public static byte[] uuidToBytes(ParcelUuid uuid) {
if (uuid == null) {
throw new IllegalArgumentException("uuid cannot be null");
}
if (is16BitUuid(uuid)) {
byte[] uuidBytes = new byte[UUID_BYTES_16_BIT];
int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
uuidBytes[0] = (byte) (uuidVal & 0xFF);
uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8);
return uuidBytes;
}
if (is32BitUuid(uuid)) {
byte[] uuidBytes = new byte[UUID_BYTES_32_BIT];
int uuidVal = getServiceIdentifierFromParcelUuid(uuid);
uuidBytes[0] = (byte) (uuidVal & 0xFF);
uuidBytes[1] = (byte) ((uuidVal & 0xFF00) >> 8);
uuidBytes[2] = (byte) ((uuidVal & 0xFF0000) >> 16);
uuidBytes[3] = (byte) ((uuidVal & 0xFF000000) >> 24);
return uuidBytes;
}
// Construct a 128 bit UUID.
long msb = uuid.getUuid().getMostSignificantBits();
long lsb = uuid.getUuid().getLeastSignificantBits();
byte[] uuidBytes = new byte[UUID_BYTES_128_BIT];
ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN);
buf.putLong(8, msb);
buf.putLong(0, lsb);
return uuidBytes;
}
/**
* Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 16 bit uuid, false otherwise.
*
* @hide
*/
@UnsupportedAppUsage
public static boolean is16BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFF0000FFFFFFFFL) == 0x1000L);
}
/**
* Check whether the given parcelUuid can be converted to 32 bit bluetooth uuid.
*
* @param parcelUuid
* @return true if the parcelUuid can be converted to 32 bit uuid, false otherwise.
*
* @hide
*/
@UnsupportedAppUsage
public static boolean is32BitUuid(ParcelUuid parcelUuid) {
UUID uuid = parcelUuid.getUuid();
if (uuid.getLeastSignificantBits() != BASE_UUID.getUuid().getLeastSignificantBits()) {
return false;
}
if (is16BitUuid(parcelUuid)) {
return false;
}
return ((uuid.getMostSignificantBits() & 0xFFFFFFFFL) == 0x1000L);
}
private BluetoothUuid() {}
}
|