Do this without the asyncio with.

This commit is contained in:
Danny Staple
2022-11-03 16:34:28 +00:00
parent d03c48bf8d
commit 0a8a86960d
2 changed files with 34 additions and 33 deletions
@@ -11,11 +11,9 @@ class BleConnection:
adafruit_tx_uuid = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
ble_name = "Adafruit Bluefruit LE"
def __init__(self, receive_handler: typing.Callable[[bytes], None],
connected_handler: typing.Callable[[], None]):
def __init__(self, receive_handler: typing.Callable[[bytes], None]):
self.ble_client : bleak.BleakClient = None
self.receive_handler = receive_handler
self.connected_handler = connected_handler
def _uart_handler(self, _, data: bytes):
self.receive_handler(data)
@@ -27,16 +25,16 @@ class BleConnection:
print([device.name for device in devices])
ble_device_info = [device for device in devices if device.name==self.ble_name][0]
print("Connecting to {}...".format(ble_device_info.name))
async with bleak.BleakClient(ble_device_info.address) as ble_client:
self.ble_client = ble_client
self.connected_handler()
print("Connected to {}".format(ble_device_info.name))
asyncio.create_task(
self.ble_client.start_notify(self.adafruit_rx_uuid, self._uart_handler)
)
while True:
await asyncio.sleep(1)
self.ble_client = bleak.BleakClient(ble_device_info.address)
await self.ble_client.connect()
print("Connected to {}".format(ble_device_info.name))
self.notify_task = asyncio.create_task(
self.ble_client.start_notify(self.adafruit_rx_uuid, self._uart_handler)
)
async def close(self):
await self.ble_client.disconnect()
def send_uart_data(self, data: bytes):
if self.ble_client:
asyncio.create_task(