Skip to content

Commit

Permalink
gap: stop advertising
Browse files Browse the repository at this point in the history
  • Loading branch information
ysoldak authored and deadprogram committed May 22, 2022
1 parent 8dc1e15 commit 20f0ce6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
53 changes: 53 additions & 0 deletions examples/stop-advertisement/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// This example advertises for 5 minutes after
// - boot
// - disconnect
// and then stops advertising.
package main

import (
"time"

"tinygo.org/x/bluetooth"
)

var adapter = bluetooth.DefaultAdapter

var advUntil = time.Now().Add(5 * time.Minute)
var advState = true

func main() {
must("enable BLE stack", adapter.Enable())
adv := adapter.DefaultAdvertisement()
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "Go Bluetooth",
}))
adapter.SetConnectHandler(func(device bluetooth.Addresser, connected bool) {
if connected {
println("connected, not advertising...")
advState = false
} else {
println("disconnected, advertising...")
advState = true
advUntil = time.Now().Add(5 * time.Minute)
}
})
must("start adv", adv.Start())

println("advertising...")
address, _ := adapter.Address()
for {
if advState && time.Now().After(advUntil) {
println("timeout, not advertising...")
advState = false
must("stop adv", adv.Stop())
}
println("Go Bluetooth /", address.MAC.String())
time.Sleep(time.Second)
}
}

func must(action string, err error) {
if err != nil {
panic("failed to " + action + ": " + err.Error())
}
}
16 changes: 15 additions & 1 deletion gap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package bluetooth

import (
"errors"
"strings"

"github.com/godbus/dbus/v5"
Expand All @@ -12,6 +13,8 @@ import (
"github.com/muka/go-bluetooth/bluez/profile/device"
)

var errAdvertisementNotStarted = errors.New("bluetooth: stop advertisement that was not started")

// Address contains a Bluetooth MAC address.
type Address struct {
MACAddress
Expand All @@ -22,6 +25,7 @@ type Advertisement struct {
adapter *Adapter
advertisement *api.Advertisement
properties *advertising.LEAdvertisement1Properties
cancel func()
}

// DefaultAdvertisement returns the default advertisement instance but does not
Expand Down Expand Up @@ -60,10 +64,20 @@ func (a *Advertisement) Start() error {
if a.advertisement != nil {
panic("todo: start advertisement a second time")
}
_, err := api.ExposeAdvertisement(a.adapter.id, a.properties, uint32(a.properties.Timeout))
cancel, err := api.ExposeAdvertisement(a.adapter.id, a.properties, uint32(a.properties.Timeout))
if err != nil {
return err
}
a.cancel = cancel
return nil
}

// Stop advertisement. May only be called after it has been started.
func (a *Advertisement) Stop() error {
if a.cancel == nil {
return errAdvertisementNotStarted
}
a.cancel()
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions gap_nrf51.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func (a *Advertisement) Start() error {
return makeError(errCode)
}

// Stop advertisement.
func (a *Advertisement) Stop() error {
a.isAdvertising.Set(0)
errCode := C.sd_ble_gap_adv_stop()
return makeError(errCode)
}

// Low-level version of Start. Used to restart advertisement when a connection
// is lost.
func (a *Advertisement) start() uint32 {
Expand Down
7 changes: 7 additions & 0 deletions gap_nrf528xx-advertisement.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ func (a *Advertisement) Start() error {
errCode := C.sd_ble_gap_adv_start(a.handle, C.BLE_CONN_CFG_TAG_DEFAULT)
return makeError(errCode)
}

// Stop advertisement.
func (a *Advertisement) Stop() error {
a.isAdvertising.Set(0)
errCode := C.sd_ble_gap_adv_stop(a.handle)
return makeError(errCode)
}

0 comments on commit 20f0ce6

Please sign in to comment.