-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
60 lines (52 loc) · 1.24 KB
/
main.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
package main
import (
"fmt"
"log"
"sync"
"time"
"github.com/paypal/gatt"
"github.com/paypal/gatt/examples/option"
)
var isPoweredOn = false
var scanMutex = sync.Mutex{}
func beginScan(d gatt.Device) {
scanMutex.Lock()
for isPoweredOn {
d.Scan(nil, true) //Scan for five seconds and then restart
time.Sleep(5 * time.Second)
d.StopScanning()
}
scanMutex.Unlock()
}
func onStateChanged(d gatt.Device, s gatt.State) {
fmt.Println("State:", s)
switch s {
case gatt.StatePoweredOn:
fmt.Println("scanning...")
isPoweredOn = true
go beginScan(d)
return
case gatt.StatePoweredOff:
log.Println("REINIT ON POWER OFF")
isPoweredOn = false
d.Init(onStateChanged)
default:
log.Println("WARN: unhandled state: ", string(s))
}
}
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
fmt.Println(" TX Power Level =", a.TxPowerLevel)
ParseRuuviData(a.ManufacturerData, p.ID())
}
func main() {
d, err := gatt.NewDevice(option.DefaultClientOptions...)
if err != nil {
log.Fatalf("Failed to open device, err: %s\n", err)
return
}
// Register handlers.
d.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered))
d.Init(onStateChanged)
select {}
}