BlueZ Bonded失败且未生成input event
结论
Bonded: no 和缺失 /dev/input/eventX 之间没有直接因果关系。Bonded 是 BlueZ 的持久链路密钥属性;/dev/input/eventX 由内核 HID 层在 HID profile(经典蓝牙 HID 或 BLE HOG)建立并注册 input 设备后创建。只调用 Pair、设置 Trusted、再调用 Connect,并不能保证 HID profile 已经连接。
先判断问题出在哪一层
- 手动用
bluetoothctl复现。如果手动也生成不了 event,优先查设备模式、BlueZ 和内核;如果能生成,再查 C 的 D-Bus 调用。
bluetoothctl
power on
agent on
default-agent
scan on
pair XX:XX:XX:XX:XX:XX
trust XX:XX:XX:XX:XX:XX
connect XX:XX:XX:XX:XX:XX
info XX:XX:XX:XX:XX:XX
重点看 Bonded、Connected 和 UUIDs。如果 UUIDs 中没有 HID 相关 UUID,说明当前模式或连接没有暴露 HID 服务。经典 HID 常见 UUID 是 00001124-0000-1000-8000-00805f9b34fb;BLE HOG 常见 UUID 是 00001812-0000-1000-8000-00805f9b34fb。具体以 bluetoothctl info 实际列出的内容为准。
- 检查 HID profile 是否连接。可以在
Connect后显式调用ConnectProfile:
busctl call org.bluez /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX org.bluez.Device1 ConnectProfile s "00001124-0000-1000-8000-00805f9b34fb"
若设备是 BLE HOG,则把 UUID 换成 00001812-0000-1000-8000-00805f9b34fb。如果返回 Not Supported / Not Available,通常说明该 profile 不存在,或者设备没有进入对应模式。
- 看内核和 BlueZ 日志:
dmesg -w
journalctl -u bluetooth -f
grep -A6 -i "8BitDo" /proc/bus/input/devices
ls -l /dev/input/by-id/
evtest
正常时应看到类似 H: Handlers=eventX jsY,并出现 /dev/input/eventX。如果 dmesg 完全没有 hid/input 注册日志,说明 HID 通道没有建立,读取 /proc 的代码没有问题。
Bonded 为 no 的常见原因
- 配对没有真正持久化:BlueZ 需要注册
org.bluez.Agent1并处理RequestConfirmation、RequestPinCode、RequestPasskey、AuthorizeService等回调。只调用Pair不一定形成 bond。 Trusted=yes只是授权,不等于完成配对或绑定。- 8BitDo Pro 2 的模式或固件状态不对。确认它处于 Raspberry Pi 可识别的蓝牙手柄模式,而不是其他模式或仅临时连接。
- 旧配对信息残留。可以先
remove XX:...,再重启蓝牙或设备,然后重新pair、trust、connect。 - HID profile 未连接。ACL/Connected 成功,不代表 input 设备已经注册。
C 代码侧建议
在调用 Pair 前注册 Agent 并处理配对请求;在 Connect 成功后检查 UUIDs,必要时调用 ConnectProfile。D-Bus 调用片段示意:
const char *hid_uuid = "00001124-0000-1000-8000-00805f9b34fb";
DBusMessage *m = dbus_message_new_method_call("org.bluez", device_path,
"org.bluez.Device1", "ConnectProfile");
dbus_message_append_args(m, DBUS_TYPE_STRING, &hid_uuid, DBUS_TYPE_INVALID);
读取 event 节点不要只依赖 Bonded,也不要在失败时无休止地轮询。可以每次重新打开并关闭 /proc/bus/input/devices,先匹配 N: Name=,再在后续 H: Handlers= 中查找 event。示例:
static int find_event_for_name(const char *name, char *out, size_t outlen) {
FILE *f = fopen("/proc/bus/input/devices", "r");
char line[512];
int matched = 0;
if (!f) return -1;
while (fgets(line, sizeof(line), f)) {
if (line[0] == 'N' && strstr(line, name)) matched = 1;
else if (matched && line[0] == 'H') {
char *p = strstr(line, "event");
if (p) {
int n;
if (sscanf(p, "event%d", &n) == 1) {
snprintf(out, outlen, "/dev/input/event%d", n);
fclose(f);
return 0;
}
}
} else if (line[0] == '\n') {
matched = 0;
}
}
fclose(f);
return -1;
}
更稳妥的方式是使用 udev,或监听 /dev/input/by-id/ 下的稳定符号链接,而不是每次拼 /dev/input/event%d。
注意事项
- 先用手动
bluetoothctl验证,能快速区分是 C 代码问题还是设备/内核问题。 - 不要用
Bonded作为 event 节点是否存在的唯一判据。 - 确认 8BitDo Pro 2 当前模式和 UUID;不同模式可能使用不同 HID/BLE 路径。
- 如果手动
bluetoothctl也始终没有/dev/input/eventX,应检查设备模式、固件、BlueZ 版本与内核 HID 支持,而不是继续修改/proc解析代码。
备注:内容仅供参考。
