Ubuntu 22.04下cwiid库连接Wiimote在新Thinkpad上异常排查
解决cwiid库在新Thinkpad L14 Gen4上的蓝牙连接问题
1. 绕过系统蓝牙服务的设备占用
系统配对后,bluetoothd会抢占Wiimote的HID通道,导致cwiid无法访问,可通过以下方式处理:
- 临时停止蓝牙服务测试:
sudo systemctl stop bluetooth,再运行你的程序验证是否正常连接。 - 修改
bluetoothd配置避免抢占:编辑/etc/bluetooth/main.conf,添加或修改:
重启服务:DisablePlugins = hidsudo systemctl restart bluetooth,系统将不再把Wiimote识别为HID摇杆,cwiid可正常访问。
2. 适配内核蓝牙栈变化,指定旧API行为
新内核可能调整了HCI接口默认逻辑,可通过以下方式强制兼容旧API:
- 明确使用HCI原始套接字:在程序初始化时确保套接字创建指定
BTPROTO_HCI,避免权限拦截:int sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); if (sock < 0) { perror("Failed to create HCI socket"); exit(1); } - 调整
hci_inquiry参数:修改扫描超时、强制刷新缓存,解决首次检测不到设备的问题:inquiry_info *ii = NULL; int max_rsp = 20; int len = 8; long flags = IREQ_CACHE_FLUSH; int num_rsp = hci_inquiry(hci_devid(NULL), len, max_rsp, NULL, &ii, flags); - 加载蓝牙模块时指定兼容参数:卸载并重新加载
btusb模块,禁用重复扫描:sudo rmmod btusb sudo modprobe btusb disable_scan_duplicates=1
3. 调整权限确保cwiid正常访问
- 给程序添加原始套接字权限:
无需root即可访问HCI接口,避免权限不足导致的扫描失败。sudo setcap cap_net_raw+ep /path/to/your/program - 简化蓝牙安全验证:编辑
/etc/bluetooth/main.conf,设置:
重启服务后,配对验证逻辑简化,可能解决连接后卡在SecurityManager = falseread()的问题。
4. 调试read()阻塞问题
若仍卡在read(),用strace跟踪系统调用定位问题:
strace -f ./your-program
给套接字添加超时设置,避免无限阻塞:
struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
内容的提问来源于stack exchange,提问作者Pd-L2Ork
相关产品推荐
相关产品推荐

