Skip to content

Instantly share code, notes, and snippets.

@mrverrall
Last active December 11, 2015 07:09
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 mrverrall/3e1c5ca3ef47f7456ca7 to your computer and use it in GitHub Desktop.
Save mrverrall/3e1c5ca3ef47f7456ca7 to your computer and use it in GitHub Desktop.
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