/
Device1.go
110 lines (95 loc) · 2.64 KB
/
Device1.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
package profile
import (
"github.com/godbus/dbus"
"github.com/saurabh-newera/BLE/bluez"
"github.com/op/go-logging"
)
// NewDevice1 create a new Device1 client
func NewDevice1(path string) *Device1 {
a := new(Device1)
a.client = bluez.NewClient(
&bluez.Config{
Name: "org.bluez",
Iface: "org.bluez.Device1",
Path: path,
Bus: bluez.SystemBus,
},
)
a.logger = logging.MustGetLogger(path)
a.Properties = new(Device1Properties)
a.GetProperties()
return a
}
// Device1 client
type Device1 struct {
client *bluez.Client
logger *logging.Logger
Properties *Device1Properties
}
// Device1Properties exposed properties for Device1
type Device1Properties struct {
UUIDs []string
Blocked bool
Connected bool
LegacyPairing bool
Paired bool
ServicesResolved bool
Trusted bool
ServiceData map[string]dbus.Variant
ManufacturerData map[uint16]dbus.Variant
RSSI int16
TxPower int16
Adapter dbus.ObjectPath
Address string
Alias string
Icon string
Modalias string
Name string
Appearance uint16
Class uint32
}
// Close the connection
func (d *Device1) Close() {
d.client.Disconnect()
}
//Register for changes signalling
func (d *Device1) Register() (chan *dbus.Signal, error) {
return d.client.Register(d.client.Config.Path, bluez.PropertiesInterface)
}
//Unregister for changes signalling
func (d *Device1) Unregister() error {
return d.client.Unregister(d.client.Config.Path, bluez.PropertiesInterface)
}
//GetProperties load all available properties
func (d *Device1) GetProperties() (*Device1Properties, error) {
err := d.client.GetProperties(d.Properties)
return d.Properties, err
}
//GetProperty get a property
func (d *Device1) GetProperty(name string) (dbus.Variant, error) {
return d.client.GetProperty(name)
}
//CancelParing stop the pairing process
func (d *Device1) CancelParing() error {
return d.client.Call("CancelParing", 0).Store()
}
//Connect to the device
func (d *Device1) Connect() error {
return d.client.Call("Connect", 0).Store()
}
//ConnectProfile connect to the specific profile
func (d *Device1) ConnectProfile(uuid string) error {
return d.client.Call("ConnectProfile", 0, uuid).Store()
}
//Disconnect from the device
func (d *Device1) Disconnect() error {
return d.client.Call("Disconnect", 0).Store()
}
//DisconnectProfile from the device
func (d *Device1) DisconnectProfile(uuid string) error {
return d.client.Call("DisconnectProfile", 0, uuid).Store()
}
//Pair with the device
func (d *Device1) Pair() error {
return d.client.Call("Pair", 0).Store()
}