086c797e0f
This changes the previous raw advertisement packets to structured advertisement configuration. That means you can set the local name not with a raw byte array but with a normal string. While this departs a bit from the original low-level interface as is often used on microcontroller BLE stacks, it is certainly easier to use and better matches higher level APIs that are commonly provided by general-purpose operating systems. If there is a need for raw BLE packets (for baremetal systems only), this can easily be added in the future.
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/tinygo-org/bluetooth"
|
|
)
|
|
|
|
var adapter = bluetooth.DefaultAdapter
|
|
|
|
// TODO: use atomics to access this value.
|
|
var heartRate uint8 = 75 // 75bpm
|
|
|
|
func main() {
|
|
println("starting")
|
|
adapter.SetEventHandler(handleBluetoothEvents)
|
|
must("enable BLE stack", adapter.Enable())
|
|
adv := adapter.NewAdvertisement()
|
|
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
|
|
LocalName: "Go HRS",
|
|
Interval: bluetooth.NewAdvertisementInterval(100),
|
|
}))
|
|
must("start adv", adv.Start())
|
|
|
|
var heartRateMeasurement bluetooth.Characteristic
|
|
must("add service", adapter.AddService(&bluetooth.Service{
|
|
UUID: bluetooth.New16BitUUID(0x180D), // Heart Rate
|
|
Characteristics: []bluetooth.CharacteristicConfig{
|
|
{
|
|
Handle: &heartRateMeasurement,
|
|
UUID: bluetooth.New16BitUUID(0x2A37), // Heart Rate Measurement
|
|
Value: []byte{0, heartRate},
|
|
Flags: bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicWritePermission,
|
|
WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
|
|
if offset != 0 || len(value) < 2 {
|
|
return
|
|
}
|
|
if value[1] != 0 { // avoid divide by zero
|
|
heartRate = value[1]
|
|
println("heart rate is now:", heartRate)
|
|
}
|
|
},
|
|
},
|
|
},
|
|
}))
|
|
|
|
nextBeat := time.Now()
|
|
for {
|
|
nextBeat = nextBeat.Add(time.Minute / time.Duration(heartRate))
|
|
println("tick", time.Now().Format("04:05.000"))
|
|
time.Sleep(nextBeat.Sub(time.Now()))
|
|
}
|
|
}
|
|
|
|
func must(action string, err error) {
|
|
if err != nil {
|
|
panic("failed to " + action + ": " + err.Error())
|
|
}
|
|
}
|
|
|
|
// handleBluetoothEvents prints BLE events as they happen.
|
|
func handleBluetoothEvents(evt bluetooth.Event) {
|
|
switch evt := evt.(type) {
|
|
case *bluetooth.ConnectEvent:
|
|
println("evt: connected", evt.Connection)
|
|
case *bluetooth.DisconnectEvent:
|
|
println("evt: disconnected", evt.Connection)
|
|
default:
|
|
println("evt: unknown")
|
|
}
|
|
}
|