6
6
"encoding/binary"
7
7
"errors"
8
8
"slices"
9
+ "strconv"
9
10
"time"
10
11
)
11
12
@@ -353,6 +354,11 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
353
354
return nil
354
355
}
355
356
357
+ // via https://www.bluetooth.com/wp-content/uploads/Files/Specification/HTML/Core-54/out/en/low-energy-controller/link-layer-specification.html
358
+ // 4.4.3.5. Advertising reports
359
+ // The maximum size of the advertising report is 31 bytes.
360
+ const maxAdvLen = 31
361
+
356
362
// Start advertisement. May only be called after it has been configured.
357
363
func (a * Advertisement ) Start () error {
358
364
// uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
@@ -368,13 +374,16 @@ func (a *Advertisement) Start() error {
368
374
return err
369
375
}
370
376
371
- var advertisingData [31 ]byte
377
+ var advertisingData [maxAdvLen ]byte
372
378
advertisingDataLen := uint8 (0 )
373
379
374
- advertisingData [0 ] = 0x02 // length
375
- advertisingData [1 ] = ADFlags
376
- advertisingData [2 ] = ADTypeGeneralDiscoverable + ADTypeFlagsBREDRNotSupported
377
- advertisingDataLen += 3
380
+ // flags, only if not non-connectable
381
+ if a .advertisementType != AdvertisingTypeNonConnInd {
382
+ advertisingData [0 ] = 0x02 // length
383
+ advertisingData [1 ] = ADFlags
384
+ advertisingData [2 ] = ADTypeGeneralDiscoverable + ADTypeFlagsBREDRNotSupported
385
+ advertisingDataLen += 3
386
+ }
378
387
379
388
// TODO: handle multiple service UUIDs
380
389
if len (a .serviceUUIDs ) == 1 {
@@ -392,15 +401,15 @@ func (a *Advertisement) Start() error {
392
401
copy (advertisingData [5 :], data [:])
393
402
}
394
403
395
- advertisingData [3 ] = 0x03 // length
396
- advertisingData [4 ] = ADCompleteAdvertisedService16
404
+ advertisingData [advertisingDataLen ] = 0x03 // length
405
+ advertisingData [advertisingDataLen + 1 ] = ADCompleteAdvertisedService16
397
406
advertisingDataLen += sz + 2
398
407
}
399
408
400
409
if len (a .manufacturerData ) > 0 {
401
410
for _ , md := range a .manufacturerData {
402
- if advertisingDataLen + 4 + uint8 (len (md .Data )) > 31 {
403
- return errors .New ("ManufacturerData too long" )
411
+ if advertisingDataLen + 4 + uint8 (len (md .Data )) > maxAdvLen {
412
+ return errors .New ("ManufacturerData too long:" + strconv . Itoa ( int ( advertisingDataLen + 4 + uint8 ( len ( md . Data )))) )
404
413
}
405
414
406
415
advertisingData [advertisingDataLen ] = 3 + uint8 (len (md .Data )) // length
0 commit comments