Skip to content

Instantly share code, notes, and snippets.

Last active Jul 6, 2019
What would you like to do?
How to make bluetooth flawless in gnome shell
#!/usr/bin/env bash
# This shell script setups bluetooth to be flawless in gnome shell.
# Devices are now instantly connected when they're near the laptop.
# (It probably works as a service for random WMs too.)
# The issue is essentially that gnome-bluetooth is issuing the wrong
# dbus calls to connect the device immediately; org.bluez gets EAGAIN
# from the kernel when it processes gnome-bluetooth dbus calls (until
# it eventually succeeds, 20-30s later). However, bluetoothctl is
# flawless at it. Maybe some day gnome-bluetooth will fix this. In the
# meantime, this hack lets me have a seamless bluetooth experience.
# PS: I tried to report the bug to gnome-bluetooth but haven't figured
# out how. Apparently I need to register to 2 or 3 bug trackers? I may
# just be dense.
# To be able to run this hack, you need: bluez (which provides the
# bluetoothctl binary) and python3-gevent.
# Source:
# License: MIT (is that even needed for such a small amount of code?)
set -e
# Start with checking the requirements.
python3 -c 'import gevent' &> /dev/null || echo 'You need python3-gevent' && exit 1
which bluetoothctl &> /dev/null || echo 'You need bluez (for bluetoothctl)' && exit 1
# And now the fun begins.
cd ~
# Create a systemd user service for our hack.
cat > .config/systemd/user/fix-bluetooth.service <<EOF
Description=Fix bluetooth
# Now put the hack in the ~/bin folder.
mkdir -p bin
cat > bin/fix-bluetooth <<EOF
#!/usr/bin/env python3
import gevent.monkey
import subprocess
import time
import gevent
def _refresh(device):
while True:
subprocess.check_call(["bluetoothctl", "connect", device])
except subprocess.CalledProcessError:
if __name__ == "__main__":
refreshers = {}
while True:
out = subprocess.check_output(["bluetoothctl", "devices"])
devices = []
for line in out.splitlines():
_, device, _ = line.split(b" ", 2)
except ValueError:
for device in devices:
if device not in refreshers:
print("Taking care of device: {}".format(device))
refreshers[device] = gevent.spawn(_refresh, device)
delete = []
for device in refreshers:
if device not in devices:
print("Forgetting about device: {}".format(device))
for device in delete:
del refreshers[device]
# Don't forget this or you look stupid.
chmod +x bin/fix-bluetooth
# Finally, enable and start our systemd user service.
systemctl --user enable fix-bluetooth
systemctl --user start fix-bluetooth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment