Skip to content

Instantly share code, notes, and snippets.

@lanrat
Created April 4, 2017 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanrat/5621b342394b3e1bd9b209a869bb8e93 to your computer and use it in GitHub Desktop.
Save lanrat/5621b342394b3e1bd9b209a869bb8e93 to your computer and use it in GitHub Desktop.
Force ALSA to play nice with BT headset
#! /usr/bin/env bash
# info: https://wiki.archlinux.org/index.php/Bluetooth_headset#Troubleshooting
set -eu
DEVICE_NAME="PLT_BBTPRO"
reset_bt()
{
# turn card off
CARD=$(pactl list cards short | cut -f2 | grep blue | head -1)
if [ ! -z "${CARD}" ]; then
echo "Setting a2dp card off : ${CARD}"
pacmd set-card-profile ${CARD} off
sleep 2
fi
# BT off
echo -e 'power off\nquit' | bluetoothctl
sleep 5
# bt on
echo -e 'power on\nquit' | bluetoothctl
sleep 5
# find device MAC
MAC=$(echo -e 'paired-devices\nquit' | bluetoothctl | grep "^Device" | grep "$DEVICE_NAME" | cut -d ' ' -f2)
# bt connect
echo -e "connect $MAC\nquit" | bluetoothctl
echo "Waiting for Connection to settle"
sleep 15
}
set_audio()
{
# set card
CARD=$(pactl list cards short | cut -f2 | grep blue | head -1)
if [ -z "${CARD}" ]; then
echo "No bluetooth card found"
return $(false)
fi
echo "Turning profile off"
pacmd set-card-profile ${CARD} off
sleep 1
echo "Setting a2dp card: ${CARD}"
pacmd set-card-profile ${CARD} a2dp_sink
sleep 1
# set sink
SINK=$(pactl list sinks short | cut -f2 | grep blue | head -1)
if [ -z "${SINK}" ]; then
echo "No bluetooth sink found"
return $(false)
fi
echo "Sink: ${SINK}"
pacmd set-default-sink ${SINK}
sleep 1
# set source
SOURCE=$(pactl list sources short | cut -f2 | grep blue | head -1)
if [ -z "${SOURCE}" ]; then
echo "No bluetooth source found"
return $(false)
fi
echo "Source: ${SOURCE}"
pacmd set-default-source ${SOURCE}
sleep 1
return $(true)
}
test_bt_audio()
{
# TODO test for audio errors
mplayer -endpos 00:00:05 03.\ Franz\ Ferdinand\ -\ Take\ Me\ Out.mp3
}
# do the things
if set_audio;
then
echo "Set Audio on first try!"
else
echo "Audio Sink failed, resetting BT"
reset_bt
if ! set_audio;
then
echo "Fatal audio error, try running again?"
exit
fi
fi
test_bt_audio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment