-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapter.go
75 lines (60 loc) · 1.96 KB
/
adapter.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
package sphero
import (
"fmt"
"github.com/hashicorp/go-hclog"
"tinygo.org/x/bluetooth"
)
var defaultAdapter = bluetooth.DefaultAdapter
// BluetoothAdapter allows the interaction with the phyiscal bluetooth stack
type BluetoothAdapter struct {
adapter *bluetooth.Adapter
log hclog.Logger
scanResult chan ScanResult
}
// ScanResult is returned from the Scan function and encapsulates the
// details of a Bluetooth device
type ScanResult struct {
Name string
Address bluetooth.Addresser
}
// NewBluetoothAdapter creates and initializes the default bluetooth adapter on the machine
func NewBluetoothAdapter(l hclog.Logger) (*BluetoothAdapter, error) {
err := defaultAdapter.Enable()
if err != nil {
l.Error("Unable to enable the bluetooth adapter", "error", err)
return nil, err
}
return &BluetoothAdapter{adapter: defaultAdapter, log: l}, nil
}
// Scan for bluetooth devices, this method returns a channel of ScanResult
// that can be constantly itterated over to print the devices
func (b *BluetoothAdapter) Scan() chan ScanResult {
b.scanResult = make(chan ScanResult)
go func() {
b.adapter.Scan(func(a *bluetooth.Adapter, d bluetooth.ScanResult) {
name := d.LocalName()
if name == "" {
name = "UNKNOWN"
}
b.scanResult <- ScanResult{Name: name, Address: d.Address}
})
}()
return b.scanResult
}
// StopScanning stops the scanning process and closes the ScanResult channel
// returned by the Scan function
func (b *BluetoothAdapter) StopScanning() {
b.adapter.StopScan()
close(b.scanResult)
}
// Connect to a bluetooth device
func (b *BluetoothAdapter) Connect(addr bluetooth.Addresser) (*bluetooth.Device, error) {
b.adapter.SetConnectHandler(func(device bluetooth.Addresser, connected bool) {
b.log.Trace("Connection status changed", "connected", connected)
})
device, err := b.adapter.Connect(addr, bluetooth.ConnectionParams{})
if err != nil {
return nil, fmt.Errorf("unable to connect to device: %s", err)
}
return device, nil
}