1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

bleakを使って、デバイスからNotifyを受信するPythonコード

import asyncio
from bleak import BleakClient

def notification_handler(sender, data: bytearray):
    print(data)

async def run(address, char_uuid, loop):
    async with BleakClient(address, loop=loop) as client:
        if client.is_connected:
            print(f"Connected to {address}")
            await client.start_notify(char_uuid, notification_handler)

            while True:
                await asyncio.sleep(1)
        else:
            print(f"Failed to connect to {address}")


if __name__ == "__main__":
    #address = "BB012A00-ADD9-6D16-AF1A-5D292BE864A0"  # BLE address for Mac
    address = "D8:DE:61:CF:0B:B4"  # BLE address for Windows
    char_uuid = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"  #  UUID of Nordic UART TX
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run(address, char_uuid, loop))

  • address:BLEデバイスのアドレス
     Windowsで使用するときは、MACアドレスを、
     Macで使用するときは、UUID を指定する。
  • char_uuid:NotifyキャラクタリスティックのUUID

デバイスのアドレスは、次のBLEスキャナーで確認できます。

BLEスキャナー

import asyncio
from bleak import BleakScanner

async def run():
    devices = await BleakScanner.discover(return_adv=True)
    for device, adv_data in devices.values():
        print(f"address: {device.address}, name: {device.name}, uuid: {adv_data.service_uuids}")

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?