在运行python脚本5-15分钟后,Raspberry PI似乎正在放弃与(某些?)配对设备的连接。
可以使用以下shell命令重新创建Connection:
bluetoothctl
但要输入此命令,脚本必须在之后被杀死并重新启动。
注意:经过一段时间的测试后,我发现它并没有解决这个问题
不工作:
虽然我没有测试运行我的程序几个小时,但这似乎可以解决问题。
from threading import Thread
from time import sleep
import subprocess
def bluetooth_stay_awake():
"""Ping the bluetooth compontent to keep all connections up"""
thread= Thread(target=(lambda:subprocess.run(["watch", "bluetoothctl"])))
thread.start()
使用shell命令watch bluetoothctl
在这里很重要,否则bluetoothctl
进程会阻止你的python脚本。
Python中的Shell Commands和Threading