Ubuntu 24.04 にライブラリーをインストール
sudo apt install python3-bleak
バージョンの確認
$ python
Python 3.12.3 (main, Sep 11 2024, 14:17:37) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from importlib.metadata import version
>>> bleak_version = version("bleak")
>>> print(bleak_version)
0.21.1
プログラム
ble_scan.py
#! /usr/bin/python
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}")
if __name__ == "__main__":
asyncio.run(run())
実行結果
$ ./ble_scan.py
address: 49:C4:23:DE:A4:F8, name: 49-C4-23-DE-A4-F8, uuid: []
address: 08:3A:F2:66:04:4A, name: BatteryMonitor, uuid: ['00001101-0000-1000-8000-00805f9b34fb']
address: FE:C0:60:7D:A3:B8, name: FE-C0-60-7D-A3-B8, uuid: ['0000fd3d-0000-1000-8000-00805f9b34fb']
address: 5E:B4:1D:9D:11:EC, name: 5E-B4-1D-9D-11-EC, uuid: ['0000fef3-0000-1000-8000-00805f9b34fb']
address: D9:16:26:43:E4:D4, name: D9-16-26-43-E4-D4, uuid: ['0000fd3d-0000-1000-8000-00805f9b34fb']
address: 0E:98:0C:72:A6:E4, name: 0E-98-0C-72-A6-E4, uuid: []
address: 54:B7:E9:55:FF:57, name: 54-B7-E9-55-FF-57, uuid: []
address: E5:D7:17:7C:E3:46, name: E5-D7-17-7C-E3-46, uuid: ['0000fd3d-0000-1000-8000-00805f9b34fb']