Skip to content

Commit 1fd1367

Browse files
committed
hci: when advertising, do not send ADFlags if we are using AdvertisingTypeNonConnInd
Signed-off-by: deadprogram <[email protected]>
1 parent ab457cb commit 1fd1367

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

gap_hci.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/binary"
77
"errors"
88
"slices"
9+
"strconv"
910
"time"
1011
)
1112

@@ -353,6 +354,11 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
353354
return nil
354355
}
355356

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+
356362
// Start advertisement. May only be called after it has been configured.
357363
func (a *Advertisement) Start() error {
358364
// uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
@@ -368,13 +374,16 @@ func (a *Advertisement) Start() error {
368374
return err
369375
}
370376

371-
var advertisingData [31]byte
377+
var advertisingData [maxAdvLen]byte
372378
advertisingDataLen := uint8(0)
373379

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+
}
378387

379388
// TODO: handle multiple service UUIDs
380389
if len(a.serviceUUIDs) == 1 {
@@ -392,15 +401,15 @@ func (a *Advertisement) Start() error {
392401
copy(advertisingData[5:], data[:])
393402
}
394403

395-
advertisingData[3] = 0x03 // length
396-
advertisingData[4] = ADCompleteAdvertisedService16
404+
advertisingData[advertisingDataLen] = 0x03 // length
405+
advertisingData[advertisingDataLen+1] = ADCompleteAdvertisedService16
397406
advertisingDataLen += sz + 2
398407
}
399408

400409
if len(a.manufacturerData) > 0 {
401410
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)))))
404413
}
405414

406415
advertisingData[advertisingDataLen] = 3 + uint8(len(md.Data)) // length

0 commit comments

Comments
 (0)