-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble.py
337 lines (304 loc) · 12.3 KB
/
ble.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
import asyncio
import enum
import threading
import queue
from bleak import BleakScanner, BleakClient
class BleStatus(enum.Enum):
Disconnected = enum.auto()
Connecting = enum.auto()
Connected = enum.auto()
Disconnecting = enum.auto()
WriteSuccessful = enum.auto()
NotificationsEnabled = enum.auto()
NotificationsDisabled = enum.auto()
class Ble:
def __init__(self):
self.found_devices = {}
self.found_device = False
self.scanning = False
self.connected_devices = {}
self.disconnect_events = {}
self.notification_devices = {}
self.stop_notify_events = {}
self.status_queue = queue.Queue()
self.data_queue = queue.Queue()
self.status_devices = {}
self.event_loop = asyncio.new_event_loop()
self.event_loop_thread = threading.Thread(
target=self._asyncloop, daemon=True
)
self.event_loop_thread.start()
def __del__(self):
# stop scanning
if self.scanning:
self.stop_scan()
# stop notifications
for dev_addr, dev_events in self.stop_notify_events.items():
for char_uuid in dev_events.keys():
self.stop_notifications_characteristic(dev_addr, char_uuid)
# disconnect from connected devices
for dev_addr in self.connected_devices.keys():
self.disconnect(dev_addr)
self.event_loop.call_soon_threadsafe(self.event_loop.stop)
self.event_loop_thread.join()
def start_scan(self):
# clear previously found devices
self.found_devices = {}
self.scan_stop_event = asyncio.Event()
asyncio.run_coroutine_threadsafe(
self.bluetooth_scan(self.scan_stop_event), self.event_loop
)
self.scanning = True
def stop_scan(self):
if self.scanning:
self.event_loop.call_soon_threadsafe(self.scan_stop_event.set)
self.scanning = False
def is_scanning(self):
return self.scanning
def has_found_device(self):
ret_val = self.found_device
self.found_device = False
return ret_val
def get_found_devices(self):
devices = []
for address, (
device,
advertisement_data,
) in self.found_devices.items():
dev = {
"name": advertisement_data.local_name,
"address": address,
"rssi": advertisement_data.rssi,
"uuids": advertisement_data.service_uuids,
"manufacturer_data": advertisement_data.manufacturer_data,
"dev": device,
}
devices.append(dev)
return devices
def connect(self, dev):
self.status_devices[dev.address] = BleStatus.Connecting
try:
self.status_queue.put_nowait((dev.address, BleStatus.Connecting))
except queue.Full:
# TODO better handling of this case
pass
self.disconnect_events[dev.address] = asyncio.Event()
asyncio.run_coroutine_threadsafe(
self.bluetooth_connect(dev, self.disconnect_events[dev.address]),
self.event_loop,
)
def disconnect(self, dev_address):
self.status_devices[dev_address] = BleStatus.Disconnecting
try:
self.status_queue.put_nowait((dev_address, BleStatus.Disconnecting))
except queue.Full:
# TODO better handling of this case
pass
# stop notifications if any
for dev_addr, dev_events in self.stop_notify_events.items():
for char_uuid in dev_events.keys():
self.stop_notifications_characteristic(dev_addr, char_uuid)
self.event_loop.call_soon_threadsafe(
self.disconnect_events[dev_address].set
)
def is_connected(self, dev_address):
return dev_address in self.connected_devices
def get_connected_devices(self):
return list(self.connected_devices.keys())
def get_status(self, dev_address):
if dev_address in self.status_devices:
return self.status_devices[dev_address]
else:
return None
def get_status_event(self):
try:
return self.status_queue.get_nowait()
except queue.Empty:
return None
def get_data_event(self):
try:
return self.data_queue.get_nowait()
except queue.Empty:
return None
def get_services_and_characteristics(self, dev_address):
if not self.is_connected(dev_address):
services_collection = None
else:
services_collection = {}
dev = self.connected_devices[dev_address]
for _, service in dev.services.services.items():
services_collection[service.uuid] = {
"name": service.description,
"service": service,
}
service_characteristics = {}
for characteristic in service.characteristics:
service_characteristics[characteristic.uuid] = {
"name": characteristic.description,
"properties": characteristic.properties,
"characteristic": characteristic,
}
characteristic_descriptors = {}
for descriptor in characteristic.descriptors:
characteristic_descriptors[descriptor.uuid] = {
"name": descriptor.description,
"descriptor": descriptor,
}
service_characteristics[characteristic.uuid][
"descriptors"
] = characteristic_descriptors
services_collection[service.uuid][
"characteristics"
] = service_characteristics
return services_collection
def read_characteristic(self, dev_addr, char_uuid):
if self.is_connected(dev_addr):
client = self.connected_devices[dev_addr]
chars = list(client.services.characteristics.values())
chars_uuids = [char.uuid for char in chars]
chars_properties = [char.properties for char in chars]
if char_uuid in chars_uuids:
i_char = chars_uuids.index(char_uuid)
if "read" in chars_properties[i_char]:
asyncio.run_coroutine_threadsafe(
self.bluetooth_read(client, char_uuid), self.event_loop
)
def write_characteristic(self, dev_addr, char_uuid, data):
if self.is_connected(dev_addr):
client = self.connected_devices[dev_addr]
chars = list(client.services.characteristics.values())
chars_uuids = [char.uuid for char in chars]
chars_properties = [char.properties for char in chars]
if char_uuid in chars_uuids:
i_char = chars_uuids.index(char_uuid)
if "write" in chars_properties[i_char]:
asyncio.run_coroutine_threadsafe(
self.bluetooth_write(client, char_uuid, data),
self.event_loop,
)
def start_notifications_characteristic(self, dev_addr, char_uuid):
if self.is_connected(dev_addr):
client = self.connected_devices[dev_addr]
chars = list(client.services.characteristics.values())
chars_uuids = [char.uuid for char in chars]
chars_properties = [char.properties for char in chars]
if char_uuid in chars_uuids:
i_char = chars_uuids.index(char_uuid)
if "notify" in chars_properties[i_char]:
if dev_addr not in self.stop_notify_events:
self.stop_notify_events[dev_addr] = {}
self.stop_notify_events[dev_addr][
char_uuid
] = asyncio.Event()
if dev_addr not in self.notification_devices:
self.notification_devices[dev_addr] = {}
asyncio.run_coroutine_threadsafe(
self.bluetooth_notify(
client,
char_uuid,
self.stop_notify_events[dev_addr][char_uuid],
),
self.event_loop,
)
def stop_notifications_characteristic(self, dev_addr, char_uuid):
if (
dev_addr in self.stop_notify_events.keys()
and char_uuid in self.stop_notify_events[dev_addr].keys()
):
self.event_loop.call_soon_threadsafe(
self.stop_notify_events[dev_addr][char_uuid].set
)
def are_notifications_enabled(self, dev_addr, char_uuid):
return (
dev_addr in self.notification_devices.keys()
and char_uuid in self.notification_devices[dev_addr].keys()
)
async def bluetooth_scan(self, stop_event):
async with BleakScanner(
detection_callback=self._detection_callback,
):
await stop_event.wait()
def _detection_callback(self, device, advertisement_data):
if advertisement_data.local_name is not None:
self.found_devices[device.address] = (
device,
advertisement_data,
)
self.found_device = True
async def bluetooth_connect(self, device, disconnect_event):
async with BleakClient(
device,
self._disconnect_callback,
) as client:
self.connected_devices[device.address] = client
self.status_devices[device.address] = BleStatus.Connected
try:
self.status_queue.put_nowait(
(device.address, BleStatus.Connected)
)
except queue.Full:
# TODO better handling of this case
pass
await disconnect_event.wait()
def _disconnect_callback(self, client):
del self.connected_devices[client.address]
del self.status_devices[client.address]
if client.address in self.notification_devices.keys():
del self.notification_devices[client.address]
try:
self.status_queue.put_nowait(
(client.address, BleStatus.Disconnected)
)
except queue.Full:
# TODO better handling of this case
pass
async def bluetooth_read(self, client, uuid):
data = await client.read_gatt_char(uuid)
try:
self.data_queue.put_nowait((client.address, uuid, data))
except queue.Full:
# TODO better handling of this case
pass
async def bluetooth_write(self, client, uuid, data):
await client.write_gatt_char(uuid, data)
try:
self.status_queue.put_nowait(
(client.address, BleStatus.WriteSuccessful, uuid)
)
except queue.Full:
# TODO better handling of this case
pass
async def bluetooth_notify(self, client, uuid, stop_event):
await client.start_notify(
uuid,
lambda uuid, data: self.bluetooth_notify_callback(
client, uuid, data
),
)
self.notification_devices[client.address][uuid] = True
try:
self.status_queue.put_nowait(
(client.address, BleStatus.NotificationsEnabled, uuid)
)
except queue.Full:
# TODO better handling of this case
pass
await stop_event.wait()
await client.stop_notify(uuid)
del self.notification_devices[client.address]
try:
self.status_queue.put_nowait(
(client.address, BleStatus.NotificationsDisabled, uuid)
)
except queue.Full:
# TODO better handling of this case
pass
def bluetooth_notify_callback(self, client, char, data):
try:
self.data_queue.put_nowait((client.address, char.uuid, data))
except queue.Full:
# TODO better handling of this case
pass
def _asyncloop(self):
asyncio.set_event_loop(self.event_loop)
self.event_loop.run_forever()