@@ -269,49 +269,69 @@ var defaultAdvertisement Advertisement
269269// Advertisement encapsulates a single advertisement instance.
270270type Advertisement struct {
271271 adapter * Adapter
272+
273+ localName []byte
274+ serviceUUIDs []UUID
275+ interval uint16
272276}
273277
274278// DefaultAdvertisement returns the default advertisement instance but does not
275279// configure it.
276280func (a * Adapter ) DefaultAdvertisement () * Advertisement {
277281 if defaultAdvertisement .adapter == nil {
278282 defaultAdvertisement .adapter = a
279-
280- a .AddService (
281- & Service {
282- UUID : ServiceUUIDGenericAccess ,
283- Characteristics : []CharacteristicConfig {
284- {
285- UUID : CharacteristicUUIDDeviceName ,
286- Flags : CharacteristicReadPermission ,
287- },
288- {
289- UUID : CharacteristicUUIDAppearance ,
290- Flags : CharacteristicReadPermission ,
291- },
292- },
293- })
294- a .AddService (
295- & Service {
296- UUID : ServiceUUIDGenericAttribute ,
297- Characteristics : []CharacteristicConfig {
298- {
299- UUID : CharacteristicUUIDServiceChanged ,
300- Flags : CharacteristicIndicatePermission ,
301- },
302- },
303- })
304283 }
305284
306285 return & defaultAdvertisement
307286}
308287
309288// Configure this advertisement.
310289func (a * Advertisement ) Configure (options AdvertisementOptions ) error {
311- // uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
290+ switch {
291+ case options .LocalName != "" :
292+ a .localName = []byte (options .LocalName )
293+ default :
294+ a .localName = []byte ("TinyGo" )
295+ }
296+
297+ a .serviceUUIDs = append ([]UUID {}, options .ServiceUUIDs ... )
298+ a .interval = uint16 (options .Interval )
299+
300+ a .adapter .AddService (
301+ & Service {
302+ UUID : ServiceUUIDGenericAccess ,
303+ Characteristics : []CharacteristicConfig {
304+ {
305+ UUID : CharacteristicUUIDDeviceName ,
306+ Flags : CharacteristicReadPermission ,
307+ Value : a .localName ,
308+ },
309+ {
310+ UUID : CharacteristicUUIDAppearance ,
311+ Flags : CharacteristicReadPermission ,
312+ },
313+ },
314+ })
315+ a .adapter .AddService (
316+ & Service {
317+ UUID : ServiceUUIDGenericAttribute ,
318+ Characteristics : []CharacteristicConfig {
319+ {
320+ UUID : CharacteristicUUIDServiceChanged ,
321+ Flags : CharacteristicIndicatePermission ,
322+ },
323+ },
324+ })
325+
326+ return nil
327+ }
328+
329+ // Start advertisement. May only be called after it has been configured.
330+ func (a * Advertisement ) Start () error {
331+ // uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
312332 typ := uint8 (0x00 )
313333
314- if err := a .adapter .hci .leSetAdvertisingParameters (uint16 ( options . Interval ), uint16 ( options . Interval ) ,
334+ if err := a .adapter .hci .leSetAdvertisingParameters (a . interval , a . interval ,
315335 typ , 0x00 , 0x00 , [6 ]byte {0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 }, 0x07 , 0 ); err != nil {
316336 return err
317337 }
@@ -325,8 +345,8 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
325345 advertisingDataLen += 3
326346
327347 // TODO: handle multiple service UUIDs
328- if len (options . ServiceUUIDs ) > 0 {
329- uuid := options . ServiceUUIDs [0 ]
348+ if len (a . serviceUUIDs ) > 0 {
349+ uuid := a . serviceUUIDs [0 ]
330350 var sz uint8
331351
332352 switch {
@@ -355,23 +375,22 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
355375 scanResponseDataLen := uint8 (0 )
356376
357377 switch {
358- case len (options . LocalName ) > 29 :
378+ case len (a . localName ) > 29 :
359379 scanResponseData [1 ] = 0x08
360380 scanResponseData [0 ] = 1 + 29
361- copy (scanResponseData [2 :], options . LocalName [:29 ])
381+ copy (scanResponseData [2 :], a . localName [:29 ])
362382 scanResponseDataLen = 31
363- case len (options . LocalName ) > 0 :
383+ case len (a . localName ) > 0 :
364384 scanResponseData [1 ] = 0x09
365- scanResponseData [0 ] = uint8 (1 + len (options . LocalName ))
366- copy (scanResponseData [2 :], options . LocalName )
367- scanResponseDataLen = uint8 (2 + len (options . LocalName ))
385+ scanResponseData [0 ] = uint8 (1 + len (a . localName ))
386+ copy (scanResponseData [2 :], a . localName )
387+ scanResponseDataLen = uint8 (2 + len (a . localName ))
368388 }
369389
370- return a .adapter .hci .leSetScanResponseData (scanResponseData [:scanResponseDataLen ])
371- }
390+ if err := a .adapter .hci .leSetScanResponseData (scanResponseData [:scanResponseDataLen ]); err != nil {
391+ return err
392+ }
372393
373- // Start advertisement. May only be called after it has been configured.
374- func (a * Advertisement ) Start () error {
375394 if err := a .adapter .hci .leSetAdvertiseEnable (true ); err != nil {
376395 return err
377396 }
0 commit comments