BLE设备在使用Python Gatt库重新连接时不会创建新的/dev/input/eventX
我是pythonBLE设备在使用Python Gatt库重新连接时不会创建新的/dev/input/eventX,python,bluetooth-lowenergy,gatt,evdev,Python,Bluetooth Lowenergy,Gatt,Evdev,我是pythongatt模块的新手,在重新连接方面遇到了问题。 基本上,我正在尝试使用pythongatt模块()与蓝牙低能(BLE)设备建立连接,然后使用evdev模块从/dev/input/eventX路径读取输入。我还想自动化重新连接过程,因此当设备超出范围并返回时,它将重新连接并继续正常工作 问题是,当设备断开连接并最终重新连接(通过以下简单例程:捕获断开连接消息->尝试重新连接)时,如果重新连接已花费2-3分钟以上,连接过程不会生成新的/dev/input/eventX路径。如果在前1
gatt
模块的新手,在重新连接方面遇到了问题。
基本上,我正在尝试使用pythongatt
模块()与蓝牙低能(BLE)设备建立连接,然后使用evdev
模块从/dev/input/eventX
路径读取输入。我还想自动化重新连接过程,因此当设备超出范围并返回时,它将重新连接并继续正常工作
问题是,当设备断开连接并最终重新连接(通过以下简单例程:捕获断开连接消息->尝试重新连接)时,如果重新连接已花费2-3分钟以上,连接过程不会生成新的/dev/input/eventX
路径。如果在前1-2分钟内重新连接成功,则不会发生这种情况
当2-3分钟过去时,我得到的错误是:
文件“/usr/lib/python3.7/site packages/dbus/proxies.py”,第145行,在
呼叫
文件“/usr/lib/python3.7/site packages/dbus/connection.py”,第651行,在call_blocking中
dbus.exceptions.DBusException:
org.freedesktop.DBus.Error.NoReply:未收到答复。可能的
原因包括:远程应用程序未发送回复,导致
消息总线安全策略阻止了回复,回复超时
已过期,或网络连接已断开
该脚本的核心内容如下:
def reconnect(mac_address):
try:
devices[mac_address].connect()
except:
print(f"thread from {mac_address} crashed")
class AnyDevice(gatt.Device):
shut_down_flag = False
def connect_succeeded(self):
super().connect_succeeded()
print(f"{self.mac_address} Connected")
def connect_failed(self, error):
super().connect_failed(error)
print(f"{self.mac_address} Connection failed.")
reconnect_thread = threading.Thread(target=reconnect, name=f'reconnect {self.mac_address}',args=(self.mac_address,))
reconnect_thread.start()
def disconnect_succeeded(self):
super().disconnect_succeeded()
print(f"{self.mac_address} Disconnected")
if not self.shut_down_flag:
reconnect_thread = threading.Thread(target=reconnect, name=f'reconnect {self.mac_address}',args=(self.mac_address,))
reconnect_thread.start()
def gatt_connect_device(mac_address):
global devices
devices.update({f'{mac_address}': AnyDevice(mac_address=f'{mac_address}', manager=manager)})
devices[f'{mac_address}'].connect()
#==== OPEN bd_addresses.txt JSON FILE ====#
if path.exists("bd_addresses.txt"):
with open("bd_addresses.txt", "r") as mac_addresses_json:
mac_addresses = json.load(mac_addresses_json)
else:
print("bd_addresses.txt file NOT FOUND\nPlace it in the same directory as the multiple_scanners.py")
#========================================#
devices={}
manager = gatt.DeviceManager(adapter_name='hci0')
for scanner_number in mac_addresses:
device_instance_thread=threading.Thread(target=gatt_connect_device, name=f'device instance for {mac_addresses[scanner_number]}', args=(mac_addresses[scanner_number],))
device_instance_thread.start()
time.sleep(3)
manager.run()
你能不能提供一段代码,让我们看看,更好地帮助你?我做到了!顺便说一句,谢谢你编辑我的文章,我会在将来更加小心我的语法,以提高文章的可读性。太好了,希望有人能够帮助。另外,关于编辑,没有问题。这就是堆栈溢出如何保持其非常好的问题和答案标准。我鼓励你对你看到的帖子也这么做!很幸运,你能提供你的代码片段吗,这样我们可以看一看,更好地帮助你?我做到了!顺便说一句,谢谢你编辑我的文章,我会在将来更加小心我的语法,以提高文章的可读性。太好了,希望有人能够帮助。另外,关于编辑,没有问题。这就是堆栈溢出如何保持其非常好的问题和答案标准。我鼓励你对你看到的帖子也这么做!祝你好运