Skip to content

Commit

Permalink
hci: when advertising, do not send ADFlags if we are using Advertisin…
Browse files Browse the repository at this point in the history
…gTypeNonConnInd

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jan 9, 2025
1 parent 0cdcf6d commit 48aec1a
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions gap_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"errors"
"slices"
"strconv"
"time"
)

Expand Down Expand Up @@ -353,6 +354,8 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
return nil
}

const maxAdvLen = 31

// Start advertisement. May only be called after it has been configured.
func (a *Advertisement) Start() error {
// uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
Expand All @@ -368,13 +371,16 @@ func (a *Advertisement) Start() error {
return err
}

var advertisingData [31]byte
var advertisingData [maxAdvLen]byte
advertisingDataLen := uint8(0)

advertisingData[0] = 0x02 // length
advertisingData[1] = ADFlags
advertisingData[2] = ADTypeGeneralDiscoverable + ADTypeFlagsBREDRNotSupported
advertisingDataLen += 3
// flags, only if not non-connectable
if a.advertisementType != AdvertisingTypeNonConnInd {
advertisingData[0] = 0x02 // length
advertisingData[1] = ADFlags
advertisingData[2] = ADTypeGeneralDiscoverable + ADTypeFlagsBREDRNotSupported
advertisingDataLen += 3
}

// TODO: handle multiple service UUIDs
if len(a.serviceUUIDs) == 1 {
Expand All @@ -392,15 +398,15 @@ func (a *Advertisement) Start() error {
copy(advertisingData[5:], data[:])
}

advertisingData[3] = 0x03 // length
advertisingData[4] = ADCompleteAdvertisedService16
advertisingData[advertisingDataLen] = 0x03 // length
advertisingData[advertisingDataLen+1] = ADCompleteAdvertisedService16
advertisingDataLen += sz + 2
}

if len(a.manufacturerData) > 0 {
for _, md := range a.manufacturerData {
if advertisingDataLen+4+uint8(len(md.Data)) > 31 {
return errors.New("ManufacturerData too long")
if advertisingDataLen+4+uint8(len(md.Data)) > maxAdvLen {
return errors.New("ManufacturerData too long:" + strconv.Itoa(int(advertisingDataLen+4+uint8(len(md.Data)))))
}

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

0 comments on commit 48aec1a

Please sign in to comment.