Skip to content

Instantly share code, notes, and snippets.

@AndBondStyle
Last active May 2, 2025 21:44

Revisions

  1. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bluetooth-reconnect
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ targets=(
    )

    function try_target() {
    if $2
    if eval $2
    then
    echo "Target $1 (check: $2) -> check true, trying to connect";
    bluetoothctl connect "$1";
  2. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bluetooth-reconnect
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ delay=10
    # Pairs of [bluetooth MAC, test expression]
    targets=(
    "98:B6:E9:47:F0:4F" "test ! -e /dev/input/js0"
    "98:B6:E9:72:6C:31" "test -z \"$(bluetoothctl info 98:B6:E9:72:6C:31 | grep 'Connected: yes')\""
    "98:B6:E9:72:6C:31" "test -z \"\$(bluetoothctl info 98:B6:E9:72:6C:31 | grep 'Connected: yes')\""
    )

    function try_target() {
  3. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ git clone https://gist.github.com/bc178789a441ce73b51e1e0637393aac.git bluetooth
    sudo chmod +x bluetooth-reconnect
    # Edit `bluetooth-reconnect` file according to your needs. Try if it works with ./bluetooth-reconnect
    sudo cp bluetooth-reconnect /usr/bin
    sudo cp bluetooth-reconnect/bluetooth-reconnect.service /etc/systemd/system
    sudo cp bluetooth-reconnect.service /etc/systemd/system
    sudo systemctl enable bluetooth-reconnect
    sudo systemctl start bluetooth-reconnect
    # Check if it's alive
  4. AndBondStyle revised this gist Apr 12, 2023. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion bluetooth-reconnect
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ delay=10
    # Pairs of [bluetooth MAC, test expression]
    targets=(
    "98:B6:E9:47:F0:4F" "test ! -e /dev/input/js0"
    "98:B6:E9:72:6C:31" "test ! -e /dev/whatever"
    "98:B6:E9:72:6C:31" "test -z \"$(bluetoothctl info 98:B6:E9:72:6C:31 | grep 'Connected: yes')\""
    )

    function try_target() {
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    Inspired by [bluetooth-autoconnect](https://github.com/jrouleau/bluetooth-autoconnect), but much simplier and actually works. Based on systemd service (daemon) which does the following, repeatedly:

    1. Check activation condition. In my case, just check if `/dev/something` does not exist.
    1. Run check command (e.g. check if device exists). If it returns zero, continue.
    2. Try to run `bluetoothctl connect <MAC>`. No checks if bluetooth is on, etc. Dead simple.
    3. Sleep for `delay` seconds (configurable).

  5. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bluetooth-reconnect
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    # Miminum delay between iterations, in seconds
    delay=10
    # Odd index: bluetooth MAC, even index: test expression
    # Pairs of [bluetooth MAC, test expression]
    targets=(
    "98:B6:E9:47:F0:4F" "test ! -e /dev/input/js0"
    "98:B6:E9:72:6C:31" "test ! -e /dev/whatever"
  6. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 0 additions and 17 deletions.
    17 changes: 0 additions & 17 deletions bluetooth-reconnect
    Original file line number Diff line number Diff line change
    @@ -33,20 +33,3 @@ do
    sleep $loop_delay
    last_time=$(date +%s)
    done

    function try_target() {
    if $2
    then
    echo "Target $1 (check: $2) -> check true, trying to connect...";
    bluetoothctl connect "$1";
    else
    echo "Target $1 (check: $2) -> check false, skipping"
    fi
    }

    # seq args: <start> <step> <end>
    for i in $(seq 1 2 ${#targets[@]});
    do try_target "${targets[i-1]}" "${targets[i]}"
    done

    echo "All targets processed"
  7. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bluetooth-reconnect
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,8 @@
    delay=10
    # Odd index: bluetooth MAC, even index: test expression
    targets=(
    "98:B6:E9:47:F0:4F" "test ! -f /dev/input/js0"
    "98:B6:E9:72:6C:31" "test ! -f /dev/whatever"
    "98:B6:E9:47:F0:4F" "test ! -e /dev/input/js0"
    "98:B6:E9:72:6C:31" "test ! -e /dev/whatever"
    )

    function try_target() {
  8. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # bluetooth-reconnect

    Inspired by [bluetooth-autoconnect]([url](https://github.com/jrouleau/bluetooth-autoconnect)), but much simplier and actually works. Based on systemd service (daemon) which does the following, repeatedly:
    Inspired by [bluetooth-autoconnect](https://github.com/jrouleau/bluetooth-autoconnect), but much simplier and actually works. Based on systemd service (daemon) which does the following, repeatedly:

    1. Check activation condition. In my case, just check if `/dev/something` does not exist.
    2. Try to run `bluetoothctl connect <MAC>`. No checks if bluetooth is on, etc. Dead simple.
  9. AndBondStyle revised this gist Apr 12, 2023. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,15 @@ Inspired by [bluetooth-autoconnect]([url](https://github.com/jrouleau/bluetooth-
    ## How to install

    ```
    // TODO
    git clone https://gist.github.com/bc178789a441ce73b51e1e0637393aac.git bluetooth-reconnect && cd bluetooth-reconnect
    sudo chmod +x bluetooth-reconnect
    # Edit `bluetooth-reconnect` file according to your needs. Try if it works with ./bluetooth-reconnect
    sudo cp bluetooth-reconnect /usr/bin
    sudo cp bluetooth-reconnect/bluetooth-reconnect.service /etc/systemd/system
    sudo systemctl enable bluetooth-reconnect
    sudo systemctl start bluetooth-reconnect
    # Check if it's alive
    sudo systemctl status bluetooth-reconnect
    ```

    ## References
  10. AndBondStyle created this gist Apr 12, 2023.
    52 changes: 52 additions & 0 deletions bluetooth-reconnect
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/bin/bash

    # Miminum delay between iterations, in seconds
    delay=10
    # Odd index: bluetooth MAC, even index: test expression
    targets=(
    "98:B6:E9:47:F0:4F" "test ! -f /dev/input/js0"
    "98:B6:E9:72:6C:31" "test ! -f /dev/whatever"
    )

    function try_target() {
    if $2
    then
    echo "Target $1 (check: $2) -> check true, trying to connect";
    bluetoothctl connect "$1";
    else
    echo "Target $1 (check: $2) -> check false, skipping"
    fi
    }

    last_time=$(date +%s)
    while true
    do
    # seq args: <start> <step> <end>
    for i in $(seq 1 2 ${#targets[@]});
    do try_target "${targets[i-1]}" "${targets[i]}"
    done

    curr_time=$(date +%s)
    elapsed=$((curr_time - last_time))
    loop_delay=$((elapsed>delay ? 0 : (delay-elapsed)))
    echo "Loop finished, sleeping for $loop_delay sec"
    sleep $loop_delay
    last_time=$(date +%s)
    done

    function try_target() {
    if $2
    then
    echo "Target $1 (check: $2) -> check true, trying to connect...";
    bluetoothctl connect "$1";
    else
    echo "Target $1 (check: $2) -> check false, skipping"
    fi
    }

    # seq args: <start> <step> <end>
    for i in $(seq 1 2 ${#targets[@]});
    do try_target "${targets[i-1]}" "${targets[i]}"
    done

    echo "All targets processed"
    9 changes: 9 additions & 0 deletions bluetooth-reconnect.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    [Unit]
    Description=Bluetooth reconnect service
    Before=bluetooth.service

    [Service]
    ExecStart=/usr/bin/bluetooth-reconnect

    [Install]
    WantedBy=bluetooth.service
    18 changes: 18 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    # bluetooth-reconnect

    Inspired by [bluetooth-autoconnect]([url](https://github.com/jrouleau/bluetooth-autoconnect)), but much simplier and actually works. Based on systemd service (daemon) which does the following, repeatedly:

    1. Check activation condition. In my case, just check if `/dev/something` does not exist.
    2. Try to run `bluetoothctl connect <MAC>`. No checks if bluetooth is on, etc. Dead simple.
    3. Sleep for `delay` seconds (configurable).

    ## How to install

    ```
    // TODO
    ```

    ## References

    - How `test` command works: https://www.computerhope.com/unix/test.htm
    - How to loop with equal invervals: https://unix.stackexchange.com/a/214660