-
Notifications
You must be signed in to change notification settings - Fork 140
/
gap_darwin.go
172 lines (140 loc) · 4.32 KB
/
gap_darwin.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package bluetooth
import (
"errors"
"fmt"
"time"
"github.com/JuulLabs-OSS/cbgo"
)
// Address contains a Bluetooth address which on macOS is a UUID.
type Address struct {
// UUID since this is macOS.
UUID
}
// IsRandom ignored on macOS.
func (ad Address) IsRandom() bool {
return false
}
// SetRandom ignored on macOS.
func (ad Address) SetRandom(val bool) {
}
// Set the address
func (ad Address) Set(val string) {
uuid, err := ParseUUID(val)
if err != nil {
return
}
ad.UUID = uuid
}
// Scan starts a BLE scan. It is stopped by a call to StopScan. A common pattern
// is to cancel the scan when a particular device has been found.
func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
if callback == nil {
return errors.New("must provide callback to Scan function")
}
if a.scanChan != nil {
return errors.New("already calling Scan function")
}
a.peripheralFoundHandler = callback
// Channel that will be closed when the scan is stopped.
// Detecting whether the scan is stopped can be done by doing a non-blocking
// read from it. If it succeeds, the scan is stopped.
a.scanChan = make(chan error)
a.cm.Scan(nil, &cbgo.CentralManagerScanOpts{
AllowDuplicates: false,
})
// Check whether the scan is stopped. This is necessary to avoid a race
// condition between the signal channel and the cancelScan channel when
// the callback calls StopScan() (no new callbacks may be called after
// StopScan is called).
select {
case <-a.scanChan:
close(a.scanChan)
a.scanChan = nil
return nil
}
}
// StopScan stops any in-progress scan. It can be called from within a Scan
// callback to stop the current scan. If no scan is in progress, an error will
// be returned.
func (a *Adapter) StopScan() error {
if a.scanChan == nil {
return errors.New("not calling Scan function")
}
a.scanChan <- nil
a.cm.StopScan()
return nil
}
// Device is a connection to a remote peripheral.
type Device struct {
delegate *peripheralDelegate
cm cbgo.CentralManager
prph cbgo.Peripheral
servicesChan chan error
charsChan chan error
services map[UUID]*DeviceService
characteristics map[UUID]*DeviceCharacteristic
}
// Connect starts a connection attempt to the given peripheral device address.
func (a *Adapter) Connect(address Addresser, params ConnectionParams) (*Device, error) {
adr := address.(Address)
uuid, err := cbgo.ParseUUID(adr.UUID.String())
if err != nil {
return nil, err
}
prphs := a.cm.RetrievePeripheralsWithIdentifiers([]cbgo.UUID{uuid})
if len(prphs) == 0 {
return nil, fmt.Errorf("Connect failed: no peer with address: %s", adr.UUID.String())
}
a.cm.Connect(prphs[0], nil)
// wait on channel for connect
select {
case p := <-a.connectChan:
d := &Device{
cm: a.cm,
prph: p,
servicesChan: make(chan error),
charsChan: make(chan error),
}
d.delegate = &peripheralDelegate{d: d}
p.SetDelegate(d.delegate)
a.connectHandler(nil, true)
return d, nil
case <-time.NewTimer(10 * time.Second).C:
return nil, errors.New("timeout on Connect")
}
}
// Disconnect from the BLE device. This method is non-blocking and does not
// wait until the connection is fully gone.
func (d *Device) Disconnect() error {
d.cm.CancelConnect(d.prph)
return nil
}
// Peripheral delegate functions
type peripheralDelegate struct {
cbgo.PeripheralDelegateBase
d *Device
}
// DidDiscoverServices is called when the services for a Peripheral
// have been discovered.
func (pd *peripheralDelegate) DidDiscoverServices(prph cbgo.Peripheral, err error) {
pd.d.servicesChan <- nil
}
// DidDiscoverCharacteristics is called when the characteristics for a Service
// for a Peripheral have been discovered.
func (pd *peripheralDelegate) DidDiscoverCharacteristics(prph cbgo.Peripheral, svc cbgo.Service, err error) {
pd.d.charsChan <- nil
}
// DidUpdateValueForCharacteristic is called when the characteristic for a Service
// for a Peripheral receives a notification with a new value,
// or receives a value for a read request.
func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripheral, chr cbgo.Characteristic, err error) {
uuid, _ := ParseUUID(chr.UUID().String())
if char, ok := pd.d.characteristics[uuid]; ok {
if err == nil && char.callback != nil {
go char.callback(chr.Value())
}
if char.readChan != nil {
char.readChan <- err
}
}
}