/
devicewatcher.go
65 lines (57 loc) · 1.34 KB
/
devicewatcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package tapgosdk
import (
"time"
"github.com/GPeye/tapgosdk/events"
"tinygo.org/x/bluetooth"
)
type DeviceWatcherStatus int
const (
Created DeviceWatcherStatus = 0
Started DeviceWatcherStatus = 1
EnumerationCompleted DeviceWatcherStatus = 2
Stopped DeviceWatcherStatus = 4
)
type DeviceWatcher struct {
adapter *bluetooth.Adapter
timeout time.Duration
timer *time.Ticker
status DeviceWatcherStatus
desiredDevices int
}
func NewDeviceWatcher() *DeviceWatcher {
d := new(DeviceWatcher)
d.timeout = 10 * time.Second
d.adapter = bluetooth.DefaultAdapter
d.status = Created
d.desiredDevices = 1
// Enable BLE interface.
must("enable BLE stack", d.adapter.Enable())
return d
}
func (d *DeviceWatcher) Start() {
d.timer = time.NewTicker(d.timeout)
go func() {
for {
select {
case <-d.timer.C:
d.adapter.StopScan()
d.timer.Stop()
d.status = Stopped
events.DeviceScanStopped.Trigger()
println("Unable to find tap device, stopping search")
}
}
}()
err := d.adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
events.DeviceFound.Trigger(device)
})
if err != nil {
panic(err.Error())
}
}
func (d *DeviceWatcher) Stop() {
d.adapter.StopScan()
d.timer.Stop()
events.DeviceScanStopped.Trigger()
d.status = Stopped
}