Skip to content

Instantly share code, notes, and snippets.

@tmonjalo
Last active November 19, 2023 23:23
Show Gist options
  • Save tmonjalo/08b8700c1504b94e49c6e110c9428d81 to your computer and use it in GitHub Desktop.
Save tmonjalo/08b8700c1504b94e49c6e110c9428d81 to your computer and use it in GitHub Desktop.
bluetooth audio profile with PulseAudio
#! /bin/sh -e
# argument can be:
# - list
# - next (default)
# - any text matching a profile name or description
arg=${1:-next}
infos=$(pactl list cards | sed -n '/bluez/,/^Card/p')
if [ -z "$infos" ] ; then
echo 'no bluetooth audio device' >&2
exit 1
fi
get_info ()
{
echo "$infos" |
sed -rn "s,[[:space:]]$1(:| =) ,,p" |
tr -d '"'
}
get_profiles()
{
echo "$infos" |
sed -n '/Profiles:/,/Active Profile:/p' |
sed -n '/available: yes/p' |
sed 's,[[:space:]]*\(.*\) (.*,\1,'
}
card=$(get_info 'Name')
bus=$(get_info 'device.bus')
form=$(get_info 'device.form_factor')
name=$(get_info 'device.description')
profile=$(get_info 'Active Profile')
printf "[%s %s %s] %s" $bus $form $name $profile
profiles=$(get_profiles)
if [ "$arg" = 'list' ] ; then
echo
echo "$profiles" | sed 's,^,\t- ,'
exit 0
fi
if [ "$arg" = 'next' ] ; then # round-robin
profiles_count=$(echo "$profiles" | wc -l)
profile_index=$(echo "$profiles" | sed -n "/$profile:/=")
next_profile_index=$(($profile_index % $profiles_count + 1))
new_profile=$(echo "$profiles" | sed -n "${next_profile_index}s,: ,\n,p")
else # free-form match
new_profile=$(echo "$profiles" | sed -n "/$arg/Ip" | head -n1 | sed 's,: ,\n,')
if [ -z "$new_profile" ] ; then
echo
echo 'no profile match' >&2
exit 1
fi
fi
echo " -> $new_profile"
pactl set-card-profile $card $(echo "$new_profile" | head -n1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment