Skip to content

Commit a6c741b

Browse files
mookdeadprogram
authored andcommitted
gap: Introduce AdvertisementPayload.ServiceUUIDs()
This adds a new method that returns all of the Service Class UUIDs found in the advertisement payload. This may require allocating a new slice of UUIDs, hence using HasServiceUUID where possible is still preferred. Fixes #363
1 parent baba482 commit a6c741b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

gap.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ type AdvertisementPayload interface {
163163
// UUIDs and 128-bit UUIDs.
164164
HasServiceUUID(UUID) bool
165165

166+
// ServiceUUIDs returns all of the Service Class UUIDs present in the
167+
// advertisement payload. Where possible, using HasServiceUUID() is preferred
168+
// because this may need to construct the UUIDs on the fly. The caller may
169+
// not modify the returned UUIDs.
170+
ServiceUUIDs() []UUID
171+
166172
// Bytes returns the raw advertisement packet, if available. It returns nil
167173
// if this data is not available.
168174
Bytes() []byte
@@ -218,6 +224,12 @@ func (p *advertisementFields) HasServiceUUID(uuid UUID) bool {
218224
return false
219225
}
220226

227+
// ServiceUUIDs returns the set of Service Class UUIDs present in the
228+
// advertisement payload. The caller may not modify the returned UUIDs.
229+
func (p *advertisementFields) ServiceUUIDs() []UUID {
230+
return p.AdvertisementFields.ServiceUUIDs
231+
}
232+
221233
// Bytes returns nil, as structured advertisement data does not have the
222234
// original raw advertisement data available.
223235
func (p *advertisementFields) Bytes() []byte {
@@ -322,6 +334,27 @@ func (buf *rawAdvertisementPayload) HasServiceUUID(uuid UUID) bool {
322334
}
323335
}
324336

337+
// ServiceUUIDs returns the set of Service Class UUIDs in the advertisement
338+
// payload. Both 16-bit UUIDs and 128-bit UUIDs will be included.
339+
func (buf *rawAdvertisementPayload) ServiceUUIDs() []UUID {
340+
var uuids []UUID
341+
b := buf.findField(0x03) // Complete List of 16-bit Service Class UUIDs
342+
if len(b) == 0 {
343+
b = buf.findField(0x02) // Incomplete List of 16-bit Service Class UUIDs
344+
}
345+
for i := 0; i < len(b)/2; i++ {
346+
uuids = append(uuids, New16BitUUID(uint16(b[i*2])|(uint16(b[i*2+1])<<8)))
347+
}
348+
b = buf.findField(0x07) // Complete List of 128-bit Service Class UUIDs
349+
if len(b) == 0 {
350+
b = buf.findField(0x06) // Incomplete List of 128-bit Service Class UUIDs
351+
}
352+
for i := 0; i < len(b)/16; i++ {
353+
uuids = append(uuids, NewUUID([16]byte(b[i*16:i*16+16])))
354+
}
355+
return uuids
356+
}
357+
325358
// ManufacturerData returns the manufacturer data in the advertisement payload.
326359
func (buf *rawAdvertisementPayload) ManufacturerData() []ManufacturerDataElement {
327360
var manufacturerData []ManufacturerDataElement

0 commit comments

Comments
 (0)