-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluez.py
155 lines (127 loc) · 5.04 KB
/
bluez.py
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
import dbus_next
async def connect(bus, listener):
client = BluezClient(bus, listener)
await bus.call(destination='org.freedesktop.DBus',
path='/',
interface='org.freedesktop.DBus',
member='AddMatch',
signature='s',
body=["type='signal',sender='org.bluez',path_namespace='/'"])
reply = await bus.call(destination='org.bluez',
path='/',
interface='org.freedesktop.DBus.ObjectManager',
member='GetManagedObjects')
client._init_objects(reply[0])
return client
class BluezClient:
def __init__(self, bus, listener):
self._bus = bus
self._listener = listener
self._adapter = None
self._devices = {}
self._handlers = {
'org.freedesktop.DBus.ObjectManager': {
'InterfacesAdded': self._interfaces_added,
'InterfacesRemoved': self._interfaces_removed
},
'org.freedesktop.DBus.Properties': {
'PropertiesChanged': self._properties_changed
}
}
bus.add_message_handler(self._handle_message)
def disconnect(self):
self._bus.remove_message_handler(self._handle_message)
def _init_objects(self, tree):
for path, interfaces in tree.items():
self._check_added_adapters(path, interfaces)
self._check_added_devices(path, interfaces)
def _handle_message(self, path, interface, member, body):
try:
handler = self._handlers[interface][member]
except KeyError:
return
handler(path, body)
def _interfaces_added(self, _, body):
self._check_added_adapters(*body)
self._check_added_devices(*body)
def _check_added_adapters(self, path, interfaces):
try:
interfaces['org.bluez.Adapter1']
except KeyError:
return
self._listener.add_adapter(Adapter(self._bus, path))
def _check_added_devices(self, path, interfaces):
try:
interface = interfaces['org.bluez.Device1']
address = interface['Address'].value
connected = interface['Connected'].value
except KeyError:
return
if path not in self._devices:
self._devices[path] = address
self._listener.add_device(address, Device(self._bus, path), connected)
def _interfaces_removed(self, _, body):
path, interfaces = body
if 'org.bluez.Device1' in interfaces:
try:
address = self._devices[path]
except KeyError:
return
del self._devices[path]
self._listener.remove_device(address)
def _properties_changed(self, path, body):
interface, changed, invalidated = body
if interface == 'org.bluez.Device1':
try:
address = self._devices[path]
connected = changed['Connected'].value
except KeyError:
return
self._listener.update_device(address, connected)
class _Listener:
def __init__(self, listener, properties):
self.listener = listener
self.properties = properties
class Adapter:
def __init__(self, bus, path):
self._bus = bus
self.path = path
async def remove_device(self, device):
await self._call(member='RemoveDevice',
signature='o',
body=[device.path])
async def set_discovery_filter(self, filter={}):
await self._call(member='SetDiscoveryFilter',
signature='a{sv}',
body=[filter])
async def start_discovery(self):
await self._call(member='StartDiscovery')
async def stop_discovery(self):
await self._call(member='StopDiscovery')
async def _call(self, **kwargs):
await self._bus.call(destination='org.bluez',
path=self.path,
interface='org.bluez.Adapter1',
**kwargs)
class Device:
def __init__(self, bus, path):
self._bus = bus
self.path = path
async def pair(self):
await self._call(member='Pair')
async def connect(self):
await self._call(member='Connect')
async def disconnect(self):
await self._call(member='Disconnect')
async def trust(self):
await self._bus.call(destination='org.bluez',
path=self.path,
interface='org.freedesktop.DBus.Properties',
member='Set',
signature='ssv',
body=['org.bluez.Device1', 'Trusted', dbus_next.Variant('b', True)])
async def _call(self, **kwargs):
await self._bus.call(destination='org.bluez',
path=self.path,
interface='org.bluez.Device1',
**kwargs)