bleak
v0.22.3
Bleak 是蓝牙低功耗平台 Agnostic Klient 的缩写。
Bleak 是一款 GATT 客户端软件,能够连接到充当 GATT 服务器的 BLE 设备。它旨在提供异步、跨平台的 Python API 来与传感器等连接和通信。
$ pip install bleak
Bleak 支持从 GATT 服务器读取、写入和获取通知,以及发现 BLE 设备的功能。
要发现可以连接到的蓝牙设备:
import asyncio
from bleak import BleakScanner
async def main ():
devices = await BleakScanner . discover ()
for d in devices :
print ( d )
asyncio . run ( main ())
连接到蓝牙设备并读取其型号:
import asyncio
from bleak import BleakClient
address = "24:71:89:cc:09:05"
MODEL_NBR_UUID = "2A24"
async def main ( address ):
async with BleakClient ( address ) as client :
model_number = await client . read_gatt_char ( MODEL_NBR_UUID )
print ( "Model Number: {0}" . format ( "" . join ( map ( chr , model_number ))))
asyncio . run ( main ( address ))
不要将您的脚本命名为bleak.py
!它将导致循环导入错误。
有关更多代码,请参阅示例文件夹,例如连接到 TI SensorTag CC2650 的示例代码