Raspberry Pi 4B连接Witmotion WT901BLECL传感器失败求助
蓝牙IMU传感器连接失败问题求助
我使用Raspberry Pi 4B搭配Witmotion WT901BLECL蓝牙AHRS IMU传感器(支持加速度、角速度、角度及磁场检测),运行以下Python代码时出现连接失败错误,无法实时获取传感器原始数据,求解决方法。
运行代码
from bluepy.btle import UUID, Peripheral, DefaultDelegate import time class NotifyDelegate(DefaultDelegate): def __init__(self): DefaultDelegate.__init__(self) def handleNotification(self,cHandle,data): print("notify from "+str(cHandle)+str(data)+"\n") p = Peripheral("FD:D4:23:B6:5D:DA").withDelegate(NotifyDelegate()) time.sleep(0.5) dev = 0 for ser in p.getServices(): print(str(ser)) for chara in ser.getCharacteristics(): print(str(chara)) print("Handle is "+str(chara.getHandle())) print("properties is "+chara.propertiesToString()) print("uuid is "+str(chara.uuid)) if "0000ffe4" in str(chara.uuid): dev = chara dev.write("1", False) print("en") if(chara.supportsRead()): data = chara.read() print(type(data)) print(len(data)) print(data) print("\n") try: while True: if dev: dev.write("1", True) print("en") if(dev.supportsRead()): data = dev.read() print(type(data)) print(len(data)) print(data) if p.waitForNotifications(1.0): continue print ("Waiting...") except ZeroDivisionError as e: print("except:",e) p.disconnect() exit(1) finally: print("final print") p.disconnect() exit(1) p.disconnect() exit(1)
错误信息
****Traceback (most recent call last): File "/home/project/Witmotion/Wit_ble.py", line 12, in <module> p = Peripheral("FD:D4:23:B6:5D:DA").withDelegate(NotifyDelegate()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/dist-packages/bluepy/btle.py", line 391, in __init__ self._connect(deviceAddr, addrType, iface) File "/usr/local/lib/python3.11/dist-packages/bluepy/btle.py", line 439, in _connect raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp) bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral FD:D4:23:B6:5D:DA, addr type: public
解决建议
确认蓝牙地址准确性
用bluetoothctl scan on重新扫描传感器,核对MAC地址是否正确——传感器的蓝牙地址可能因重启或配对状态变化而改变,务必保证代码中的地址和扫描结果完全一致。检查蓝牙权限与运行状态
- 确保树莓派蓝牙服务已启动:执行
sudo systemctl start bluetooth,或通过bluetoothctl输入power on开启蓝牙; - 以root权限运行代码:bluepy库需要系统级蓝牙访问权限,尝试用
sudo python3 Wit_ble.py执行脚本; - 确认传感器未被其他设备占用:该IMU传感器通常仅支持单设备连接,断开其他可能连接的设备后重试。
- 确保树莓派蓝牙服务已启动:执行
指定蓝牙地址类型
错误提示显示使用的是public地址类型,但部分BLE设备采用随机地址模式。修改连接代码,显式指定地址类型:p = Peripheral("FD:D4:23:B6:5D:DA", addrType="random").withDelegate(NotifyDelegate())更新bluepy库
旧版本bluepy可能存在兼容性问题,执行sudo pip3 install --upgrade bluepy升级到最新版本后重试。排查硬件与信号问题
- 测试树莓派蓝牙功能:尝试连接其他蓝牙设备(如手机),确认蓝牙模块工作正常;
- 缩短传感器与树莓派的距离:BLE信号受障碍物影响较大,确保两者间距在1米以内且无遮挡。
内容的提问来源于stack exchange,提问作者Dante
相关产品推荐
相关产品推荐

