Last active
February 15, 2022 11:49
-
-
Save ivanjimenez/8da48c74fe38f08b155179b21b16e3d9 to your computer and use it in GitHub Desktop.
Bleak Multiple Device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from bleak import BleakClient | |
from functools import partial | |
CHAR_UUID = "00000080-0001-11e1-ac36-0002a5d5c51b".format(0xFFE1) | |
def callback(client: BleakClient, sender: int, data: bytearray): | |
hex_string = data.hex() | |
print(f"{client.address}", "".join([hex_string[x:x + 2] for x in range(0, len(hex_string), 2)])) | |
def run(addresses): | |
loop = asyncio.get_event_loop() | |
loop1 = asyncio.get_event_loop() | |
for address in addresses: | |
try: | |
loop.create_task(connect_to_device(address)) | |
except Exception as e: | |
loop.stop() | |
address = address | |
loop1.create_task(connect_to_device(address)) | |
loop.run_forever() | |
loop1.run_forever() | |
async def connect_to_device(address): | |
async with BleakClient(address, timeout=20.0, use_cached=False) as client: | |
print(f"Connected: {address} {client.is_connected}") | |
await asyncio.sleep(0.8) | |
while True: | |
await client.start_notify(CHAR_UUID, partial(callback, client)) | |
if __name__ == "__main__": | |
run(["26:80:E1:4E:F8:B6", "26:80:E1:AD:99:34", "26:80:E1:C1:90:51", "26:80:E1:C1:11:D0", "26:80:E1:16:E4:F2"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment