-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreceiver.py
65 lines (53 loc) · 2.37 KB
/
receiver.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
from bleak import BleakClient, BleakScanner
from data import SensorDataDecoder
import time
class Receiver:
def __init__(self):
self.client = None
self.connected = False
self.callbacks = []
def __del__(self):
print("receiver destroyed")
self.disconnect()
async def scan_and_select_device(self):
print("Scanning for devices...")
devices = await BleakScanner.discover()
if not devices:
print("No devices found")
return None
print("Found devices:")
for i, device in enumerate(devices):
print(f"{i}: {device.name} ({device.address})")
index = int(input("Enter the number of the device you want to connect to: "))
self.device_address = devices[index].address
async def connect(self):
print("Trying to connect to device with MAC address:", self.device_address)
if not self.connected:
self.client = BleakClient(self.device_address)
await self.client.connect()
self.connected = True
print("Connected to device with MAC address:", self.device_address)
async def disconnect(self):
if self.connected:
await self.client.disconnect()
self.connected = False
print("Disconnected from device with MAC address:", self.device_address)
async def add_callback(self, callback):
self.callbacks.append(callback)
print("Callback added.")
async def remove_callback(self, callback):
self.callbacks.remove(callback)
print("Callback removed.")
async def handle_data(self, sender, data):
decoded = SensorDataDecoder.decode_data(data, time.perf_counter())
for callback in self.callbacks:
await callback(decoded)
print("Data handled by callbacks.")
async def start_notifications(self, service_uuid, characteristic_uuid):
print("Starting notifications for service UUID:", service_uuid, "and characteristic UUID:", characteristic_uuid)
await self.client.start_notify(characteristic_uuid, self.handle_data)
print("Notifications started.")
async def stop_notifications(self, characteristic_uuid):
print("Stopping notifications for characteristic UUID:", characteristic_uuid)
await self.client.stop_notify(characteristic_uuid)
print("Notifications stopped.")