以前(といっても4、5年くらい前)、WindowsでBLEのプログラミングしようと思ったら、情報も少なくすごく大変で、卒研生の研究時間の大半をBLE接続に費やしてしまった経験があり、もう二度とWindowsでBLEプログラムをするもんか。。。と誓ったことがありましたが、今は便利になってきてますね。Pythonであっさりできます。
Bleakというライブラリ
PythonでBLEというのはまあ以前から目にしたことはあり、ちょっとトライしたことがありましたが、最近はBleakという便利なライブラリがあります。Windows,Mac,Linuxで動作するという。。
asyncioで非同期処理
Bleak自体の使い方は超シンプルなのでほぼ説明不要。肝は、asyncioで非同期処理をするということですね。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from bleak import discover | |
import time | |
async def run(): | |
while True: | |
devices = await discover() | |
for d in devices: | |
print(d.address,d.metadata,d.name,d.rssi) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(run3()) |
0 件のコメント:
コメントを投稿