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 的範例程式碼