Fix up heart rate example

Fix up the heart rate example so that it conforms to the Heart Rate Service specification: https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=239866

This will let this example work with external clients like exercise equipment and fitness apps.

Changes:
- Advertise the HR service UUID instead of the HRM characteristic UUID.
- Change HRM characteristic to notify only.
- HRM payload needs to be two bytes:
  - 1st byte is flags specifying data type and sensor capabilities: this can be set to zero.
  - 2nd byte is HR measurement in bpm.

Tested on Raspberry Pi with nRF Connect app and exercise bike. Peripheral shows up as HR monitor and data is interpreted correctly.
This commit is contained in:
Michael Mogenson 2020-12-10 10:50:00 -05:00 committed by GitHub
parent 41f7317638
commit 9dde7219a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,7 @@ func main() {
adv := adapter.DefaultAdvertisement()
must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
LocalName: "Go HRS",
ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(0x2A37)},
ServiceUUIDs: []bluetooth.UUID{bluetooth.New16BitUUID(0x180D)},
}))
must("start adv", adv.Start())
@ -30,17 +30,7 @@ func main() {
Handle: &heartRateMeasurement,
UUID: bluetooth.New16BitUUID(0x2A37), // Heart Rate Measurement
Value: []byte{0, heartRate},
Flags: bluetooth.CharacteristicReadPermission | bluetooth.CharacteristicWritePermission |
bluetooth.CharacteristicNotifyPermission,
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)
}
},
Flags: bluetooth.CharacteristicNotifyPermission,
},
},
}))
@ -55,7 +45,7 @@ func main() {
heartRate = randomInt(65, 85)
// and push the next notification
heartRateMeasurement.Write([]byte{byte(heartRate)})
heartRateMeasurement.Write([]byte{0, heartRate})
}
}