Last active
April 10, 2025 17:45
-
Star
(115)
You must be signed in to star a gist -
Fork
(11)
You must be signed in to fork a gist
-
-
Save egelev/2e6b57d5a8ba62cf6df6fff2878c3fd4 to your computer and use it in GitHub Desktop.
Connect bluetooth headphones on Ubuntu 18.04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function get_headphones_index() { | |
echo $(pacmd list-cards | grep bluez_card -B1 | grep index | awk '{print $2}') | |
} | |
function get_headphones_mac_address() { | |
local temp=$(pacmd list-cards | grep bluez_card -C20 | grep 'device.string' | cut -d' ' -f 3) | |
temp="${temp%\"}" | |
temp="${temp#\"}" | |
echo "${temp}" | |
} | |
function _control_bluethoot_headphones() { | |
local op=${1} | |
local hp_mac=${2} | |
echo -e "${op} ${hp_mac}\n quit" | bluetoothctl | |
} | |
function disconnect_bluetooth_headphones() { | |
local hp_mac=${1} | |
_control_bluethoot_headphones "disconnect" ${hp_mac} | |
} | |
function connect_bluetooth_headphones() { | |
local hp_mac=${1} | |
_control_bluethoot_headphones "connect" ${hp_mac} | |
} | |
function _set_headphones_profile() { | |
local profile=${1} | |
pacmd set-card-profile $(get_headphones_index) ${profile} | |
} | |
function set_headphones_profile_a2dp_sink() { | |
_set_headphones_profile "a2dp_sink" | |
echo "Bluethoot headphones a2dp_sink" | |
} | |
function set_headphones_profile_off() { | |
_set_headphones_profile "off" | |
echo "Bluethoot headphones profile off" | |
} | |
function main() { | |
local hp_mac=$(get_headphones_mac_address) | |
set_headphones_profile_off | |
sleep 2s | |
disconnect_bluetooth_headphones ${hp_mac} | |
sleep 6s | |
connect_bluetooth_headphones ${hp_mac} | |
sleep 3s | |
set_headphones_profile_a2dp_sink | |
} | |
main |
Hi Emil. I happened to find your script just today ... and I love it!, very useful. Thank you! ( :
I'm glad it is still useful :).
Thank you very much for this - worked perfectly out of the box with my Bose QCII Headset on Ubuntu 22.04.
Thanks, worked for me!
Thank you @egelev for the script. I changed it to make it wotk with PipeWire. You can see it here: https://gist.github.com/tbulligan/b92e85a6e89e218d711a97cfa38f855e
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Emil. I happened to find your script just today ... and I love it!, very useful. Thank you! ( :