Create a gist now

Instantly share code, notes, and snippets.

@mrverrall /bt_audio_attach Secret
Last active Dec 11, 2015

Bluetooth Connector
#!/bin/bash
function setDefaults {
# The default rpi audio sink
PA_SINK='alsa_output.platform-bcm2835_AUD0.0.analog-stereo'
# Set a default source
PA_SOURCE='none'
}
function getSource {
# Get the first pulse audio bt source
pactl list sources short | fgrep -m 1 bluez | awk '{print $2}'
}
function setVolume {
# Set our volume to max
pacmd set-sink-volume 0 65537
amixer set Master 100%
}
function connect {
# Always start afresh
killall -9 pulseaudio
# Connect source to sink
pactl load-module module-loopback \
source="$PA_SOURCE" sink="$PA_SINK" rate=44100 adjust_time=0
}
# Start at a known state
setDefaults
# Keep on checking for a change
while true; do
# Get the bt sources
_newSrc=$(getSource)
# Did we get a bt source and is it new?
if [ -z "$_newSrc" ] ; then
# We don't have a source so set back to a known state
setDefaults
elif [ $_newSrc != $PA_SOURCE ] ; then
# Great, lets connect the new bluetooth source.
PA_SOURCE=$_newSrc
connect
setVolume
fi
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment