-
Hey there ! I currently have a class to handle BLE actions (with functions like send_data, set_x, set_y...). I call one function every 100ms but the usage of with...as syntax (see below) is not adapted to my class syntax. Is it possible to write it on another way ? def __init__(self, address, uuid, characteristic):
self.uuid = uuid
self.address = address
self.characteristic = characteristic
self.device = None
async def connect(self):
print("waiting for connection...")
async with BleakClient(self.address) as client:
print(f"Connected : {client.is_connected}")
self.device = client
await self.set_color(self.currentColor)
async def set_color(self, c):
self.currentColor = c
await self.device.write_gatt_char(self.characteristic, bytearray(color(c)) If I create a class instance and then call set_color, I get a "Not connected" error, which I assume is caused beacause we exited the with...as statement. Sorry if it's a newbie question, I never learned python and just use it for scripting x) Thanks in advance :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
https://bleak.readthedocs.io/en/latest/api/client.html#connecting-and-disconnecting |
Beta Was this translation helpful? Give feedback.
-
Yes, just found that in the doc, it completely does the job, thank you ! import asyncio
from bleak import BleakClient
address = "24:71:89:cc:09:05"
MODEL_NBR_UUID = "00002a24-0000-1000-8000-00805f9b34fb"
async def main(address):
client = BleakClient(address)
try:
await client.connect()
model_number = await client.read_gatt_char(MODEL_NBR_UUID)
print("Model Number: {0}".format("".join(map(chr, model_number))))
except Exception as e:
print(e)
finally:
await client.disconnect()
asyncio.run(main(address)) |
Beta Was this translation helpful? Give feedback.
Yes, just found that in the doc, it completely does the job, thank you !