Update to use DiscoverServiceEvent, DiscoverCharacteristicEvent, and

UpdateValue event.
This commit is contained in:
Greg 2019-10-24 21:29:58 -04:00
parent 11dd9c3eb4
commit 43cf503d77
1 changed files with 28 additions and 3 deletions

31
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"encoding/binary"
"fmt"
"io/ioutil"
"log"
@ -49,6 +50,15 @@ func main() {
app.Main()
}
func hrDecode(x []byte) int {
flags := x[0]
if flags&0x80 != 0 { // uint16 format
return int(binary.BigEndian.Uint16(x[1:2]))
} else {
return int(x[1])
}
}
func eventloop() {
w := app.NewWindow(
app.Size(unit.Dp(400), unit.Dp(400)),
@ -69,6 +79,7 @@ func eventloop() {
b := ble.NewBLE()
state := "starting"
var hr int
var periph ble.Peripheral
periphs := make([]ble.Peripheral, 0)
btns := make([]*widget.Button, 0)
@ -131,7 +142,7 @@ func eventloop() {
th.Body1(periph.Name).Layout(gtx)
})
c3 := f.Rigid(gtx, func() {
l := th.H1(fmt.Sprintf("%d", b.HR()))
l := th.H1(fmt.Sprintf("%d", hr))
l.Alignment = text.Middle
l.Layout(gtx)
})
@ -162,7 +173,7 @@ func eventloop() {
}
}
w.Invalidate()
case ble.DiscoverEvent:
case ble.DiscoverPeripheralEvent:
fmt.Printf("found %s\n", e.Peripheral.Identifier)
periphs = append(periphs, e.Peripheral)
btns = append(btns, &widget.Button{})
@ -173,7 +184,7 @@ func eventloop() {
fmt.Printf("Connect event\n")
state = "connected"
periph = e.Peripheral
ble.DiscoverServices(e.Peripheral)
e.Peripheral.DiscoverServices()
page = hrpage
w.Invalidate()
case ble.ConnectTimeoutEvent:
@ -183,6 +194,20 @@ func eventloop() {
page = scanpage
b.Scan()
w.Invalidate()
case ble.DiscoverServiceEvent:
fmt.Printf("DiscoverService %s\n", e.Gatt.UUID)
if e.Gatt.UUID == "180D" {
fmt.Printf("Found HRM Service\n")
e.Peripheral.DiscoverCharacteristics(e.Service)
}
case ble.DiscoverCharacteristicEvent:
fmt.Printf("DiscoverCharacteristic %s\n", e.Gatt.UUID)
if e.Gatt.UUID == "2A37" {
fmt.Printf("Found heart rate value\n")
e.Peripheral.SetNotifyValue(e.Characteristic)
}
case ble.UpdateValueEvent:
hr = hrDecode(e.Data)
}
case <-tick.C:
w.Invalidate()