c034fbca54
Instead of attempting to allocate multiple advertisement instances, only use one by default. If needed, a NewAdvertisement method could be added in the future for devices that actually do support multiple advertisements at a time. The motivation for this change is fix an inconsistency with the nrf51 (which already had the behavior of DefaultAdvertisement) and the discovery that nrf52 devices also don't seem to support more than one advertisement instance, even though their API does allow for multiple instances. But the primary motivation is that for consistency with hosted systems, it would be best if the nrf port would automatically re-enable advertisement when a connection is lost (or made). While BlueZ does support more than one instance, it is implemented by simply iterating through the active advertisement instances so could also be implemented by doing that manually. I haven't checked the behavior of Windows and MacOS - but as always, the API is not yet stable and can be changed if needed.
32 lines
590 B
Go
32 lines
590 B
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/tinygo-org/bluetooth"
|
|
)
|
|
|
|
var adapter = bluetooth.DefaultAdapter
|
|
|
|
func main() {
|
|
must("enable BLE stack", adapter.Enable())
|
|
adv := adapter.DefaultAdvertisement()
|
|
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
|
|
LocalName: "Go Bluetooth",
|
|
Interval: bluetooth.NewAdvertisementInterval(100),
|
|
}))
|
|
must("start adv", adv.Start())
|
|
|
|
println("advertising...")
|
|
for {
|
|
// Sleep forever.
|
|
time.Sleep(time.Hour)
|
|
}
|
|
}
|
|
|
|
func must(action string, err error) {
|
|
if err != nil {
|
|
panic("failed to " + action + ": " + err.Error())
|
|
}
|
|
}
|