-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
106 lines (94 loc) · 2.73 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"log"
"math"
"tinygo.org/x/bluetooth"
)
func main() {
adapter := bluetooth.DefaultAdapter
err := adapter.Enable()
if err != nil {
panic("failed to enable BLE adapter")
}
err = adapter.Scan(onScan)
if err != nil {
panic("failed to register scan callback")
}
}
const BleServiceTemperature = 0xFFE0
const BleCharacteristicTemperature = 0xFFE1
func onScan(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
//log.Println("found device:", device.Address.String(), device.RSSI, device.LocalName(), device.AdvertisementPayload)
if device.HasServiceUUID(bluetooth.New16BitUUID(BleServiceTemperature)) {
log.Println("found device with temperature service:", device.Address.String(), device.RSSI, device.LocalName())
go func() {
res, err := adapter.Connect(device.Address, bluetooth.ConnectionParams{})
if err != nil {
log.Println("error connecting:", err.Error())
return
}
onConnect(device, res)
}()
}
}
func onConnect(scanResult bluetooth.ScanResult, device *bluetooth.Device) {
log.Println("connected:", scanResult.Address.String(), scanResult.LocalName())
// Get a list of services
services, err := device.DiscoverServices([]bluetooth.UUID{
bluetooth.New16BitUUID(BleServiceTemperature),
})
// If error, bail out
if err != nil {
log.Println("error getting services:", err.Error())
return
}
// Iterate services
for _, service := range services {
if service.UUID() != bluetooth.New16BitUUID(BleServiceTemperature) {
// Wrong service
continue
}
// Found the correct service
// Get a list of characteristics below the service
characteristics, err := service.DiscoverCharacteristics([]bluetooth.UUID{
bluetooth.New16BitUUID(BleCharacteristicTemperature),
})
// If error, bail out
if err != nil {
log.Println("error getting characteristics:", err.Error())
return
}
// Iterate characteristics
for _, characteristic := range characteristics {
err := characteristic.EnableNotifications(characteristicReceiverTemperature)
// If error, bail out
if err != nil {
log.Println("error enabling notifications:", err.Error())
return
}
}
}
}
func characteristicReceiverTemperature(buf []byte) {
log.Printf("received: %x", buf)
if !isTemperatureSuccess(buf) {
log.Printf("wrong measurement")
return
}
temp := (float64(buf[4]) * 256) + float64(buf[5])
temp = math.Round(temp/10) / 10
log.Printf("temp: %v", temp)
}
var TemperatureMeasureSuccess = [...]byte{0xAA, 0x01, 0xC1}
var TemperatureMeasureError = [...]byte{0xAA, 0x01, 0xCE}
func isTemperatureSuccess(buf []byte) bool {
if len(buf) < len(TemperatureMeasureSuccess) {
return false
}
for i, b := range TemperatureMeasureSuccess {
if buf[i] != b {
return false
}
}
return true
}