-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluez_adapter.go
89 lines (80 loc) · 2.2 KB
/
bluez_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package bluez
import (
"github.com/godbus/dbus"
"github.com/pkg/errors"
"strings"
)
func (self *BlueZAdapter) GetDevices() (devices []*BlueZDevice, e error) {
if managedObjects, err := self.bluez.getManagedObjects(); err == nil {
for path, o := range managedObjects {
if HasPrefix(path, self.Object.Path()) {
if data, exists := o["org.bluez.Device1"]; exists {
device := &BlueZDevice{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, adapter: self, data: data}
devices = append(devices, device)
}
}
}
} else {
e = err
}
return
}
func (self *BlueZAdapter) GetGattManager() (gattManager *BlueZGattManager, e error) {
if managedObjects, err := self.bluez.getManagedObjects(); err == nil {
for path, o := range managedObjects {
if HasPrefix(path, self.Object.Path()) {
if data, exists := o["org.bluez.GattManager1"]; exists {
blueZGattManager = &BlueZGattManager{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, adapter: self, data: data}
gattManager = blueZGattManager
return
}
}
}
if gattManager == nil {
e = errors.New("GattManager not found")
}
} else {
e = err
}
return
}
func (self *BlueZAdapter) StartDiscovery() (e error) {
if call := self.Object.Call("org.bluez.Adapter1.StartDiscovery", 0); call.Err == nil {
} else {
e = call.Err
}
return
}
func (self *BlueZAdapter) StopDiscovery() (e error) {
if call := self.Object.Call("org.bluez.Adapter1.StopDiscovery", 0); call.Err == nil {
} else {
e = call.Err
}
return
}
func (self *BlueZAdapter) FindPeripheral(nameOrAddress string) (foundDevice *BlueZDevice) {
tokens := strings.Split(nameOrAddress, ":")
var findByAddress = len(tokens) == 6
nameOrAddress = strings.ToLower(nameOrAddress)
if devices, err := self.GetDevices(); err == nil {
for _, device := range devices {
var key string
if findByAddress {
key = device.Address()
} else {
key = device.Name()
}
if strings.ToLower(key) == nameOrAddress {
foundDevice = device
return
}
}
}
return
}
type BlueZAdapter struct {
BlueZObject
bluez *BlueZ
data map[string]dbus.Variant
advertisements map[dbus.ObjectPath]*LEAdvertisement1
}