Raspberry PiからLED電球にBLEでコマンドを送る(bleak使用)

ここを参考に
scrapbox.io

scan_devices.py

import asyncio
from bleak import BleakScanner

async def run():
    devices = await BleakScanner.discover()
    for d in devices:
        print(f"address: {d.address}, name: {d.name}, uuid: {d.metadata['uuids']}")

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

get_uuid.py

import asyncio
from bleak import BleakClient

address = "DC:23:4D:68:64:55"

async def run(address, loop):
    async with BleakClient(address, loop=loop) as client:
        x = client.is_connected
        print("Connected: {0}".format(x))
        for service in client.services:
            print(f"{service.uuid}: {service.description}: {[f'{c.properties},{c.uuid}' for c in service.characteristics]}")

loop = asyncio.get_event_loop()
loop.run_until_complete(run(address, loop))


send.py
動いてない

import asyncio
from bleak import BleakClient

address= "DC:23:4D:68:64:55"
#NUS_RX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
#NUS_TX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
WRITE = "00002b11-0000-1000-8000-00805f9b34fb"

def notif_hndlr(_, data):
    """Simple notification handler which prints the data received."""
    print(data)

async def main(address):
    async with BleakClient(address) as client:
        #print("Start notify")
        #await client.start_notify(NUS_TX, notif_hndlr)

        print("Sending Message...")
        #await client.write_gatt_char(WRITE, b"\x00\x31\x20")
        #await client.write_gatt_char(WRITE, b"\x01\xBC\xD4")
        #await client.write_gatt_char(WRITE, b"\x02\xD0\x3B")

        await client.write_gatt_char(WRITE, b"\x00\x21\x20")
        await client.write_gatt_char(WRITE, b"\x01\xD1\xAA")

        #print("Stopping notify after 3s")
        #await asyncio.sleep(3)
        #await client.stop_notify(NUS_TX)

        print("fin")

if __name__ == "__main__":
    asyncio.run(main(address))