Add watch function for battery level

This commit is contained in:
Elara 2021-08-24 21:55:03 -07:00
parent 8648afeebf
commit 0430ddcd30
1 changed files with 28 additions and 0 deletions

View File

@ -351,6 +351,34 @@ func (i *Device) WatchHeartRate() (<-chan uint8, error) {
return out, nil
}
func (i *Device) WatchBatteryLevel() (<-chan uint8, error) {
if !i.device.Properties.Connected {
return make(<-chan uint8), nil
}
// Start notifications on heart rate characteristic
err := i.battLevelChar.StartNotify()
if err != nil {
return nil, err
}
// Watch characteristics of heart rate characteristic
ch, err := i.battLevelChar.WatchProperties()
if err != nil {
return nil, err
}
out := make(chan uint8, 2)
go func() {
// For every event
for event := range ch {
// If value changed
if event.Name == "Value" {
// Send heart rate to channel
out <- uint8(event.Value.([]byte)[0])
}
}
}()
return out, nil
}
// SetTime sets the watch's time using the Current Time Service
func (i *Device) SetTime(t time.Time) error {
if !i.device.Properties.Connected {