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
|
/*
* Copyright 2022 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.
*/
package android.bluetooth;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.List;
/**
* This class represents a Broadcast Source group and the associated information that is needed
* by Broadcast Audio Scan Service (BASS) to set up a Broadcast Sink.
*
* <p>For example, an LE Audio Broadcast Sink can use the information contained within an instance
* of this class to synchronize with an LE Audio Broadcast group in order to listen to audio from
* Broadcast subgroup using one or more Broadcast Channels.
*
* @hide
*/
@SystemApi
public final class BluetoothLeBroadcastMetadata implements Parcelable {
// Information needed for adding broadcast Source
// Optional: Identity address type
private final @BluetoothDevice.AddressType int mSourceAddressType;
// Optional: Must use identity address
private final BluetoothDevice mSourceDevice;
private final int mSourceAdvertisingSid;
private final int mBroadcastId;
private final int mPaSyncInterval;
private final boolean mIsEncrypted;
private final byte[] mBroadcastCode;
// BASE structure
// See Section 7 for description. Range: 0x000000 – 0xFFFFFF Units: μs
//All other values: RFU
private final int mPresentationDelayMicros;
// Number of subgroups used to group BISes present in the BIG
//Shall be at least 1, as defined by Rule 1
// Sub group info numSubGroup = mSubGroups.length
private final List<BluetoothLeBroadcastSubgroup> mSubgroups;
private BluetoothLeBroadcastMetadata(int sourceAddressType,
BluetoothDevice sourceDevice, int sourceAdvertisingSid, int broadcastId,
int paSyncInterval, boolean isEncrypted, byte[] broadcastCode, int presentationDelay,
List<BluetoothLeBroadcastSubgroup> subgroups) {
mSourceAddressType = sourceAddressType;
mSourceDevice = sourceDevice;
mSourceAdvertisingSid = sourceAdvertisingSid;
mBroadcastId = broadcastId;
mPaSyncInterval = paSyncInterval;
mIsEncrypted = isEncrypted;
mBroadcastCode = broadcastCode;
mPresentationDelayMicros = presentationDelay;
mSubgroups = subgroups;
}
/**
* Get the address type of the Broadcast Source.
*
* Can be either {@link BluetoothDevice#ADDRESS_TYPE_PUBLIC},
* {@link BluetoothDevice#ADDRESS_TYPE_RANDOM}
*
* @return address type of the Broadcast Source
* @hide
*/
@SystemApi
public @BluetoothDevice.AddressType int getSourceAddressType() {
return mSourceAddressType;
}
/**
* Get the MAC address of the Broadcast Source, which can be Public Device Address,
* Random Device Address, Public Identity Address or Random (static) Identity Address.
*
* @return MAC address of the Broadcast Source
* @hide
*/
@SystemApi
public @Nullable BluetoothDevice getSourceDevice() {
return mSourceDevice;
}
/**
* Get Advertising_SID subfield of the ADI field of the AUX_ADV_IND PDU or the
* LL_PERIODIC_SYNC_IND containing the SyncInfo that points to the PA transmitted by the
* Broadcast Source.
*
* @return 1-byte long Advertising_SID of the Broadcast Source
* @hide
*/
@SystemApi
public int getSourceAdvertisingSid() {
return mSourceAdvertisingSid;
}
/**
* Broadcast_ID of the Broadcast Source.
*
* @return 3-byte long Broadcast_ID of the Broadcast Source
* @hide
*/
@SystemApi
public int getBroadcastId() {
return mBroadcastId;
}
/**
* Indicated that Periodic Advertising Sync interval is unknown.
* @hide
*/
@SystemApi
public static final int PA_SYNC_INTERVAL_UNKNOWN = 0xFFFF;
/**
* Get Periodic Advertising Sync interval of the broadcast Source.
*
* @return Periodic Advertising Sync interval of the broadcast Source,
* {@link #PA_SYNC_INTERVAL_UNKNOWN} if unknown
* @hide
*/
@SystemApi
public int getPaSyncInterval() {
return mPaSyncInterval;
}
/**
* Return true if the Broadcast Source is encrypted.
*
* @return true if the Broadcast Source is encrypted
* @hide
*/
@SystemApi
public boolean isEncrypted() {
return mIsEncrypted;
}
/**
* Get the Broadcast Code currently set for this Broadcast Source.
*
* Only needed when encryption is enabled
*
* <p>As defined in Volume 3, Part C, Section 3.2.6 of Bluetooth Core Specification, Version
* 5.3, Broadcast Code is used to encrypt a broadcast audio stream.
* <p>It must be a UTF-8 string that has at least 4 octets and should not exceed 16 octets.
*
* @return Broadcast Code currently set for this Broadcast Source, null if code is not required
* or code is currently unknown
* @hide
*/
@SystemApi
public @Nullable byte[] getBroadcastCode() {
return mBroadcastCode;
}
/**
* Get the overall presentation delay in microseconds of this Broadcast Source.
*
* Presentation delay is defined in Section 7 of the Basic Audio Profile.
*
* @return presentation delay of this Broadcast Source in microseconds
* @hide
*/
@SystemApi
public @IntRange(from = 0, to = 0xFFFFFF) int getPresentationDelayMicros() {
return mPresentationDelayMicros;
}
/**
* Get available subgroups in this broadcast source.
*
* @return list of subgroups in this broadcast source, which should contain at least one
* subgroup for each Broadcast Source
* @hide
*/
@SystemApi
public @NonNull List<BluetoothLeBroadcastSubgroup> getSubgroups() {
return mSubgroups;
}
/**
* {@inheritDoc}
* @hide
*/
@Override
public int describeContents() {
return 0;
}
/**
* {@inheritDoc}
* @hide
*/
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mSourceAddressType);
if (mSourceDevice != null) {
out.writeInt(1);
out.writeTypedObject(mSourceDevice, 0);
} else {
// zero indicates missing mSourceDevice
out.writeInt(0);
}
out.writeInt(mSourceAdvertisingSid);
out.writeInt(mBroadcastId);
out.writeInt(mPaSyncInterval);
out.writeBoolean(mIsEncrypted);
if (mBroadcastCode != null) {
out.writeInt(mBroadcastCode.length);
out.writeByteArray(mBroadcastCode);
} else {
// -1 indicates missing broadcast code
out.writeInt(-1);
}
out.writeInt(mPresentationDelayMicros);
out.writeTypedList(mSubgroups);
}
/**
* A {@link Parcelable.Creator} to create {@link BluetoothLeBroadcastMetadata} from parcel.
* @hide
*/
@SystemApi
public static final @NonNull Parcelable.Creator<BluetoothLeBroadcastMetadata> CREATOR =
new Parcelable.Creator<BluetoothLeBroadcastMetadata>() {
public @NonNull BluetoothLeBroadcastMetadata createFromParcel(@NonNull Parcel in) {
Builder builder = new Builder();
final int sourceAddressType = in.readInt();
final int deviceExist = in.readInt();
BluetoothDevice sourceDevice = null;
if (deviceExist == 1) {
sourceDevice = in.readTypedObject(BluetoothDevice.CREATOR);
}
builder.setSourceDevice(sourceDevice, sourceAddressType);
builder.setSourceAdvertisingSid(in.readInt());
builder.setBroadcastId(in.readInt());
builder.setPaSyncInterval(in.readInt());
builder.setEncrypted(in.readBoolean());
final int codeLen = in.readInt();
byte[] broadcastCode = null;
if (codeLen != -1) {
broadcastCode = new byte[codeLen];
if (codeLen > 0) {
in.readByteArray(broadcastCode);
}
}
builder.setBroadcastCode(broadcastCode);
builder.setPresentationDelayMicros(in.readInt());
final List<BluetoothLeBroadcastSubgroup> subgroups = new ArrayList<>();
in.readTypedList(subgroups, BluetoothLeBroadcastSubgroup.CREATOR);
for (BluetoothLeBroadcastSubgroup subgroup : subgroups) {
builder.addSubgroup(subgroup);
}
return builder.build();
}
public @NonNull BluetoothLeBroadcastMetadata[] newArray(int size) {
return new BluetoothLeBroadcastMetadata[size];
}
};
private static final int UNKNOWN_VALUE_PLACEHOLDER = -1;
/**
* Builder for {@link BluetoothLeBroadcastMetadata}.
* @hide
*/
@SystemApi
public static final class Builder {
private @BluetoothDevice.AddressType int mSourceAddressType =
BluetoothDevice.ADDRESS_TYPE_UNKNOWN;
private BluetoothDevice mSourceDevice = null;
private int mSourceAdvertisingSid = UNKNOWN_VALUE_PLACEHOLDER;
private int mBroadcastId = UNKNOWN_VALUE_PLACEHOLDER;
private int mPaSyncInterval = UNKNOWN_VALUE_PLACEHOLDER;
private boolean mIsEncrypted = false;
private byte[] mBroadcastCode = null;
private int mPresentationDelayMicros = UNKNOWN_VALUE_PLACEHOLDER;
private List<BluetoothLeBroadcastSubgroup> mSubgroups = new ArrayList<>();
/**
* Create an empty builder.
* @hide
*/
@SystemApi
public Builder() {}
/**
* Create a builder with copies of information from original object.
*
* @param original original object
* @hide
*/
@SystemApi
public Builder(@NonNull BluetoothLeBroadcastMetadata original) {
mSourceAddressType = original.getSourceAddressType();
mSourceDevice = original.getSourceDevice();
mSourceAdvertisingSid = original.getSourceAdvertisingSid();
mBroadcastId = original.getBroadcastId();
mPaSyncInterval = original.getPaSyncInterval();
mIsEncrypted = original.isEncrypted();
mBroadcastCode = original.getBroadcastCode();
mPresentationDelayMicros = original.getPresentationDelayMicros();
mSubgroups = original.getSubgroups();
}
/**
* Set the address type and MAC address of the Broadcast Source.
*
* Address type can be either {@link BluetoothDevice#ADDRESS_TYPE_PUBLIC},
* {@link BluetoothDevice#ADDRESS_TYPE_RANDOM}
*
* MAC address can be Public Device Address, Random Device Address, Public Identity Address
* or Random (static) Identity Address
*
* @param sourceDevice source advertiser address
* @param sourceAddressType source advertiser address type
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setSourceDevice(@Nullable BluetoothDevice sourceDevice,
@BluetoothDevice.AddressType int sourceAddressType) {
mSourceDevice = sourceDevice;
mSourceAddressType = sourceAddressType;
return this;
}
/**
* Set Advertising_SID that is a subfield of the ADI field of the AUX_ADV_IND PDU or the
* LL_PERIODIC_SYNC_IND containing the SyncInfo that points to the PA transmitted by the
* Broadcast Source.
*
* @param sourceAdvertisingSid 1-byte long Advertising_SID of the Broadcast Source
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setSourceAdvertisingSid(int sourceAdvertisingSid) {
mSourceAdvertisingSid = sourceAdvertisingSid;
return this;
}
/**
* Set the Broadcast_ID of the Broadcast Source.
*
* @param broadcastId 3-byte long Broadcast_ID of the Broadcast Source
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setBroadcastId(int broadcastId) {
mBroadcastId = broadcastId;
return this;
}
/**
* Set Periodic Advertising Sync interval of the broadcast Source.
*
* @param paSyncInterval Periodic Advertising Sync interval of the broadcast Source,
* {@link #PA_SYNC_INTERVAL_UNKNOWN} if unknown
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setPaSyncInterval(int paSyncInterval) {
mPaSyncInterval = paSyncInterval;
return this;
}
/**
* Set whether the Broadcast Source should be encrypted.
*
* When setting up a Broadcast Source, if <var>isEncrypted</var> is true while
* <var>broadcastCode</var> is null, the implementation will automatically generate
* a Broadcast Code
*
* @param isEncrypted whether the Broadcast Source is encrypted
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setEncrypted(boolean isEncrypted) {
mIsEncrypted = isEncrypted;
return this;
}
/**
* Set the Broadcast Code currently set for this Broadcast Source.
*
* Only needed when encryption is enabled
*
* <p>As defined in Volume 3, Part C, Section 3.2.6 of Bluetooth Core Specification, Version
* 5.3, Broadcast Code is used to encrypt a broadcast audio stream.
* <p>It must be a UTF-8 string that has at least 4 octets and should not exceed 16 octets.
*
* @param broadcastCode Broadcast Code for this Broadcast Source, null if code is not
* required
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setBroadcastCode(@Nullable byte[] broadcastCode) {
mBroadcastCode = broadcastCode;
return this;
}
/**
* Set the overall presentation delay in microseconds of this Broadcast Source.
*
* Presentation delay is defined in Section 7 of the Basic Audio Profile.
*
* @param presentationDelayMicros presentation delay of this Broadcast Source in
* microseconds
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder setPresentationDelayMicros(
@IntRange(from = 0, to = 0xFFFFFF) int presentationDelayMicros) {
mPresentationDelayMicros = presentationDelayMicros;
return this;
}
/**
* Add a subgroup to this broadcast source.
*
* @param subgroup {@link BluetoothLeBroadcastSubgroup} that contains a subgroup's metadata
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder addSubgroup(@NonNull BluetoothLeBroadcastSubgroup subgroup) {
mSubgroups.add(subgroup);
return this;
}
/**
* Clear subgroup list so that one can reset the builder after create it from an existing
* object.
*
* @return this builder
* @hide
*/
@SystemApi
public @NonNull Builder clearSubgroup() {
mSubgroups.clear();
return this;
}
/**
* Build {@link BluetoothLeBroadcastMetadata}.
*
* @return {@link BluetoothLeBroadcastMetadata}
* @throws IllegalArgumentException if the object cannot be built
* @hide
*/
@SystemApi
public @NonNull BluetoothLeBroadcastMetadata build() {
return new BluetoothLeBroadcastMetadata(mSourceAddressType, mSourceDevice,
mSourceAdvertisingSid, mBroadcastId, mPaSyncInterval, mIsEncrypted,
mBroadcastCode, mPresentationDelayMicros, mSubgroups);
}
}
}
|