检查蓝牙开关是否超时
要实现打开蓝牙和关闭蓝牙的操作,分别在5秒和3秒内成功则视为通过(pass),如果超时则视为失败(failed),同时确保整个操作的最大超时不超过30秒,可以对现有代码进行一些调整。以下是实现这个逻辑的示例代码:
代码示例
import subprocess
import time
import threadingdef check_bt_state(stop_checking):"""检查蓝牙状态并输出"""try:# 执行 adb 命令获取蓝牙状态result = subprocess.run(['adb', 'shell', 'settings', 'get', 'global', 'bluetooth_on'], capture_output=True, text=True)# 输出结果output = result.stdout.strip()if output == "1":print("bt开关已打开")elif output == "0":print("bt开关已关闭")else:print("执行异常")except Exception as e:print(f"发生错误: {e}")# 如果设置了停止标志,则退出检测if stop_checking.is_set():returndef