Fix Bluetooth HID connections in dual-mode BlueZ
ControllerMode=bredr made BlueZ 5.87 reject the LE-only HOGP profile, so discovered BLE keyboards and mice never created input devices. Use dual mode, power/LE preflight, LE bearer selection for HID-over-GATT devices, automatic pairing on Connect Device and a pkill-free daemon stop path. Add scripts/test-bluetooth-hid.py and check-rebuild guards, synchronize docs, and rebuild the 2720 image (09226459...).
This commit is contained in:
1 parent
ba2f25aa36
commit
20603893e7
9 files changed
+225
-11
No files matched your search
@@ -3,7 +3,7 @@
|
||||
# user enables it, do not leave the controller permanently discoverable.
|
||||
DiscoverableTimeout = 60
|
||||
PairableTimeout = 60
|
||||
ControllerMode = bredr
|
||||
ControllerMode = dual
|
||||
MultiProfile = multiple
|
||||
JustWorksRepairing = always
|
||||
Experimental = true
|
||||
|
||||
@@ -547,6 +547,26 @@ def trust_device_path(conn, dev_path):
|
||||
pass
|
||||
|
||||
|
||||
def set_hog_preferred_bearer(conn, dev_path, props=None):
|
||||
"""Force the LE bearer for HID-over-GATT devices before Pair/Connect."""
|
||||
if not isinstance(dev_path, str) or not dev_path.startswith("/org/bluez/"):
|
||||
return
|
||||
uuids = []
|
||||
if isinstance(props, dict):
|
||||
uuids = [str(v).lower() for v in (props.get("UUIDs") or [])]
|
||||
if not any(u.startswith("00001812-") for u in uuids):
|
||||
return
|
||||
try:
|
||||
from jeepney import DBusAddress, new_method_call
|
||||
prop_addr = DBusAddress(dev_path, bus_name="org.bluez",
|
||||
interface="org.freedesktop.DBus.Properties")
|
||||
conn.send(new_method_call(prop_addr, "Set", "ssv",
|
||||
("org.bluez.Device1", "PreferredBearer",
|
||||
("s", "le"))))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def handle_agent_method_call(conn, msg, agent_path):
|
||||
from jeepney import new_method_return, MessageType
|
||||
if msg is None or msg.header.message_type != MessageType.method_call:
|
||||
@@ -1006,6 +1026,26 @@ def ensure_daemon_running():
|
||||
pass
|
||||
|
||||
|
||||
def prepare_bt_controller():
|
||||
"""Power the adapter and enable LE before DBus Pair/Connect calls."""
|
||||
try:
|
||||
subprocess.run(["timeout", "3", "bluetoothctl", "power", "on"],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
timeout=4)
|
||||
except Exception:
|
||||
pass
|
||||
helper = "/usr/bin/bananarchy-bt-le-enable.sh"
|
||||
try:
|
||||
if os.geteuid() == 0:
|
||||
subprocess.run([helper], stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL, timeout=4)
|
||||
else:
|
||||
subprocess.run(["doas", "-n", helper], stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL, timeout=4)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def clear_stale_linkkey(mac):
|
||||
mac = mac.strip().upper()
|
||||
if not mac:
|
||||
@@ -1057,6 +1097,7 @@ def connect_or_pair_bt_device(mac, dev_name="", do_pair=False):
|
||||
from jeepney.io.blocking import open_dbus_connection
|
||||
|
||||
ensure_daemon_running()
|
||||
prepare_bt_controller()
|
||||
mac = mac.strip().upper()
|
||||
if not mac:
|
||||
return 1
|
||||
@@ -1189,8 +1230,9 @@ def connect_or_pair_bt_device(mac, dev_name="", do_pair=False):
|
||||
)
|
||||
|
||||
def attempt_connect_path(dpath, want_pair):
|
||||
trust_device_path(nonlocal_conn[0], dpath)
|
||||
p = get_dev_props(dpath)
|
||||
set_hog_preferred_bearer(nonlocal_conn[0], dpath, p)
|
||||
trust_device_path(nonlocal_conn[0], dpath)
|
||||
if p is None:
|
||||
return False
|
||||
if p.get("Connected"):
|
||||
@@ -1293,6 +1335,8 @@ def connect_or_pair_bt_device(mac, dev_name="", do_pair=False):
|
||||
|
||||
props = get_dev_props(dev_path)
|
||||
orig_unbonded = (props is None) or (not props.get("Paired") and not props.get("Bonded"))
|
||||
if not do_pair and orig_unbonded and (props is not None or dev_name):
|
||||
do_pair = True
|
||||
if props and not dev_name:
|
||||
dev_name = str(props.get("Name") or props.get("Alias") or "").strip()
|
||||
if props is None and dev_name:
|
||||
@@ -1344,10 +1388,12 @@ def connect_or_pair_bt_device(mac, dev_name="", do_pair=False):
|
||||
|
||||
|
||||
def stop_daemon():
|
||||
pids = set()
|
||||
if os.path.exists(PID_FILE):
|
||||
try:
|
||||
with open(PID_FILE, "r", encoding="utf-8") as f:
|
||||
pid = int(f.read().strip())
|
||||
pids.add(pid)
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
except Exception:
|
||||
pass
|
||||
@@ -1355,8 +1401,26 @@ def stop_daemon():
|
||||
os.unlink(PID_FILE)
|
||||
except OSError:
|
||||
pass
|
||||
subprocess.run(["pkill", "-f", "bananarchy-bt-obex daemon"],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
# The target rootfs is BusyBox-based and does not ship an external process
|
||||
# killer, so find stale daemon command lines through /proc instead.
|
||||
try:
|
||||
for entry in os.listdir("/proc"):
|
||||
if not entry.isdigit() or int(entry) == os.getpid():
|
||||
continue
|
||||
try:
|
||||
with open("/proc/" + entry + "/cmdline", "rb") as cf:
|
||||
cmd = cf.read().replace(b"\x00", b" ").decode("utf-8", "replace")
|
||||
except OSError:
|
||||
continue
|
||||
if "bananarchy-bt-obex" in cmd and " daemon" in cmd:
|
||||
pids.add(int(entry))
|
||||
except OSError:
|
||||
pass
|
||||
for pid in pids:
|
||||
try:
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in new issue
Block a user