+# This script is called by udev when you link a bluetooth device with your comp$
+# It's called to add or remove the device from pulseaudio
+LOGFILE="/var/log/bluetooth_dev"
+# Name of the local sink in this computer
+# You can get it by calling : pactl list short sinks
+# AUDIOSINK="alsa_output.platform-bcm2835_AUD0.0.analog-stereo"
+AUDIOSINK="alsa_output.0.analog-stereo"
+# User used to execute pulseaudio, an active session must be open to avoid erro$
+# Audio Output for raspberry-pi
+# 0=auto, 1=headphones, 2=hdmi.
+# If on, this computer is not discovearable when an audio device is connected
+echo "For output see $LOGFILE"
+## This function add the pulseaudio loopback interface from source to sink
+## The source is set by the bluetooth mac address using XX_XX_XX_XX_XX_XX forma$
+## param: XX_XX_XX_XX_XX_XX
+ if [ -z "$1" ] # zero params
+ echo "Mac not found" >> $LOGFILE
+ mac=$1 # Mac is parameter-1
+ bluez_dev=bluez_source.$mac
+ echo "bluez source: $mac" >> $LOGFILE
+ # This script is called early, we just wait to be sure that pulseaudio $
+ # Very that the source is present
+ CONFIRM=`sudo -u pi pactl list short | grep $bluez_dev`
+ echo "Adding the loopback interface: $bluez_dev" >> $LOGFILE
+ echo "sudo -u $USER pactl load-module module-loopback source=$bluez$
+ # This command route audio from bluetooth source to the local sink..
+ # it's the main goal of this script
+ sudo -u $USER pactl load-module module-loopback source=$bluez_dev s$
+ echo "Unable to find a bluetooth device compatible with pulsaudio u$
+## This function will detect the bluetooth mac address from input device and co$
+## Lots of devices are seen as input devices. But Mac OS X is not detected as i$
+detect_mac_from_input(){
+ echo "Detecting mac from input devices" >> $LOGFILE
+ for dev in $(find /sys/devices/virtual/input/ -name input*)
+ mac=$(cat "$dev/name" | sed 's/:/_/g')
+ # Endfor if the command is successfull
+ if [ $ERRORCODE -eq 0]; then
+## This function will detect the bt mac address from dev-path and configure it.
+## Devpath is set by udev on device link
+detect_mac_from_devpath(){
+ if [ ! -z "$DEVPATH" ]; then
+ echo "Detecting mac from DEVPATH" >> $LOGFILE
+ for dev in $(find /sys$DEVPATH -name address)
+ mac=$(cat "$dev" | sed 's/:/_/g')
+ # Endfor if the command is successfull
+ if [ $ERRORCODE -eq 0]; then
+ echo "DEVPATH not set, wrong bluetooth device? " >> $LOGFILE
+## Detecting if an action is set
+if [ -z "$ACTION" ]; then
+ echo "The script must be called from udev." >> $LOGFILE
+ACTION=$(expr "$ACTION" : "\([a-zA-Z]\+\).*")
+ # Turn off bluetooth discovery before connecting existing BT device to $
+ if [ $ENABLE_BT_DISCOVER == 1 ]
+ echo "Set computer as hidden" >> $LOGFILE
+ echo "Not setting computer as hidden" >> $LOGFILE
+ # Detect BT Mac Address from input devices
+ # Detect BT Mac address from device path on a bluetooth event
+ if [ "$SUBSYSTEM" == "bluetooth" ]; then
+ detect_mac_from_devpath
+ # Check if the add was successfull, otherwise display all available sources
+ echo "Your bluetooth device is not detected !" >> $LOGFILE
+ echo "Available sources are:" >> $LOGFILE
+ sudo -u $USER pactl list short sources >> $LOGFILE
+ echo "Device successfully added " >> $LOGFILE
+ # Turn off bluetooth discovery before connecting existing BT device to $
+ if [ $ENABLE_BT_DISCOVER == 1 ]
+ echo "Set computer as visable" >> $LOGFILE
+ sudo hciconfig hci0 piscan
+ echo "Not setting computer as visible" >> $LOGFILE
+ echo "Removed Audio Device" >> $LOGFILE
+ echo "Unsuported action $action" >> $LOGFILE