Skip to content

Instantly share code, notes, and snippets.

@ralt
Last active July 6, 2019 22:27
Show Gist options
  • Select an option

  • Save ralt/b25095dcc1be680e581a9a53f2edcb43 to your computer and use it in GitHub Desktop.

Select an option

Save ralt/b25095dcc1be680e581a9a53f2edcb43 to your computer and use it in GitHub Desktop.

Revisions

  1. ralt revised this gist Jul 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -52,7 +52,7 @@ gevent.monkey.patch_all()
    import subprocess
    import time
    import gevent.pool
    import gevent
    def _refresh(device):
  2. ralt revised this gist Jul 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@
    # out how. Apparently I need to register to 2 or 3 bug trackers? I may
    # just be dense.

    # To be able to run this fix, you need: bluez (which provides the
    # To be able to run this hack, you need: bluez (which provides the
    # bluetoothctl binary) and python3-gevent.

    # Source: https://gist.github.com/ralt/b25095dcc1be680e581a9a53f2edcb43
  3. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -23,8 +23,8 @@
    set -e

    # Start with checking the requirements.
    python3 -c 'import gevent'
    which bluetoothctl
    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 ~
  4. ralt revised this gist Jul 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@

    set -e

    # Start with checking the requirements:
    # Start with checking the requirements.
    python3 -c 'import gevent'
    which bluetoothctl

  5. ralt revised this gist Jul 6, 2019. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -26,8 +26,10 @@ set -e
    python3 -c 'import gevent'
    which bluetoothctl

    # And now the fun begins.
    cd ~

    # Create a systemd user service for our hack.
    cat > .config/systemd/user/fix-bluetooth.service <<EOF
    [Unit]
    Description=Fix bluetooth
    @@ -39,6 +41,7 @@ ExecStart=/home/$USER/bin/fix-bluetooth
    WantedBy=default.target
    EOF

    # Now put the hack in the ~/bin folder.
    mkdir -p bin
    cat > bin/fix-bluetooth <<EOF
    #!/usr/bin/env python3
    @@ -94,7 +97,9 @@ if __name__ == "__main__":
    time.sleep(60)
    EOF

    # 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
  6. ralt revised this gist Jul 6, 2019. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,10 @@

    set -e

    # Start with checking the requirements:
    python3 -c 'import gevent'
    which bluetoothctl

    cd ~

    cat > .config/systemd/user/fix-bluetooth.service <<EOF
  7. ralt revised this gist Jul 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@
    # bluetoothctl binary) and python3-gevent.

    # Source: https://gist.github.com/ralt/b25095dcc1be680e581a9a53f2edcb43
    # License: WTFPL (is that even needed for such a small amount of code?)
    # License: MIT (is that even needed for such a small amount of code?)

    set -e

  8. ralt revised this gist Jul 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@
    # bluetoothctl binary) and python3-gevent.

    # Source: https://gist.github.com/ralt/b25095dcc1be680e581a9a53f2edcb43
    # License: WTFPL
    # License: WTFPL (is that even needed for such a small amount of code?)

    set -e

  9. ralt revised this gist Jul 6, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,9 @@
    # To be able to run this fix, you need: bluez (which provides the
    # bluetoothctl binary) and python3-gevent.

    # Source: https://gist.github.com/ralt/b25095dcc1be680e581a9a53f2edcb43
    # License: WTFPL

    set -e

    cd ~
  10. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,8 @@

    set -e

    cd ~

    cat > .config/systemd/user/fix-bluetooth.service <<EOF
    [Unit]
    Description=Fix bluetooth
  11. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,8 @@
    # To be able to run this fix, you need: bluez (which provides the
    # bluetoothctl binary) and python3-gevent.

    set -e

    cat > .config/systemd/user/fix-bluetooth.service <<EOF
    [Unit]
    Description=Fix bluetooth
  12. ralt revised this gist Jul 6, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ ExecStart=/home/$USER/bin/fix-bluetooth
    WantedBy=default.target
    EOF

    mkdir -p bin
    cat > bin/fix-bluetooth <<EOF
    #!/usr/bin/env python3
  13. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    #!/usr/bin/env bash

    # This shell script setups bluetooth to be flawless.
    # 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
  14. ralt renamed this gist Jul 6, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.sh → install-bluetooth.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # This shell scripts setups bluetooth to be flawless.
    #!/usr/bin/env bash

    # This shell script setups bluetooth to be flawless.
    # Devices are now instantly connected when they're near the laptop.

    # The issue is essentially that gnome-bluetooth is issuing the wrong
  15. ralt revised this gist Jul 6, 2019. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # This shell session explains how to setup bluetooth to be flawless.
    # This shell scripts setups bluetooth to be flawless.
    # Devices are now instantly connected when they're near the laptop.

    # The issue is essentially that gnome-bluetooth is issuing the wrong
    @@ -14,17 +14,18 @@
    # To be able to run this fix, you need: bluez (which provides the
    # bluetoothctl binary) and python3-gevent.

    ralt@zap:~/ -> cat > .config/systemd/user/fix-bluetooth.service <<EOF
    cat > .config/systemd/user/fix-bluetooth.service <<EOF
    [Unit]
    Description=Fix bluetooth
    [Service]
    ExecStart=/home/ralt/bin/fix-bluetooth
    ExecStart=/home/$USER/bin/fix-bluetooth
    [Install]
    WantedBy=default.target
    EOF
    ralt@zap:~/ -> cat > bin/fix-bluetooth <<EOF

    cat > bin/fix-bluetooth <<EOF
    #!/usr/bin/env python3
    import gevent.monkey
    @@ -77,7 +78,8 @@ if __name__ == "__main__":
    time.sleep(60)
    EOF
    ralt@zap:~/ -> chmod +x bin/fix-bluetooth
    ralt@zap:~/ -> systemctl --user enable fix-bluetooth
    Created symlink /home/ralt/.config/systemd/user/default.target.wants/fix-bluetooth.service → /home/ralt/.config/systemd/user/fix-bluetooth.service.
    ralt@zap:~/ -> systemctl --user start fix-bluetooth

    chmod +x bin/fix-bluetooth

    systemctl --user enable fix-bluetooth
    systemctl --user start fix-bluetooth
  16. ralt revised this gist Jul 6, 2019. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@
    # To be able to run this fix, you need: bluez (which provides the
    # bluetoothctl binary) and python3-gevent.

    ralt@zap:~/ -> cat .config/systemd/user/fix-bluetooth.service
    ralt@zap:~/ -> cat > .config/systemd/user/fix-bluetooth.service <<EOF
    [Unit]
    Description=Fix bluetooth
    @@ -23,7 +23,8 @@ ExecStart=/home/ralt/bin/fix-bluetooth
    [Install]
    WantedBy=default.target
    ralt@zap:~/ -> cat bin/fix-bluetooth
    EOF
    ralt@zap:~/ -> cat > bin/fix-bluetooth <<EOF
    #!/usr/bin/env python3
    import gevent.monkey
    @@ -75,6 +76,8 @@ if __name__ == "__main__":
    del refreshers[device]
    time.sleep(60)
    ralt@zap:~/.config/systemd/user/ -> systemctl --user enable fix-bluetooth
    EOF
    ralt@zap:~/ -> chmod +x bin/fix-bluetooth
    ralt@zap:~/ -> systemctl --user enable fix-bluetooth
    Created symlink /home/ralt/.config/systemd/user/default.target.wants/fix-bluetooth.service → /home/ralt/.config/systemd/user/fix-bluetooth.service.
    ralt@zap:~/.config/systemd/user/ -> systemctl --user start fix-bluetooth
    ralt@zap:~/ -> systemctl --user start fix-bluetooth
  17. ralt revised this gist Jul 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    # 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
    # be just dense.
    # just be dense.

    # To be able to run this fix, you need: bluez (which provides the
    # bluetoothctl binary) and python3-gevent.
  18. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,8 @@
    # out how. Apparently I need to register to 2 or 3 bug trackers? I may
    # be just dense.

    # To be able to run this fix, you need: bluetoothctl and python3-gevent.
    # To be able to run this fix, you need: bluez (which provides the
    # bluetoothctl binary) and python3-gevent.

    ralt@zap:~/ -> cat .config/systemd/user/fix-bluetooth.service
    [Unit]
  19. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,8 @@
    # out how. Apparently I need to register to 2 or 3 bug trackers? I may
    # be just dense.

    # To be able to run this fix, you need: bluetoothctl and python3-gevent.

    ralt@zap:~/ -> cat .config/systemd/user/fix-bluetooth.service
    [Unit]
    Description=Fix bluetooth
  20. ralt revised this gist Jul 6, 2019. No changes.
  21. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # This shell session explains how to setup bluetooth to be flawless.
    # Devices are immediately connecting when they're near the laptop.
    # Devices are now instantly connected when they're near the laptop.

    # 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
  22. ralt revised this gist Jul 6, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,10 @@
    # Devices are immediately connecting when they're near the laptop.
    # 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. 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.
    # 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
    # be just dense.
  23. ralt renamed this gist Jul 6, 2019. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions gistfile1.txt → gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,14 @@
    # This shell session explains how to setup bluetooth to be flawless.
    # Devices are immediately connecting when they're near the laptop.
    # 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. 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 be just dense.
    # 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. 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
    # be just dense.

    ralt@zap:~/ -> cat .config/systemd/user/fix-bluetooth.service
    [Unit]
  24. ralt revised this gist Jul 6, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # This shell session explains how to setup bluetooth to be flawless.
    # Devices are immediately connecting when they're near the laptop.
    # 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. 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 be just dense.

    ralt@zap:~/ -> cat .config/systemd/user/fix-bluetooth.service
    [Unit]
  25. ralt created this gist Jul 6, 2019.
    67 changes: 67 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    # This shell session explains how to setup bluetooth to be flawless.
    # Devices are immediately connecting when they're near the laptop.

    ralt@zap:~/ -> cat .config/systemd/user/fix-bluetooth.service
    [Unit]
    Description=Fix bluetooth

    [Service]
    ExecStart=/home/ralt/bin/fix-bluetooth

    [Install]
    WantedBy=default.target
    ralt@zap:~/ -> cat bin/fix-bluetooth
    #!/usr/bin/env python3

    import gevent.monkey
    gevent.monkey.patch_all()

    import subprocess
    import time

    import gevent.pool


    def _refresh(device):
    while True:
    try:
    subprocess.check_call(["bluetoothctl", "connect", device])
    except subprocess.CalledProcessError:
    time.sleep(1)
    else:
    time.sleep(10)

    if __name__ == "__main__":
    refreshers = {}

    while True:
    out = subprocess.check_output(["bluetoothctl", "devices"])

    devices = []
    for line in out.splitlines():
    try:
    _, device, _ = line.split(b" ", 2)
    except ValueError:
    continue

    devices.append(device)

    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))
    refreshers[device].kill()
    delete.append(device)

    for device in delete:
    del refreshers[device]

    time.sleep(60)
    ralt@zap:~/.config/systemd/user/ -> systemctl --user enable fix-bluetooth
    Created symlink /home/ralt/.config/systemd/user/default.target.wants/fix-bluetooth.service → /home/ralt/.config/systemd/user/fix-bluetooth.service.
    ralt@zap:~/.config/systemd/user/ -> systemctl --user start fix-bluetooth