Add watch function for battery level

This commit is contained in:
Elara 2021-08-24 21:55:03 -07:00
parent 8648afeebf
commit 0430ddcd30

View File

@ -351,6 +351,34 @@ func (i *Device) WatchHeartRate() (<-chan uint8, error) {
return out, nil 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 // SetTime sets the watch's time using the Current Time Service
func (i *Device) SetTime(t time.Time) error { func (i *Device) SetTime(t time.Time) error {
if !i.device.Properties.Connected { if !i.device.Properties.Connected {