如何解决蓝牙设备断开连接时出现PyBluez事件
目前我正在使用PyBluez检查是否连接了特定的蓝牙设备
import threading
import bluetooth
known_devices_mac = ["FF:A0:AB:21:20:F4"]
def finder():
while True:
for mac in known_devices_mac: #1 second per device :( (if I add more devices time increases exponentially so that needs to be fixed as well :) (maybe every device would have a separate thread?? expecting around 20devices)
#Try to connect to device and if you can return some info
#10 seconds if device not in range
#1 second if device is in range
name = bluetooth.lookup_name(mac,timeout=1)
services = bluetooth.find_service(address=mac)
if name == None and services == []:
print("{0} not in range".format(mac))
CallAPINotInRange(mac)
else:
temp_mac = "nope"
try:
temp_mac = services[0]["host"]
except:
temp_mac = "nope"
if (temp_mac == mac): #sometimes services aren't defined ye>
print("{0} in range\n\tName: {1}\n\n".format(mac,name))
CallApiInRange(temp_mac)
def main():
#Bluetooth scanner thread
thread = threading.Thread(target=finder,name="finder")
thread.start()
if __name__ == "__main__":
main()
在配对模式下,我会获得设备的蓝牙地址(因为我发现(至少在我的Samsung Galaxy S10 +上)它不会更改
问题是我的API使用限制为每秒20个请求,每小时1500个请求,每天10000个请求,因此如果我每秒请求一次,我当然会很快达到上限(因为它要求设备是否存在,并且每秒检查设备是否存在
任何人都不知道我该如何创建一个事件或类似的事情,当设备实际上不在范围内时会触发该事件,这样我就可以减少调用我的API的次数
感谢纠缠和问候!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。