2021-08-20 00:41:09 +00:00
|
|
|
package infinitime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2021-10-27 04:42:41 +00:00
|
|
|
"strings"
|
2021-08-20 00:41:09 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
bt "github.com/muka/go-bluetooth/api"
|
|
|
|
"github.com/muka/go-bluetooth/bluez/profile/adapter"
|
|
|
|
"github.com/muka/go-bluetooth/bluez/profile/device"
|
|
|
|
"github.com/muka/go-bluetooth/bluez/profile/gatt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const BTName = "InfiniTime"
|
|
|
|
|
|
|
|
const (
|
|
|
|
NewAlertChar = "00002a46-0000-1000-8000-00805f9b34fb"
|
2021-10-15 07:23:54 +00:00
|
|
|
NotifEventChar = "00020001-78fc-48fe-8e23-433b3a1942d0"
|
2021-10-22 19:59:51 +00:00
|
|
|
StepCountChar = "00030001-78fc-48fe-8e23-433b3a1942d0"
|
|
|
|
MotionValChar = "00030002-78fc-48fe-8e23-433b3a1942d0"
|
2021-08-20 00:41:09 +00:00
|
|
|
FirmwareVerChar = "00002a26-0000-1000-8000-00805f9b34fb"
|
|
|
|
CurrentTimeChar = "00002a2b-0000-1000-8000-00805f9b34fb"
|
|
|
|
BatteryLvlChar = "00002a19-0000-1000-8000-00805f9b34fb"
|
|
|
|
HeartRateChar = "00002a37-0000-1000-8000-00805f9b34fb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Device struct {
|
|
|
|
opts *Options
|
|
|
|
device *device.Device1
|
|
|
|
newAlertChar *gatt.GattCharacteristic1
|
2021-10-15 07:23:54 +00:00
|
|
|
notifEventChar *gatt.GattCharacteristic1
|
2021-10-22 19:59:51 +00:00
|
|
|
stepCountChar *gatt.GattCharacteristic1
|
|
|
|
motionValChar *gatt.GattCharacteristic1
|
2021-08-20 00:41:09 +00:00
|
|
|
fwVersionChar *gatt.GattCharacteristic1
|
|
|
|
currentTimeChar *gatt.GattCharacteristic1
|
|
|
|
battLevelChar *gatt.GattCharacteristic1
|
|
|
|
heartRateChar *gatt.GattCharacteristic1
|
|
|
|
onReconnect func()
|
|
|
|
Music MusicCtrl
|
|
|
|
DFU DFU
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrNoDevices = errors.New("no InfiniTime devices found")
|
|
|
|
var ErrNotFound = errors.New("could not find any advertising InfiniTime devices")
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
AttemptReconnect bool
|
2021-10-15 07:23:54 +00:00
|
|
|
WhitelistEnabled bool
|
|
|
|
Whitelist []string
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultOptions = &Options{
|
|
|
|
AttemptReconnect: true,
|
2021-10-15 07:23:54 +00:00
|
|
|
WhitelistEnabled: false,
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect will attempt to connect to a
|
|
|
|
// paired InfiniTime device. If none are paired,
|
|
|
|
// it will attempt to discover and pair one.
|
|
|
|
//
|
|
|
|
// It will also attempt to reconnect to the device
|
|
|
|
// if it disconnects.
|
|
|
|
func Connect(opts *Options) (*Device, error) {
|
|
|
|
if opts == nil {
|
|
|
|
opts = DefaultOptions
|
|
|
|
}
|
|
|
|
// Attempt to connect to paired device by name
|
2021-10-15 07:23:54 +00:00
|
|
|
dev, err := connectByName(opts)
|
2021-08-20 00:41:09 +00:00
|
|
|
// If such device does not exist
|
|
|
|
if errors.Is(err, ErrNoDevices) {
|
|
|
|
// Attempt to pair device
|
2021-10-27 04:42:41 +00:00
|
|
|
dev, err = pair(opts)
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dev.opts = opts
|
|
|
|
dev.onReconnect = func() {}
|
|
|
|
// Watch device properties
|
|
|
|
devEvtCh, err := dev.device.WatchProperties()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If AttemptReconnect enabled
|
|
|
|
if dev.opts.AttemptReconnect {
|
|
|
|
go func() {
|
|
|
|
disconnEvtNum := 0
|
|
|
|
// For every event
|
|
|
|
for evt := range devEvtCh {
|
|
|
|
// If device disconnected
|
|
|
|
if evt.Name == "Connected" && evt.Value == false {
|
|
|
|
// Increment disconnect event number
|
|
|
|
disconnEvtNum++
|
|
|
|
// If more than one disconnect event
|
|
|
|
if disconnEvtNum > 1 {
|
|
|
|
// Decrement disconnect event number
|
|
|
|
disconnEvtNum--
|
|
|
|
// Skip loop
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Set connected to false
|
|
|
|
dev.device.Properties.Connected = false
|
|
|
|
// While not connected
|
|
|
|
for !dev.device.Properties.Connected {
|
|
|
|
// Attempt to connect via bluetooth address
|
|
|
|
reConnDev, err := ConnectByAddress(dev.device.Properties.Address)
|
|
|
|
if err != nil {
|
|
|
|
// Decrement disconnect event number
|
|
|
|
disconnEvtNum--
|
|
|
|
// Skip rest of loop
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Store onReconn callback
|
|
|
|
onReconn := dev.onReconnect
|
|
|
|
// Set device to new device
|
|
|
|
*dev = *reConnDev
|
|
|
|
// Run on reconnect callback
|
|
|
|
onReconn()
|
|
|
|
// Assign callback to new device
|
|
|
|
dev.onReconnect = onReconn
|
|
|
|
}
|
|
|
|
// Decrement disconnect event number
|
|
|
|
disconnEvtNum--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return dev, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnReconnect sets the callback that runs on reconnect
|
|
|
|
func (i *Device) OnReconnect(f func()) {
|
|
|
|
i.onReconnect = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect connects to a paired InfiniTime device
|
2021-10-15 07:23:54 +00:00
|
|
|
func connectByName(opts *Options) (*Device, error) {
|
2021-08-20 00:41:09 +00:00
|
|
|
// Create new device
|
|
|
|
out := &Device{}
|
|
|
|
// Get devices from default adapter
|
|
|
|
devs, err := defaultAdapter.GetDevices()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// For every device
|
|
|
|
for _, dev := range devs {
|
|
|
|
// If device name is InfiniTime
|
|
|
|
if dev.Properties.Name == BTName {
|
2021-10-15 07:23:54 +00:00
|
|
|
if opts.WhitelistEnabled && !contains(opts.Whitelist, dev.Properties.Address) {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-20 00:41:09 +00:00
|
|
|
// Set outout device to discovered device
|
|
|
|
out.device = dev
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if out.device == nil {
|
|
|
|
return nil, ErrNoDevices
|
|
|
|
}
|
|
|
|
// Connect to device
|
2021-08-22 03:25:09 +00:00
|
|
|
err = out.device.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-20 00:41:09 +00:00
|
|
|
// Resolve characteristics
|
|
|
|
err = out.resolveChars()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2021-10-15 07:23:54 +00:00
|
|
|
func contains(ss []string, s string) bool {
|
|
|
|
for _, str := range ss {
|
2021-10-27 04:42:41 +00:00
|
|
|
if strings.EqualFold(str, s) {
|
2021-10-15 07:23:54 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:41:09 +00:00
|
|
|
// Pair attempts to discover and pair an InfiniTime device
|
2021-10-27 04:42:41 +00:00
|
|
|
func pair(opts *Options) (*Device, error) {
|
2021-08-20 00:41:09 +00:00
|
|
|
// Create new device
|
|
|
|
out := &Device{}
|
|
|
|
// Start bluetooth discovery
|
2021-08-22 20:09:56 +00:00
|
|
|
// Ignore the cancel function as it blocks forever
|
|
|
|
discovery, _, err := bt.Discover(defaultAdapter, &adapter.DiscoveryFilter{Transport: "le"})
|
2021-08-20 00:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-22 20:12:16 +00:00
|
|
|
// For every discovery event
|
|
|
|
for event := range discovery {
|
|
|
|
// If device removed, skip event
|
|
|
|
if event.Type == adapter.DeviceRemoved {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Create new device with discovered path
|
|
|
|
dev, err := device.NewDevice1(event.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If device name is InfiniTime
|
|
|
|
if dev.Properties.Name == BTName {
|
2021-10-27 04:42:41 +00:00
|
|
|
if opts.WhitelistEnabled && !contains(opts.Whitelist, dev.Properties.Address) {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-22 20:12:16 +00:00
|
|
|
// Set output device
|
|
|
|
out.device = dev
|
|
|
|
break
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.device == nil {
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pair device
|
|
|
|
out.device.Pair()
|
|
|
|
|
|
|
|
// Set connected to true
|
|
|
|
out.device.Properties.Connected = true
|
|
|
|
|
|
|
|
// Resolve characteristics
|
|
|
|
err = out.resolveChars()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectByAddress tries to connect to an InifiniTime at
|
|
|
|
// the specified InfiniTime address
|
|
|
|
func ConnectByAddress(addr string) (*Device, error) {
|
|
|
|
var err error
|
|
|
|
// Create new device
|
|
|
|
out := &Device{}
|
|
|
|
// Get device from bluetooth address
|
|
|
|
out.device, err = defaultAdapter.GetDeviceByAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to device
|
|
|
|
err = out.device.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve characteristics
|
|
|
|
err = out.resolveChars()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveChars attempts to set all required
|
|
|
|
// characteristics in an InfiniTime struct
|
|
|
|
func (i *Device) resolveChars() error {
|
|
|
|
// Get device characteristics
|
|
|
|
chars, err := i.device.GetCharacteristics()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// While no characteristics found
|
|
|
|
for len(chars) == 0 {
|
|
|
|
// Sleep one second
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
// Attempt to retry getting characteristics
|
|
|
|
chars, err = i.device.GetCharacteristics()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// For every discovered characteristics
|
|
|
|
for _, char := range chars {
|
|
|
|
// Set correct characteristics
|
|
|
|
switch char.Properties.UUID {
|
|
|
|
case NewAlertChar:
|
|
|
|
i.newAlertChar = char
|
2021-10-15 07:23:54 +00:00
|
|
|
case NotifEventChar:
|
|
|
|
i.notifEventChar = char
|
2021-10-22 19:59:51 +00:00
|
|
|
case StepCountChar:
|
|
|
|
i.stepCountChar = char
|
|
|
|
case MotionValChar:
|
|
|
|
i.motionValChar = char
|
2021-08-20 00:41:09 +00:00
|
|
|
case FirmwareVerChar:
|
|
|
|
i.fwVersionChar = char
|
|
|
|
case CurrentTimeChar:
|
|
|
|
i.currentTimeChar = char
|
|
|
|
case BatteryLvlChar:
|
|
|
|
i.battLevelChar = char
|
|
|
|
case HeartRateChar:
|
|
|
|
i.heartRateChar = char
|
|
|
|
case MusicEventChar:
|
|
|
|
i.Music.eventChar = char
|
|
|
|
case MusicStatusChar:
|
|
|
|
i.Music.statusChar = char
|
|
|
|
case MusicArtistChar:
|
|
|
|
i.Music.artistChar = char
|
|
|
|
case MusicTrackChar:
|
|
|
|
i.Music.trackChar = char
|
|
|
|
case MusicAlbumChar:
|
|
|
|
i.Music.albumChar = char
|
|
|
|
case DFUCtrlPointChar:
|
|
|
|
i.DFU.ctrlPointChar = char
|
|
|
|
case DFUPacketChar:
|
|
|
|
i.DFU.packetChar = char
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns the InfiniTime's bluetooth address
|
|
|
|
func (i *Device) Address() string {
|
|
|
|
return i.device.Properties.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns InfiniTime's reported firmware version string
|
|
|
|
func (i *Device) Version() (string, error) {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
ver, err := i.fwVersionChar.ReadValue(nil)
|
|
|
|
return string(ver), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// BatteryLevel gets the watch's battery level via the Battery Service
|
|
|
|
func (i *Device) BatteryLevel() (uint8, error) {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
battLevel, err := i.battLevelChar.ReadValue(nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint8(battLevel[0]), nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 19:59:51 +00:00
|
|
|
func (i *Device) StepCount() (uint32, error) {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
stepCountData, err := i.stepCountChar.ReadValue(nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint32(stepCountData), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type MotionValues struct {
|
|
|
|
X int16
|
|
|
|
Y int16
|
|
|
|
Z int16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Device) Motion() (MotionValues, error) {
|
|
|
|
out := MotionValues{}
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
motionVals, err := i.motionValChar.ReadValue(nil)
|
|
|
|
if err != nil {
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
motionValReader := bytes.NewReader(motionVals)
|
|
|
|
err = binary.Read(motionValReader, binary.LittleEndian, &out)
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:41:09 +00:00
|
|
|
func (i *Device) HeartRate() (uint8, error) {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
heartRate, err := i.heartRateChar.ReadValue(nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint8(heartRate[1]), nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:26:33 +00:00
|
|
|
func (i *Device) WatchHeartRate() (<-chan uint8, func(), error) {
|
2021-08-20 00:41:09 +00:00
|
|
|
if !i.device.Properties.Connected {
|
2021-10-23 04:26:33 +00:00
|
|
|
return make(<-chan uint8), nil, nil
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
// Start notifications on heart rate characteristic
|
|
|
|
err := i.heartRateChar.StartNotify()
|
|
|
|
if err != nil {
|
2021-10-23 04:26:33 +00:00
|
|
|
return nil, nil, err
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
// Watch characteristics of heart rate characteristic
|
|
|
|
ch, err := i.heartRateChar.WatchProperties()
|
|
|
|
if err != nil {
|
2021-10-23 04:26:33 +00:00
|
|
|
return nil, nil, err
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
out := make(chan uint8, 2)
|
2021-08-25 05:17:34 +00:00
|
|
|
currentHeartRate, err := i.HeartRate()
|
|
|
|
if err != nil {
|
2021-10-23 04:26:33 +00:00
|
|
|
return nil, nil, err
|
2021-08-25 05:17:34 +00:00
|
|
|
}
|
|
|
|
out <- currentHeartRate
|
2021-10-23 04:26:33 +00:00
|
|
|
cancel, done := cancelFunc()
|
2021-08-20 00:41:09 +00:00
|
|
|
go func() {
|
|
|
|
// For every event
|
|
|
|
for event := range ch {
|
2021-10-23 04:26:33 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
close(out)
|
|
|
|
close(done)
|
|
|
|
i.heartRateChar.StopNotify()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// If value changed
|
|
|
|
if event.Name == "Value" {
|
|
|
|
// Send heart rate to channel
|
|
|
|
out <- uint8(event.Value.([]byte)[1])
|
|
|
|
}
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-10-23 04:26:33 +00:00
|
|
|
return out, cancel, nil
|
2021-08-20 00:41:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 04:26:33 +00:00
|
|
|
func (i *Device) WatchBatteryLevel() (<-chan uint8, func(), error) {
|
2021-08-25 04:55:03 +00:00
|
|
|
if !i.device.Properties.Connected {
|
2021-10-23 04:26:33 +00:00
|
|
|
return make(<-chan uint8), nil, nil
|
2021-08-25 04:55:03 +00:00
|
|
|
}
|
|
|
|
// Start notifications on heart rate characteristic
|
|
|
|
err := i.battLevelChar.StartNotify()
|
|
|
|
if err != nil {
|
2021-10-23 04:26:33 +00:00
|
|
|
return nil, nil, err
|
2021-08-25 04:55:03 +00:00
|
|
|
}
|
|
|
|
// Watch characteristics of heart rate characteristic
|
|
|
|
ch, err := i.battLevelChar.WatchProperties()
|
|
|
|
if err != nil {
|
2021-10-23 04:26:33 +00:00
|
|
|
return nil, nil, err
|
2021-08-25 04:55:03 +00:00
|
|
|
}
|
|
|
|
out := make(chan uint8, 2)
|
2021-08-25 05:17:34 +00:00
|
|
|
currentBattLevel, err := i.BatteryLevel()
|
|
|
|
if err != nil {
|
2021-10-23 04:26:33 +00:00
|
|
|
return nil, nil, err
|
2021-08-25 05:17:34 +00:00
|
|
|
}
|
|
|
|
out <- currentBattLevel
|
2021-10-23 04:26:33 +00:00
|
|
|
cancel, done := cancelFunc()
|
2021-08-25 04:55:03 +00:00
|
|
|
go func() {
|
|
|
|
// For every event
|
|
|
|
for event := range ch {
|
2021-10-23 04:26:33 +00:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
close(out)
|
|
|
|
close(done)
|
|
|
|
i.battLevelChar.StopNotify()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// If value changed
|
|
|
|
if event.Name == "Value" {
|
|
|
|
// Send heart rate to channel
|
|
|
|
out <- uint8(event.Value.([]byte)[0])
|
|
|
|
}
|
2021-08-25 04:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-10-23 04:26:33 +00:00
|
|
|
return out, cancel, nil
|
2021-08-25 04:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 19:59:51 +00:00
|
|
|
func (i *Device) WatchStepCount() (<-chan uint32, func(), error) {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return make(<-chan uint32), nil, nil
|
|
|
|
}
|
|
|
|
// Start notifications on step count characteristic
|
|
|
|
err := i.stepCountChar.StartNotify()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Watch properties of step count characteristic
|
|
|
|
ch, err := i.stepCountChar.WatchProperties()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
out := make(chan uint32, 2)
|
|
|
|
currentStepCount, err := i.StepCount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
out <- currentStepCount
|
|
|
|
cancel, done := cancelFunc()
|
|
|
|
go func() {
|
|
|
|
// For every event
|
|
|
|
for event := range ch {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
close(out)
|
|
|
|
close(done)
|
|
|
|
i.stepCountChar.StopNotify()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// If value changed
|
|
|
|
if event.Name == "Value" {
|
|
|
|
// Send step count to channel
|
|
|
|
out <- binary.LittleEndian.Uint32(event.Value.([]byte))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out, cancel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Device) WatchMotion() (<-chan MotionValues, func(), error) {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return make(<-chan MotionValues), nil, nil
|
|
|
|
}
|
|
|
|
// Start notifications on motion characteristic
|
|
|
|
err := i.motionValChar.StartNotify()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Watch properties of motion characteristic
|
|
|
|
ch, err := i.motionValChar.WatchProperties()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
out := make(chan MotionValues, 2)
|
|
|
|
motionVals, err := i.Motion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
out <- motionVals
|
|
|
|
cancel, done := cancelFunc()
|
|
|
|
go func() {
|
|
|
|
// For every event
|
|
|
|
for event := range ch {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
close(out)
|
|
|
|
close(done)
|
|
|
|
i.motionValChar.StopNotify()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// If value changed
|
|
|
|
if event.Name == "Value" {
|
|
|
|
// Read binary into MotionValues struct
|
|
|
|
binary.Read(bytes.NewReader(event.Value.([]byte)), binary.LittleEndian, &motionVals)
|
|
|
|
// Send step count to channel
|
|
|
|
out <- motionVals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out, cancel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cancelFunc() (func(), chan struct{}) {
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
return func() {
|
|
|
|
done <- struct{}{}
|
|
|
|
}, done
|
|
|
|
}
|
|
|
|
|
2021-08-20 00:41:09 +00:00
|
|
|
// SetTime sets the watch's time using the Current Time Service
|
|
|
|
func (i *Device) SetTime(t time.Time) error {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint16(t.Year()))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(t.Month()))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(t.Day()))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(t.Hour()))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(t.Minute()))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(t.Second()))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(t.Weekday()))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8((t.Nanosecond()/1000)/1e6*256))
|
|
|
|
binary.Write(buf, binary.LittleEndian, uint8(0b0001))
|
|
|
|
return i.currentTimeChar.WriteValue(buf.Bytes(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify sends a notification to InfiniTime via
|
|
|
|
// the Alert Notification Service (ANS)
|
|
|
|
func (i *Device) Notify(title, body string) error {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return i.newAlertChar.WriteValue(
|
2021-10-15 07:23:54 +00:00
|
|
|
append([]byte{0x00, 0x01, 0x00}, []byte(title+"\x00"+body)...),
|
2021-08-20 00:41:09 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}
|
2021-10-15 07:23:54 +00:00
|
|
|
|
|
|
|
// These constants represent the possible call statuses selected by the user
|
|
|
|
const (
|
|
|
|
CallStatusDeclined uint8 = iota
|
|
|
|
CallStatusAccepted
|
|
|
|
CallStatusMuted
|
|
|
|
)
|
|
|
|
|
|
|
|
// NotifyCall sends a call notification to the PineTime and returns a channel.
|
|
|
|
// This channel will contain the user's response to the call notification. It
|
|
|
|
// can only contain one value.
|
|
|
|
func (i *Device) NotifyCall(from string) (<-chan uint8, error) {
|
|
|
|
if !i.device.Properties.Connected {
|
|
|
|
return make(<-chan uint8), nil
|
|
|
|
}
|
|
|
|
// Write call notification to new alert characteristic
|
|
|
|
err := i.newAlertChar.WriteValue(
|
|
|
|
append([]byte{0x03, 0x01, 0x00}, []byte(from)...),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Start notifications on notification event characteristic
|
|
|
|
err = i.notifEventChar.StartNotify()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Watch properties of notification event characteristic
|
|
|
|
ch, err := i.notifEventChar.WatchProperties()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Create new output channel for status
|
|
|
|
out := make(chan uint8, 1)
|
|
|
|
go func() {
|
|
|
|
// For every event
|
|
|
|
for event := range ch {
|
|
|
|
// If value changed
|
|
|
|
if event.Name == "Value" {
|
|
|
|
// Send status to channel
|
|
|
|
out <- uint8(event.Value.([]byte)[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out, nil
|
|
|
|
}
|