[SOLVED] A command to enable Bluetooth Connection?

Every time I wake up PC from sleep I have to go to bluetooth settings -> select device -> enable connection to get sound on bluetooth speakers (Anker Soundcore). Bluetooth came with MBO and drivers were working out of the box after PopOS install.

I hope there is a command I can use instead of clicking in the GUI. Anyone know a command I could use?

[SOLUTION]

Using this command (with bluetooth speaker MAC address):

bluetoothctl connect A4:77:58:0A:DF:F1

[SOLUTION]

Bonus question: I was thinking I could map that command to a keyboard shortcut (like CTRL+ALT+B). What is the best way (or app) to accomplish this? I believe I could google this part quickly, but happy to hear suggestions anyway

[SOLUTION]

It’s possible with PopOS: Settings -> Keyboard -> Keaboard Shortcuts -> Custom Shortcuts

[SOLUTION]

<3

juja,

Please don’t crucify me but is there a way to do this on windows ? I just have a handheld gaming console that came with windows and it’s setup just the way I want but would be nice if I can also connect to Bluetooth using command line so that I can map it to playnite and have it automatically connect to my headphones when launching a game. I did a lot of research and tried some stuff like “Bluetooth command line tools” which worked a bit but somehow caused a lot of instability and caused crashes so had to stop using it.

Para_lyzed,

Probably, but this is not the place to ask if you want answers. This forum is for Linux discussion, not Windows, and while I could get this set up in Linux for you, I wouldn’t even know where to start with Windows, as I haven’t used it in a decade. You’ll see a lot of the same with experienced Linux users here. Most of us will not be able to help you. I recommend you ask in a Windows forum instead, as you’ll have a much greater chance of finding someone knowledgeable to help. Maybe there’s a forum for Windows command line (or Powershell? I don’t know what they’re calling it these days).

juja,

Understood. Will do that. Thanks.

boredsquirrel,

I just recently did something like that

Checkout my github repo under "desktop entries

It should be systemctl start bluetooth

rambos,

Thx! I could enable/disable bluetooth with your commands and that didn’t solve my problem, but helped me google the right command. It is actually much simpler that I thought, I just had to find speakers MAC and I can use:

bluetoothctl connect A1:11:22:3A:CD:F1

Now working on mapping that to a key, cheers!

Para_lyzed, (edited )

You could also run the command automatically every time your screen is unlocked depending on your DE. For instance, if you use GNOME, this will likely work

EDIT: see comment below for better solution

rambos,

Thank you! I use GNOME, but this is kinda confusing tbh. I was also looking at this forum post where I should put a script in /lib/systemd/system-sleep/. I should play around with that, but can’t afford breaking system right now xD. Will try it soon, I might install separate OS just for testing

Para_lyzed, (edited )

EDIT: Previous answer was distro dependent and had a syntax error. I have replaced it with the following instructions.

Create 2 files bluetooth-reconnect.sh and bluetooth-reconnect.service. Using your favorite text editor, edit these files so they will contain the following:

bluetooth-reconnect.sh


<span style="color:#323232;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;">gdbus monitor -y -d org.freedesktop.login1 |
</span><span style="color:#323232;">  (while read x; do
</span><span style="color:#323232;">    if echo "$x" | grep -q "{'LockedHint': <false>}"; then
</span><span style="color:#323232;">      bluetoothctl connect A1:11:22:3A:CD:F1
</span><span style="color:#323232;">    fi
</span><span style="color:#323232;">  done)
</span>

bluetooth-reconnect.service


<span style="color:#323232;">[Unit]
</span><span style="color:#323232;">Description=Reconnect Bluetooth after waking from sleep
</span><span style="color:#323232;">After=default.target
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Service]
</span><span style="color:#323232;">Type=simple
</span><span style="color:#323232;">ExecStart=/usr/local/bin/bluetooth-reconnect.sh
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Install]
</span><span style="color:#323232;">WantedBy=multi-user.target
</span>

Open a terminal in whatever location you created them (you can right click in your file manager and open in terminal, or use cd to navigate). Now move them to the correct locations (you will need sudo privilege for this):


<span style="color:#323232;">sudo mv bluetooth-reconnect.sh /usr/local/bin/bluetooth-reconnect.sh
</span><span style="color:#323232;">sudo mv bluetooth-reconnect.service /etc/systemd/system/bluetooth-reconnect.service
</span>

Make the script executable:


<span style="color:#323232;">sudo chmod +x /usr/local/bin/bluetooth-reconnect.sh
</span>

Enable and start the service:


<span style="color:#323232;">sudo systemctl enable bluetooth-reconnect.service
</span><span style="color:#323232;">sudo systemctl start bluetooth-reconnect.service
</span>

Check to make sure the service started correctly (the “Active:” field should say “active (running)” in green.


<span style="color:#323232;">sudo systemctl status bluetooth-reconnect.service
</span>

This should now do everything automatically. This has been tested and is working on my Fedora Workstation system (uses GNOME). This should be distro independent, unlike my previous answer (and also without the syntax error I had in my initial submission).

To uninstall:


<span style="color:#323232;">sudo systemctl stop bluetooth-reconnect.service
</span><span style="color:#323232;">sudo systemctl disable bluetooth-reconnect.service
</span><span style="color:#323232;">sudo rm /etc/systemd/system/bluetooth-reconnect.service
</span><span style="color:#323232;">sudo rm /usr/local/bin/bluetooth-reconnect.sh
</span>
rambos,

Thank you a lot mate for explaining in detail. I will deffo go that route

Para_lyzed, (edited )

While trying to test this on my system, I came across a few issues. I found a more modern and standardized way to check for login after sleep, and have updated my previous comment with a new script. I have new instructions though that are more comprehensive that you can follow.

Create 2 files bluetooth-reconnect.sh and bluetooth-reconnect.service. Using your favorite text editor, edit these files so they will contain the following:

bluetooth-reconnect.sh


<span style="color:#323232;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;">gdbus monitor -y -d org.freedesktop.login1 |
</span><span style="color:#323232;">  (while read x; do
</span><span style="color:#323232;">    if echo "$x" | grep -q "{'LockedHint': <false>}"; then
</span><span style="color:#323232;">      bluetoothctl connect A1:11:22:3A:CD:F1
</span><span style="color:#323232;">    fi
</span><span style="color:#323232;">  done)
</span>

bluetooth-reconnect.service


<span style="color:#323232;">[Unit]
</span><span style="color:#323232;">Description=Reconnect Bluetooth after waking from sleep
</span><span style="color:#323232;">After=default.target
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Service]
</span><span style="color:#323232;">Type=simple
</span><span style="color:#323232;">ExecStart=/usr/local/bin/bluetooth-reconnect.sh
</span><span style="color:#323232;">
</span><span style="color:#323232;">[Install]
</span><span style="color:#323232;">WantedBy=multi-user.target
</span>

Open a terminal in whatever location you created them (you can right click in your file manager and open in terminal, or use cd to navigate). Now move them to the correct locations (you will need sudo privilege for this):


<span style="color:#323232;">sudo mv bluetooth-reconnect.sh /usr/local/bin/bluetooth-reconnect.sh
</span><span style="color:#323232;">sudo mv bluetooth-reconnect.service /etc/systemd/system/bluetooth-reconnect.service
</span>

Make the script executable:


<span style="color:#323232;">sudo chmod +x /usr/local/bin/bluetooth-reconnect.sh
</span>

Enable and start the service:


<span style="color:#323232;">sudo systemctl enable bluetooth-reconnect.service
</span><span style="color:#323232;">sudo systemctl start bluetooth-reconnect.service
</span>

Check to make sure the service started correctly (the “Active:” field should say “active (running)” in green).


<span style="color:#323232;">sudo systemctl status bluetooth-reconnect.service
</span>

This should now do everything automatically. This has been tested and is working on my Fedora Workstation system (uses GNOME). This should be distro independent, unlike my previous answer (and also without the syntax error I had in my initial submission).

To uninstall:


<span style="color:#323232;">sudo systemctl stop bluetooth-reconnect.service
</span><span style="color:#323232;">sudo systemctl disable bluetooth-reconnect.service
</span><span style="color:#323232;">sudo rm /etc/systemd/system/bluetooth-reconnect.service
</span><span style="color:#323232;">sudo rm /usr/local/bin/bluetooth-reconnect.sh
</span>
rambos,

Dude this is amazing. Working like a charm and I love linux even more because of you <3 Such a straightforward guide. Have a wonderful day my friend!

Para_lyzed,

One of the wonderful things about Linux is that if something isn’t working, you can just make it work! Also, if you ever need to make a simple service to run a script on boot, you know how now; it can be very useful. Always happy to help though, enjoy your journey through Linux!

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • osvaldo12
  • magazineikmin
  • tacticalgear
  • khanakhh
  • InstantRegret
  • Youngstown
  • everett
  • slotface
  • ngwrru68w68
  • rosin
  • Durango
  • kavyap
  • thenastyranch
  • DreamBathrooms
  • JUstTest
  • cubers
  • normalnudes
  • ethstaker
  • modclub
  • mdbf
  • tester
  • GTA5RPClips
  • cisconetworking
  • anitta
  • megavids
  • Leos
  • provamag3
  • lostlight
  • All magazines