Created
July 13, 2013 14:27
-
-
Save mdup/5990899 to your computer and use it in GitHub Desktop.
Raspberry PI automatic A2DP receiver script.
Handles several devices which may connect and disconnect live. Source:
http://www.blackv.tk/projects/retropi/raspberry-pi-a2dp-audio-sink/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| #change if you don't use default audio out | |
| AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo" | |
| echo "Executing bluetooth script...|$ACTION|" >> /var/log/bluetooth_dev | |
| ACTION=$(expr "$ACTION" : "\([a-zA-Z]\+\).*") | |
| if [ "$ACTION" = "add" ] | |
| then | |
| for dev in $(find /sys/devices/virtual/input/ -name input*) | |
| do | |
| if [ -f "$dev/name" ] | |
| then | |
| mac=$(cat "$dev/name" | sed 's/:/_/g') | |
| bluez_dev=bluez_source.$mac | |
| sleep 1 | |
| CONFIRM=`sudo -u pi pactl list short | grep $bluez_dev` | |
| EXISTS=`sudo -u pi pactl list short | grep "module-loopback\ssource=$bluez_dev"` # exists if this is not zero | |
| if [ ! -z "$CONFIRM" -a -z "$EXISTS" ] | |
| then | |
| echo "Setting bluez_source to: $bluez_dev" >> /var/log/bluetooth_dev | |
| echo pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev | |
| sudo -u pi pactl load-module module-loopback source=$bluez_dev sink=$AUDIOSINK rate=44100 adjust_time=0 >> /var/log/bluetooth_dev | |
| fi | |
| fi | |
| done | |
| fi | |
| if [ "$ACTION" = "remove" ] | |
| then | |
| # better run the following several times, it won't hurt, it will just try to | |
| # remove loopback-modules for bluetooth devices that no longer exist | |
| for i in 1 2 3 4 5 | |
| do | |
| echo trying to remove stale bluetooth devices "($i/5)"... >>/var/log/bluetooth_dev | |
| for removed_entry in $(sudo -u pi pactl list short | grep module-loopback.source= | sed 's/.*source=bluez_source.\(.._.._.._.._.._..\).*/\1/' | sed 's/_/:/g' | grep -vF "`sudo -u pi pactl list short | grep module-bluetooth-device | grep address | sed 's/.*address=.\(..:..:..:..:..:..\)..*/\1/'`") | |
| do | |
| echo removed entry: $removed_entry >> /var/log/bluetooth_dev | |
| module_n=$(sudo -u pi pactl list modules short | grep module-loopback | grep `echo $removed_entry | sed 's/:/_/g'` | awk '{ print $1; }') | |
| echo sudo -u pi pactl unload-module $module_n >> /var/log/bluetooth_dev | |
| sudo -u pi pactl unload-module $module_n | |
| done | |
| sleep 1s | |
| done | |
| echo ending remove process >>/var/log/bluetooth_dev | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment