Skip to content

gap: Introduce AdvertisementPayload.ServiceUUIDs() #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions gap.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ type AdvertisementPayload interface {
// UUIDs and 128-bit UUIDs.
HasServiceUUID(UUID) bool

// ServiceUUIDs returns all of the Service Class UUIDs present in the
// advertisement payload. Where possible, using HasServiceUUID() is preferred
// because this may need to construct the UUIDs on the fly. The caller may
// not modify the returned UUIDs.
ServiceUUIDs() []UUID

// Bytes returns the raw advertisement packet, if available. It returns nil
// if this data is not available.
Bytes() []byte
Expand Down Expand Up @@ -218,6 +224,12 @@ func (p *advertisementFields) HasServiceUUID(uuid UUID) bool {
return false
}

// ServiceUUIDs returns the set of Service Class UUIDs present in the
// advertisement payload. The caller may not modify the returned UUIDs.
func (p *advertisementFields) ServiceUUIDs() []UUID {
return p.AdvertisementFields.ServiceUUIDs
}

// Bytes returns nil, as structured advertisement data does not have the
// original raw advertisement data available.
func (p *advertisementFields) Bytes() []byte {
Expand Down Expand Up @@ -322,6 +334,27 @@ func (buf *rawAdvertisementPayload) HasServiceUUID(uuid UUID) bool {
}
}

// ServiceUUIDs returns the set of Service Class UUIDs in the advertisement
// payload. Both 16-bit UUIDs and 128-bit UUIDs will be included.
func (buf *rawAdvertisementPayload) ServiceUUIDs() []UUID {
var uuids []UUID
b := buf.findField(0x03) // Complete List of 16-bit Service Class UUIDs
if len(b) == 0 {
b = buf.findField(0x02) // Incomplete List of 16-bit Service Class UUIDs
}
for i := 0; i < len(b)/2; i++ {
uuids = append(uuids, New16BitUUID(uint16(b[i*2])|(uint16(b[i*2+1])<<8)))
}
b = buf.findField(0x07) // Complete List of 128-bit Service Class UUIDs
if len(b) == 0 {
b = buf.findField(0x06) // Incomplete List of 128-bit Service Class UUIDs
}
for i := 0; i < len(b)/16; i++ {
uuids = append(uuids, NewUUID([16]byte(b[i*16:i*16+16])))
}
return uuids
}

// ManufacturerData returns the manufacturer data in the advertisement payload.
func (buf *rawAdvertisementPayload) ManufacturerData() []ManufacturerDataElement {
var manufacturerData []ManufacturerDataElement
Expand Down
Loading