首先,确保所使用的蓝牙适配器具有BLE支持。然后,尝试添加以下代码以修复问题:
import asyncio
from bleak import BleakClient
async def run(address):
async with BleakClient(address) as client:
# Add the following line to enable notifications
await client.start_notify("0000ffe1-0000-1000-8000-00805f9b34fb", callback)
# The rest of your code goes here
async def callback(sender, data):
print(f"Received data: {data}")
loop = asyncio.get_event_loop()
loop.run_until_complete(run("00:11:22:33:44:55"))
这会启用通知功能并确保当BLE设备有新数据时您的代码会得到通知。