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?

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

  • rambos@lemm.eeOP
    link
    fedilink
    arrow-up
    0
    ·
    5 months ago

    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@lemmy.world
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      5 months ago

      Well, for your particular case, you’d make a script, and a service to run that script on boot. Once the service starts, it will keep itself alive.

      Here’s the script:

      bluetooth-reconnect.sh

      #!/bin/bash
      
      dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" |
        while read x; do
          if echo $x | grep -q "boolean false"
            bluetoothctl connect A1:11:22:3A:CD:F1
          fi
        done
      

      You’d place this script somewhere that has system execution privilege (if your distro uses SELinux). I’d recommend using mv for this instead of a file explorer. I will use the directory /usr/scripts/ for example purposes (note that you will have to create this folder, perhaps with mkdir /usr/scripts). Make sure to mark it executable with chmod +x /usr/scripts/bluetooth-reconnect.sh

      You’d then write a service to start at boot, just really barebones and simple:

      bluetooth-reconnect.service

      [Unit]
      Description=Reconnect Bluetooth after waking from sleep
      After=default.target
      
      [Service]
      Type=simple
      ExecStart=/usr/scripts/bluetooth-reconnect.sh
      
      [Install]
      WantedBy=multi-user.target
      

      Move the service (again, I recommend mv instead of file explorer) into /etc/systemd/system/ (filepath should be /etc/systemd/system/bluetooth-reconnect.service), and enable it and start it:

      sudo systemctl enable bluetooth-reconnect.service && sudo systemctl start bluetooth-reconnect.service

      And you should be good to go. At least assuming your distro doesn’t have some specific quirk, which I wouldn’t be able to help you with unless I knew what distro you run. Granted, this is my adaptation of what I saw in the linked forum and my own experience with services, I haven’t actually tested this. But even if it has an issue, this will get you 90% of the way there, and there’s a good chance it just works if the forum answers work for your distro.

      Also, the method in the forum post you linked is Ubuntu specific, and there’s a good chance that directory has changed since 2015, when it was last answered. You will not break your system by using my method. If it doesn’t work, you can view the logs using sudo systemctl status bluetooth-reconnect.service and you can stop and disable the service like so:

      sudo systemctl stop bluetooth-reconnect.service && sudo systemctl disable bluetooth-reconnect.service

      The script only really does observation, it doesn’t try to make any writes to the system except for your Bluetooth command, which would not cause system breakage. It just listens for signals on the dbus that GNOME sends when the screen is locked/unlocked. If you wanted to completely “uninstall”, you’d rm the files from their respective locations afterwards. So please don’t do a clean install to test this, that’s entirely unnecessary and far too much work for such a simple task. Even if there’s a syntax error, the service will just exit, which is no problem.

      • rambos@lemm.eeOP
        link
        fedilink
        arrow-up
        0
        ·
        5 months ago

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

        • Para_lyzed@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          5 months ago

          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

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

          bluetooth-reconnect.service

          [Unit]
          Description=Reconnect Bluetooth after waking from sleep
          After=default.target
          
          [Service]
          Type=simple
          ExecStart=/usr/local/bin/bluetooth-reconnect.sh
          
          [Install]
          WantedBy=multi-user.target
          

          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):

          sudo mv bluetooth-reconnect.sh /usr/local/bin/bluetooth-reconnect.sh
          sudo mv bluetooth-reconnect.service /etc/systemd/system/bluetooth-reconnect.service
          

          Make the script executable:

          sudo chmod +x /usr/local/bin/bluetooth-reconnect.sh
          

          Enable and start the service:

          sudo systemctl enable bluetooth-reconnect.service
          sudo systemctl start bluetooth-reconnect.service
          

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

          sudo systemctl status bluetooth-reconnect.service
          

          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:

          sudo systemctl stop bluetooth-reconnect.service
          sudo systemctl disable bluetooth-reconnect.service
          sudo rm /etc/systemd/system/bluetooth-reconnect.service
          sudo rm /usr/local/bin/bluetooth-reconnect.sh
          
          • rambos@lemm.eeOP
            link
            fedilink
            arrow-up
            0
            ·
            5 months ago

            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!