From 43cf503d7770d6909cc8ffb76c8797100b40761a Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 24 Oct 2019 21:29:58 -0400 Subject: [PATCH] Update to use DiscoverServiceEvent, DiscoverCharacteristicEvent, and UpdateValue event. --- main.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index a104799..5ea16d4 100644 --- a/main.go +++ b/main.go @@ -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()