-
Notifications
You must be signed in to change notification settings - Fork 7
/
tealblue.py
executable file
·412 lines (343 loc) · 14.5 KB
/
tealblue.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib
import threading
import queue
import traceback
import time
class NotConnectedError(Exception):
pass
class DBusInvalidArgsException(dbus.exceptions.DBusException):
_dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs'
def format_uuid(uuid):
if type(uuid) == int:
if uuid > 0xffff:
raise ValueError('32-bit UUID not supported yet')
uuid = '%04X' % uuid
return uuid
class TealBlue:
def __init__(self):
self._bus = dbus.SystemBus()
self._bluez = dbus.Interface(self._bus.get_object('org.bluez', '/'),
'org.freedesktop.DBus.ObjectManager')
def find_adapter(self):
# find the first adapter
objects = self._bluez.GetManagedObjects()
for path in sorted(objects.keys()):
interfaces = objects[path]
if 'org.bluez.Adapter1' not in interfaces:
continue
properties = interfaces['org.bluez.Adapter1']
return Adapter(self, path, properties)
return None # no adapter found
# copied from:
# https://github.com/adafruit/Adafruit_Python_BluefruitLE/blob/master/Adafruit_BluefruitLE/bluez_dbus/provider.py
def _print_tree(self):
"""Print tree of all bluez objects, useful for debugging."""
# This is based on the bluez sample code get-managed-objects.py.
objects = self._bluez.GetManagedObjects()
for path in sorted(objects.keys()):
print("[ %s ]" % (path))
interfaces = objects[path]
for interface in sorted(interfaces.keys()):
if interface in ["org.freedesktop.DBus.Introspectable",
"org.freedesktop.DBus.Properties"]:
continue
print(" %s" % (interface))
properties = interfaces[interface]
for key in sorted(properties.keys()):
print(" %s = %s" % (key, properties[key]))
class Adapter:
def __init__(self, teal, path, properties):
self._teal = teal
self._path = path
self._properties = properties
self._object = dbus.Interface(teal._bus.get_object('org.bluez', path), 'org.bluez.Adapter1')
self._advertisement = None
def __repr__(self):
return '<tealblue.Adapter address=%s>' % (self._properties['Address'])
def devices(self):
'''
Returns the devices that BlueZ has discovered.
'''
objects = self._teal._bluez.GetManagedObjects()
for path in sorted(objects.keys()):
interfaces = objects[path]
if 'org.bluez.Device1' not in interfaces:
continue
properties = interfaces['org.bluez.Device1']
yield Device(self._teal, path, properties)
def scan(self):
return Scanner(self._teal, self, self.devices())
@property
def advertisement(self):
if self._advertisement is None:
self._advertisement = Advertisement(self._teal, self)
return self._advertisement
def advertise(self, enable):
if enable:
self.advertisement.enable()
else:
self.advertisement.disable()
def advertise_data(self, local_name=None, service_data=None, service_uuids=None, manufacturer_data=None):
self.advertisement.local_name = local_name
self.advertisement.service_data = service_data
self.advertisement.service_uuids = service_uuids
self.advertisement.manufacturer_data = manufacturer_data
class Scanner:
def __init__(self, teal, adapter, initial_devices):
self._teal = teal
self._adapter = adapter
self._was_discovering = adapter._properties['Discovering'] # TODO get current value, or watch property changes
self._queue = queue.Queue()
for device in initial_devices:
self._queue.put(device)
def new_device(path, interfaces):
if not 'org.bluez.Device1' in interfaces:
return
if not path.startswith(self._adapter._path+'/'):
return
properties = interfaces['org.bluez.Device1']
self._queue.put(Device(self._teal, path, interfaces['org.bluez.Device1']))
self._signal_receiver = self._teal._bus.add_signal_receiver(new_device, dbus_interface='org.freedesktop.DBus.ObjectManager', signal_name='InterfacesAdded')
if not self._was_discovering:
self._adapter._object.StartDiscovery()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
if not self._was_discovering:
self._adapter._object.StopDiscovery()
self._signal_receiver.remove()
def __iter__(self):
return self
def __next__(self):
return self._queue.get()
class Device:
def __init__(self, teal, path, properties):
self._teal = teal
self._path = path
self._properties = properties
self._services_resolved = threading.Event()
self._services = None
if properties['ServicesResolved']:
self._services_resolved.set()
# Listen to device events (connect, disconnect, ServicesResolved, ...)
self._device = dbus.Interface(teal._bus.get_object('org.bluez', path), 'org.bluez.Device1')
self._device_props = dbus.Interface(self._device, 'org.freedesktop.DBus.Properties')
self._signal_receiver = self._device_props.connect_to_signal('PropertiesChanged', lambda itf, ch, inv: self._on_prop_changed(itf, ch, inv))
def __del__(self):
self._signal_receiver.remove()
def __repr__(self):
return '<tealblue.Device address=%s name=%r>' % (self.address, self.name)
def _on_prop_changed(self, properties, changed_props, invalidated_props):
for key, value in changed_props.items():
self._properties[key] = value
if 'ServicesResolved' in changed_props:
if changed_props['ServicesResolved']:
self._services_resolved.set()
else:
self._services_resolved.clear()
def _wait_for_discovery(self):
# wait until ServicesResolved is True
self._services_resolved.wait()
def connect(self):
self._device.Connect()
def disconnect(self):
self._device.Disconnect()
def resolve_services(self):
self._services_resolved.wait()
@property
def services(self):
if not self._services_resolved.is_set():
return None
if self._services is None:
self._services = {}
objects = self._teal._bluez.GetManagedObjects()
for path in sorted(objects.keys()):
if not path.startswith(self._path+'/'):
continue
if 'org.bluez.GattService1' in objects[path]:
properties = objects[path]['org.bluez.GattService1']
service = Service(self._teal, self, path, properties)
self._services[service.uuid] = service
elif 'org.bluez.GattCharacteristic1' in objects[path]:
properties = objects[path]['org.bluez.GattCharacteristic1']
characterstic = Characteristic(self._teal, self, path, properties)
for service in self._services.values():
if properties['Service'] == service._path:
service.characteristics[characterstic.uuid] = characterstic
return self._services
@property
def connected(self):
return bool(self._properties['Connected'])
@property
def services_resolved(self):
return bool(self._properties['ServicesResolved'])
@property
def UUIDs(self):
return [str(s) for s in self._properties['UUIDs']]
@property
def address(self):
return str(self._properties['Address'])
@property
def name(self):
if not 'Name' in self._properties:
return None
return str(self._properties['Name'])
@property
def alias(self):
if not 'Alias' in self._properties:
return None
return str(self._properties['Alias'])
class Service:
def __init__(self, teal, device, path, properties):
self._device = device
self._teal = teal
self._path = path
self._properties = properties
self.characteristics = {}
def __repr__(self):
return '<tealblue.Service device=%s uuid=%s>' % (self._device.address, self.uuid)
@property
def uuid(self):
return str(self._properties['UUID'])
class Characteristic:
def __init__(self, teal, device, path, properties):
self._device = device
self._teal = teal
self._path = path
self._properties = properties
self.on_notify = None
self._char = dbus.Interface(teal._bus.get_object('org.bluez', path), 'org.bluez.GattCharacteristic1')
char_props = dbus.Interface(self._char, 'org.freedesktop.DBus.Properties')
self._signal_receiver = char_props.connect_to_signal('PropertiesChanged', lambda itf, ch, inv: self._on_prop_changed(itf, ch, inv))
def __repr__(self):
return '<tealblue.Characteristic device=%s uuid=%s>' % (self._device.address, self.uuid)
def __del__(self):
self._signal_receiver.remove()
def _on_prop_changed(self, properties, changed_props, invalidated_props):
for key, value in changed_props.items():
self._properties[key] = bytes(value)
if 'Value' in changed_props and self.on_notify is not None:
self.on_notify(self, changed_props['Value'])
def read(self):
return bytes(self._char.ReadValue({}))
def write(self, value):
start = time.time()
try:
self._char.WriteValue(value, {})
except dbus.DBusException as e:
if e.get_dbus_name() == 'org.bluez.Error.Failed' and e.get_dbus_message() == 'Not connected':
raise NotConnectedError()
else:
raise # some other error
# Workaround: if the write took very long, it is possible the connection
# broke (without causing an exception). So check whether we are still
# connected.
# I think this is a bug in BlueZ.
if time.time() - start > 0.5:
if not self._device._device_props.Get('org.bluez.Device1', 'Connected'):
raise NotConnectedError()
def start_notify(self):
self._char.StartNotify()
@property
def uuid(self):
return str(self._properties['UUID'])
class Advertisement(dbus.service.Object):
PATH = '/com/github/aykevl/pynus/advertisement'
def __init__(self, teal, adapter):
self._teal = teal
self._adapter = adapter
self._enabled = False
self.service_uuids = None
self.manufacturer_data = None
self.solicit_uuids = None
self.service_data = None
self.local_name = None
self.include_tx_power = None
self._manager = dbus.Interface(teal._bus.get_object('org.bluez', self._adapter._path),
'org.bluez.LEAdvertisingManager1')
self._adv_enabled = threading.Event()
dbus.service.Object.__init__(self, teal._bus, self.PATH)
def enable(self):
if self._enabled:
return
self._manager.RegisterAdvertisement(dbus.ObjectPath(self.PATH), dbus.Dictionary({}, signature='sv'),
reply_handler=self._cb_enabled,
error_handler=self._cb_enabled_err)
self._adv_enabled.wait()
self._adv_enabled.clear()
def _cb_enabled(self):
self._enabled = True
if self._adv_enabled.is_set():
raise RuntimeError('called enable() twice')
self._adv_enabled.set()
def _cb_enabled_err(self, err):
self._enabled = False
if self._adv_enabled.is_set():
raise RuntimeError('called enable() twice')
self._adv_enabled.set()
def disable(self):
if not self._enabled:
return
self._bus.UnregisterAdvertisement(PATH)
self._enabled = False
@property
def enabled(self):
return self._enabled
@dbus.service.method('org.freedesktop.DBus.Properties',
in_signature='s',
out_signature='a{sv}')
def GetAll(self, interface):
print('GetAll')
if interface != 'org.bluez.LEAdvertisement1':
raise DBusInvalidArgsException()
try:
properties = {
'Type': dbus.String('peripheral'),
}
if self.service_uuids is not None:
properties['ServiceUUIDs'] = dbus.Array(map(format_uuid, self.service_uuids), signature='s')
if self.solicit_uuids is not None:
properties['SolicitUUIDs'] = dbus.Array(map(format_uuid, self.solicit_uuids), signature='s')
if self.manufacturer_data is not None:
properties['ManufacturerData'] = dbus.Dictionary({k: v for k, v in self.manufacturer_data.items()}, signature='qv')
if self.service_data is not None:
properties['ServiceData'] = dbus.Dictionary(self.service_data, signature='sv')
if self.local_name is not None:
properties['LocalName'] = dbus.String(self.local_name)
if self.include_tx_power is not None:
properties['IncludeTxPower'] = dbus.Boolean(self.include_tx_power)
except:
print('err')
print('properties:', properties)
return properties
@dbus.service.method('org.bluez.LEAdvertisement1',
in_signature='',
out_signature='')
def Release(self):
self._enabled = True
def glib_mainloop_wrapper(callback, args=()):
loop = GLib.MainLoop()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
def callback_wrapper():
try:
callback(*args)
except:
traceback.print_exc()
finally:
loop.quit()
threading.Thread(target=callback_wrapper, daemon=True).start()
loop.run()
def test():
adapter = TealBlue().find_adapter()
print('Bluetooth adapter:', adapter)
with adapter.scan() as scanner:
for device in scanner:
print('Device:', device)
for uuid in device.UUIDs:
print(' UUID:', uuid)
if __name__ == '__main__':
glib_mainloop_wrapper(test)