2

I am having the most bizarre bluetooth problem in Ubuntu 24.04. I have several bluetooth devices (logitech k585 BT keyboard, Airpods pro 2, generic BT mouse) paired/connected to the computer. After waking from sleep/suspend or after a reboot, the mouse and airpods reconnect to BT just fine however the keyboard will not reconnect unless I turn bluetooth off then on again in the system menu.

I have tried playing around with various settings in /etc/bluetooth/main.conf and it appears that setting:

ControllerMode = le

Will enable the keyboard to properly reconnect after sleep/suspend and on boot, but then the airpods will no longer connect or pair with the computer. If I set ControllerMode = bredr, the airpods work fine but the keyboard will no longer connect at all (let alone after wake or reboot). The only setting that seems to work for all devices is ControllerMode = dual, and in this case I have the undesired behavior that the keyboard will not reconnect on wake or reboot unless the bluetooth adapter is turned off and on again.

I have also tried setting MultiProfile = multiple and FastConnectable = true to no avail.

For reference the driver I'm using is bluez/noble,now 5.72-0ubuntu5 amd64 and the kernel is:

bender:~> uname -a
Linux bender 6.8.0-49-generic #49-Ubuntu SMP PREEMPT_DYNAMIC Mon Nov  4 02:06:24 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Any help is appreciated as it's not really feasible to turn BT off/on after wake as my mouse is BT as well, meaning I have to ssh in from another computer to re-enable.

The Only thing I've noticed in dmesg is some cryptic opcode failures:

bender:~> sudo dmesg | grep -i blue
[    3.617461] Bluetooth: Core ver 2.22
[    3.617494] NET: Registered PF_BLUETOOTH protocol family
[    3.617495] Bluetooth: HCI device and connection manager initialized
[    3.617500] Bluetooth: HCI socket layer initialized
[    3.617502] Bluetooth: L2CAP socket layer initialized
[    3.617507] Bluetooth: SCO socket layer initialized
[    3.678221] Bluetooth: hci0: Legacy ROM 2.x revision 5.0 build 25 week 20 2015
[    3.679172] Bluetooth: hci0: Intel Bluetooth firmware file: intel/ibt-hw-37.8.10-fw-22.50.19.14.f.bseq
[    4.068237] Bluetooth: hci0: Intel BT fw patch 0x43 completed & activated
[    4.755238] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[    4.755243] Bluetooth: BNEP filters: protocol multicast
[    4.755248] Bluetooth: BNEP socket layer initialized
[    4.761546] Bluetooth: MGMT ver 1.22
[    4.856327] Bluetooth: hci0: Bad flag given (0x1) vs supported (0x0)
[    4.856373] Bluetooth: hci0: Bad flag given (0x1) vs supported (0x0)
[    7.018615] Bluetooth: RFCOMM TTY layer initialized
[    7.018632] Bluetooth: RFCOMM socket layer initialized
[    7.018641] Bluetooth: RFCOMM ver 1.11
[   59.704537] Bluetooth: hci0: Opcode 0x2005 failed: -22
[   59.705031] Bluetooth: hci0: Opcode 0x200b failed: -22
[   63.288518] hid-generic 0005:046D:B35A.0003: input,hidraw2: BLUETOOTH HID v0.11 Keyboard [Logi K580/K585] on dc:46:28:46:68:b9

Happy to provide any other info/output that would be helpful.

UPDATE:

It appears the problem is worse than I thought. The keyboard is not able to reconnect any time after the screen locks once it disconnects. I had mistakenly thought the issue was related to sleep/suspend but it's actually merely screen lock.

Per the link provided by Hannu, running:

rfkill block bluetooth
rfkill unblock bluetooth

seems to be the only way to get the keyboard to connect again.

Nicpac
  • 31

2 Answers2

1

Thanks to @Hannu for getting me down the right path with rfkill, but figured I'd write up the full solution. Full disclosure: it's absolutely horrifying to have to do this, but the only way I found to solve this problem is to run a script in the background that monitors for screen lock events and calls rfkill block bluetooth followed by rfkill unblock bluetooth.

Using info from the following links I managed to cobble together a working solution:

https://superuser.com/a/1768878/346288 (thanks @Hannu)

How to run a script at screen lock / unlocks in ubuntu 17.10

https://forum.manjaro.org/t/run-script-after-login-after-the-lock-screen-after-wakeup/25993

Solution:

  1. Create a bash script (/use/local/bin/bt_toggle) to monitor for screen lock events, and call rfkill block/unblock bluetooth (ensure script is executable):
#!/bin/bash

run once on bootup for initial connection reset

rfkill block bluetooth rfkill unblock bluetooth

run every time a screen lock occurs

gdbus monitor -y -d org.freedesktop.login1 |
( while true do read X if echo $X | grep "'LockedHint': <true>" &> /dev/null; then rfkill block bluetooth rfkill unblock bluetooth fi done )

  1. Run the script on startup with systemd by creating the following job in /etc/systemd/system/bt_toggle.service:
[Unit]
Description=Toggle bluetooth off/on when screen locks
After=network.target
[Service]
ExecStart=/usr/local/bin/bt_toggle
[Install]
WantedBy=default.target

Tested on both bootup and screen lock and seems to let the keyboard reconnect to bluetooth in all cases. I really hate doing this as on screen lock it will disconnect all bluetooth devices however since the only devices I consistently have connected are mouse and keyboard, its not a huge deal. I'm hoping this behavior will be fixed in a future kernel or bluez update and I can do away with these scripts but until that time, hopefully this helps some other poor soul.

Nicpac
  • 31
0

https://superuser.com/a/1768878/346288

or even a sequence of:

off="bluetoothctl disconnect $mac";
on="bluetoothctl connect $mac";
echo $off;
$off;
echo "sleep 2";
sleep 2;
echo $on;
$on

If either of those gets it running you might wish to arrange a script file to be run on a mouse click.

Hannu
  • 6,605
  • 1
  • 28
  • 45