Skip to content

Instantly share code, notes, and snippets.

@rifatx
Last active May 4, 2024 15:16
Show Gist options
  • Save rifatx/0c04ab3e0a53b17037c4c1487f237f55 to your computer and use it in GitHub Desktop.
Save rifatx/0c04ab3e0a53b17037c4c1487f237f55 to your computer and use it in GitHub Desktop.
raspi osmc setup and autoconnect to alsa:default bt speaker
#!/bin/bash
# tweaked from https://gist.github.com/mitchese/7a3373132f94fc946b1cbeb2b04e7818
USER="kodi-user"
PASSWORD="kodi-pass"
PORT="8080"
ANALOGUEDEVICE="ALSA:sysdefault:CARD=Headphones"
BTDEVICE="ALSA:bluealsa"
STAT="NA"
SetAnalogueOutput()
{
curl -u ${USER}:${PASSWORD} -H "Content-type: application/json" -X POST -d "{\"jsonrpc\":\"2.0\",\"method\":\"Settings.SetSettingValue\", \"params\":{\"setting\":\"audiooutput.audiodevice\",\"value\":\"${ANALOGUEDEVICE}\"},\"id\":1}" http://127.0.0.1:${PORT}/jsonr
}
SetBTOutput()
{
curl -u ${USER}:${PASSWORD} -H "Content-type: application/json" -X POST -d "{\"jsonrpc\":\"2.0\",\"method\":\"Settings.SetSettingValue\", \"params\":{\"setting\":\"audiooutput.audiodevice\",\"value\":\"${BTDEVICE}\"},\"id\":1}" http://127.0.0.1:${PORT}/jsonrpc > /
}
CheckDevice()
{
boundbt=`bluetoothctl devices | grep Device | awk '{print $2}'`
devicename=`bluetoothctl devices | grep Device | cut -d" " -f3-`
if [ ! -z $boundbt ] ; then
# echo "if"
connectedbt=`hcitool con | tail -1 | awk '{print $3}'`
if [ "$connectedbt" = "$boundbt" ] ; then
# echo "bt"
[ $STAT != "BT" ] && SetBTOutput
STAT="BT"
else
# echo "analog"
[ $STAT != "HDMI" ] && SetAnalogueOutput
STAT="HDMI"
fi
fi
}
while true ;
do
CheckDevice
sleep 10
done
sudo apt-get install -y a2dp-app-osmc alsa-utils
# copy btaudioautoconnect.sh and
# run from /etc/rc.local with &.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment