树莓派4与ESP32基于BluePy的BLE频繁断开问题求助
Raspberry Pi 4 BLE Central与ESP32 Peripheral通信频繁断开问题
我最近碰到了一个非常棘手的BLE通信问题:树莓派4作为BLE中心端(客户端)连接ESP32外设端(服务端)时,连接极不稳定,但用安卓手机的nRF Connect应用连接ESP32时却全程稳定,这说明问题肯定出在树莓派本身或者我的代码逻辑上。
具体表现是:初始BLE连接能正常建立,也能成功读取特征值,但在随机次数(通常1-50次)的读取操作后,连接必然会断开。目前我使用的是BluePy 1.3.0版本,系统是全新刷入的Raspbian镜像。
以下是我的核心代码:
import time from bluepy.btle import Peripheral peripheral_address = "8c:aa:b5:85:20:1e" service_uuid = "537e7010-9928-4595-89dc-46b495862dc6" characteristic_uuid = "3778ceab-0974-4eb0-9da5-26c3a69cc742" # Read from peripheral p = Peripheral(peripheral_address, "public") #random does not work!! Service=p.getServiceByUUID(service_uuid) Characterization=Service.getCharacteristics(characteristic_uuid)[0] print("Got characterization") time.sleep(1) while True: value = Characterization.read() print(value) time.sleep(0.1)
连接断开时抛出的错误回溯信息:
Traceback (most recent call last): File "/home/pi/Desktop/BLETest/bleRead.py", line 16, in <module> value = Characterization.read() File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 197, in read return self.peripheral.readCharacteristic(self.valHandle) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 530, in readCharacteristic resp = self._getResp('rd') File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 407, in _getResp resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 362, in _waitResp raise BTLEDisconnectError("Device disconnected", resp) bluepy.btle.BTLEDisconnectError: Device disconnected
我尝试过添加异常捕获逻辑来处理断开后的重连,但不仅没解决根本问题,反而引发了更多未知错误,而且重连的耗时还会导致关键数据丢失。有没有具备树莓派客户端稳定BLE通信经验的开发者能给我一些可行的解决方案?非常感谢!
内容的提问来源于stack exchange,提问作者user1200233
相关产品推荐
相关产品推荐

