Skip to content

Instantly share code, notes, and snippets.

@lanrat
Created July 28, 2018 05:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanrat/0c9df3bf9d41e211afc12e3eb7868eaf to your computer and use it in GitHub Desktop.
Save lanrat/0c9df3bf9d41e211afc12e3eb7868eaf to your computer and use it in GitHub Desktop.
ALSA for Bluetooth on Linux reset Script
#! /usr/bin/env bash
# info: https://wiki.archlinux.org/index.php/Bluetooth_headset#Troubleshooting
set -eu
DEVICE_NAME="TODO"
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
}
unset_audio()
{
# find 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
}
set_audio()
{
# find 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 "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
#Audio device got stuck!
#A: 0.1 (00.0) of 237.0 (03:57.0) ??,?%
mplayer -endpos 00:00:05 test.mp3
}
# do the things
if set_audio;
then
echo "Set Audio on first try!"
else
unset_audio
if set_audio;
then
echo "Set Audio on after unset!"
else
echo "Audio Sink failed, resetting BT"
reset_bt
unset_audio
if ! set_audio;
then
echo "Fatal audio error, try running again?"
exit
fi
fi
fi
test_bt_audio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment