@@ -55,29 +55,19 @@ type AdvertisementOptions struct {
55
55
Interval Duration
56
56
57
57
// ManufacturerData stores Advertising Data.
58
- ManufacturerData []ManufacturerDataElement
58
+ ManufacturerData []AdvertismentDataElement
59
59
60
60
// ServiceData stores Advertising Data.
61
61
ServiceData []AdvertismentDataElement
62
62
}
63
63
64
- // Manufacturer data that's part of an advertisement packet.
65
- type ManufacturerDataElement struct {
64
+ // AdvertismentDataElement strores a uuid/byte-array pair used as ServiceData or ManufacturerData advertisment elements
65
+ type AdvertismentDataElement struct {
66
+ // service uuid or company uuid
66
67
// The company ID, which must be one of the assigned company IDs.
67
68
// The full list is in here:
68
69
// https://www.bluetooth.com/specifications/assigned-numbers/
69
- // The list can also be viewed here:
70
- // https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/company_identifiers/company_identifiers.yaml
71
70
// The value 0xffff can also be used for testing.
72
- CompanyID uint16
73
-
74
- // The value, which can be any value but can't be very large.
75
- Data []byte
76
- }
77
-
78
- // AdvertismentDataElement strores a uuid/byte-array pair used as ServiceData or ManufacturerData advertisment elements
79
- type AdvertismentDataElement struct {
80
- // service uuid or company uuid
81
71
// The list can also be viewed here:
82
72
// https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/company_identifiers/company_identifiers.yaml
83
73
// https://bitbucket.org/bluetooth-SIG/public/src/main/assigned_numbers/uuids/service_uuids.yaml
@@ -139,7 +129,7 @@ type AdvertisementPayload interface {
139
129
140
130
// ManufacturerData returns a slice with all the manufacturer data present in the
141
131
// advertising. It may be empty.
142
- ManufacturerData () []ManufacturerDataElement
132
+ ManufacturerData () []AdvertismentDataElement
143
133
144
134
// ServiceData returns a slice with all the service data present in the
145
135
// advertising. It may be empty.
@@ -158,7 +148,7 @@ type AdvertisementFields struct {
158
148
ServiceUUIDs []UUID
159
149
160
150
// ManufacturerData is the manufacturer data of the advertisement.
161
- ManufacturerData []ManufacturerDataElement
151
+ ManufacturerData []AdvertismentDataElement
162
152
163
153
// ServiceData is the service data of the advertisement.
164
154
ServiceData []AdvertismentDataElement
@@ -195,7 +185,7 @@ func (p *advertisementFields) Bytes() []byte {
195
185
}
196
186
197
187
// ManufacturerData returns the underlying ManufacturerData field.
198
- func (p * advertisementFields ) ManufacturerData () []ManufacturerDataElement {
188
+ func (p * advertisementFields ) ManufacturerData () []AdvertismentDataElement {
199
189
return p .AdvertisementFields .ManufacturerData
200
190
}
201
191
@@ -315,21 +305,12 @@ func (buf *rawAdvertisementPayload) HasServiceUUID(uuid UUID) bool {
315
305
}
316
306
317
307
// ManufacturerData returns the manufacturer data in the advertisement payload.
318
- func (buf * rawAdvertisementPayload ) ManufacturerData () []ManufacturerDataElement {
319
- var manufacturerData []ManufacturerDataElement
320
- for index := 0 ; index < int (buf .len )+ 4 ; index += int (buf .data [index ]) + 1 {
321
- fieldLength := int (buf .data [index + 0 ])
322
- if fieldLength < 3 {
323
- continue
324
- }
325
- fieldType := buf .data [index + 1 ]
326
- if fieldType != 0xff {
327
- continue
328
- }
329
- key := uint16 (buf .data [index + 2 ]) | uint16 (buf .data [index + 3 ])<< 8
330
- manufacturerData = append (manufacturerData , ManufacturerDataElement {
331
- CompanyID : key ,
332
- Data : buf .data [index + 4 : index + fieldLength + 1 ],
308
+ func (buf * rawAdvertisementPayload ) ManufacturerData () []AdvertismentDataElement {
309
+ var manufacturerData []AdvertismentDataElement
310
+ for _ , data := range buf .findAllFields (0xFF ) {
311
+ manufacturerData = append (manufacturerData , AdvertismentDataElement {
312
+ UUID : New16BitUUID (uint16 (data [0 ])+ (uint16 (data [1 ])<< 8 )),
313
+ Data : data [2 :],
333
314
})
334
315
}
335
316
return manufacturerData
@@ -390,7 +371,7 @@ func (buf *rawAdvertisementPayload) addFromOptions(options AdvertisementOptions)
390
371
}
391
372
392
373
for _ , element := range options .ManufacturerData {
393
- if err := buf .addManufacturerData (element .CompanyID , element .Data ); err != nil {
374
+ if err := buf .addManufacturerData (element .UUID , element .Data ); err != nil {
394
375
return err
395
376
}
396
377
}
@@ -405,19 +386,19 @@ func (buf *rawAdvertisementPayload) addFromOptions(options AdvertisementOptions)
405
386
}
406
387
407
388
// addManufacturerData adds manufacturer data ([]byte) entries to the advertisement payload.
408
- func (buf * rawAdvertisementPayload ) addManufacturerData (key uint16 , value []byte ) (err error ) {
389
+ func (buf * rawAdvertisementPayload ) addManufacturerData (uuid UUID , data []byte ) (err error ) {
409
390
// Check whether the field can fit this manufacturer data.
410
- fieldLength := len (value ) + 4
391
+ fieldLength := 1 + 1 + 2 + len (data ) // 1 byte length, 1 byte ad type, 2 bytes uuid, actual manufacturer data
411
392
if int (buf .len )+ fieldLength > len (buf .data ) {
412
393
return errAdvertisementPacketTooBig
413
394
}
414
395
415
396
// Add the data.
416
- buf .data [buf .len + 0 ] = uint8 (fieldLength - 1 )
417
- buf .data [buf .len + 1 ] = 0xff
418
- buf .data [buf .len + 2 ] = uint8 ( key )
419
- buf .data [buf .len + 3 ] = uint8 ( key >> 8 )
420
- copy (buf .data [buf .len + 4 :], value )
397
+ buf .data [buf .len + 0 ] = byte (fieldLength - 1 )
398
+ buf .data [buf .len + 1 ] = 0xFF
399
+ buf .data [buf .len + 2 ] = byte ( uuid . Get16Bit () )
400
+ buf .data [buf .len + 3 ] = byte ( uuid . Get16Bit () >> 8 )
401
+ copy (buf .data [buf .len + 4 :], data )
421
402
buf .len += uint8 (fieldLength )
422
403
423
404
return nil
0 commit comments