Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Connect bluetooth headphones on Arch Linux (Gnome settings can't do it)
#!/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_bluetooth_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_bluetooth_headphones "disconnect" ${hp_mac}
}
function connect_bluetooth_headphones() {
local hp_mac=${1}
_control_bluetooth_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 "Bluetooth headphones a2dp_sink"
}
function set_headphones_profile_off() {
_set_headphones_profile "off"
echo "Bluetooth headphones profile off"
}
function main() {
local hp_mac=$(get_headphones_mac_address)
set_headphones_profile_off
sleep 5s
disconnect_bluetooth_headphones ${hp_mac}
sleep 5s
connect_bluetooth_headphones ${hp_mac}
sleep 10s
set_headphones_profile_a2dp_sink
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment