Snippets
Alex Hogen Copied from https://www.raspberrypi.org/forums/viewtopic.php?f=35&t=161944 on March 11, 2017. This needs to be located at `/usr/lib/udev/bluetooth`, as the tutorial at the URL says.
Created by
Alex Hogen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #!/bin/bash
# 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
#
#
# Output to this file
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$
USER="pi"
# Audio Output for raspberry-pi
# 0=auto, 1=headphones, 2=hdmi.
AUDIO_OUTPUT=1
# If on, this computer is not discovearable when an audio device is connected
# 0=off, 1=on
ENABLE_BT_DISCOVER=1
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
## return 0 on success
add_from_mac(){
if [ -z "$1" ] # zero params
then
echo "Mac not found" >> $LOGFILE
else
mac=$1 # Mac is parameter-1
# Setting source name
bluez_dev=bluez_source.$mac
echo "bluez source: $mac" >> $LOGFILE
# This script is called early, we just wait to be sure that pulseaudio $
sleep 1
# Very that the source is present
CONFIRM=`sudo -u pi pactl list short | grep $bluez_dev`
if [ ! -z "$CONFIRM" ]
then
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$
return $?
else
echo "Unable to find a bluetooth device compatible with pulsaudio u$
return -1
fi
fi
}
## 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$
## return 0 on success
detect_mac_from_input(){
ERRORCODE=-1
echo "Detecting mac from input devices" >> $LOGFILE
for dev in $(find /sys/devices/virtual/input/ -name input*)
do
if [ -f "$dev/name" ]
then
mac=$(cat "$dev/name" | sed 's/:/_/g')
add_from_mac $mac
# Endfor if the command is successfull
ERRORCODE=$?
if [ $ERRORCODE -eq 0]; then
return 0
fi
fi
done
# Error
return $ERRORCODE
}
## This function will detect the bt mac address from dev-path and configure it.
## Devpath is set by udev on device link
## return 0 on success
detect_mac_from_devpath(){
ERRORCODE=-1
if [ ! -z "$DEVPATH" ]; then
echo "Detecting mac from DEVPATH" >> $LOGFILE
for dev in $(find /sys$DEVPATH -name address)
do
mac=$(cat "$dev" | sed 's/:/_/g')
add_from_mac $mac
# Endfor if the command is successfull
ERRORCODE=$?
if [ $ERRORCODE -eq 0]; then
return 0
fi
done
return $ERRORCODE;
else
echo "DEVPATH not set, wrong bluetooth device? " >> $LOGFILE
return -2
fi
return $ERRORCODE
}
## Detecting if an action is set
if [ -z "$ACTION" ]; then
echo "The script must be called from udev." >> $LOGFILE
exit -1;
fi
## Getting the action
ACTION=$(expr "$ACTION" : "\([a-zA-Z]\+\).*")
# Switch case
case "$ACTION" in
"add")
# Turn off bluetooth discovery before connecting existing BT device to $
if [ $ENABLE_BT_DISCOVER == 1 ]
then
echo "Set computer as hidden" >> $LOGFILE
hciconfig hci0 noscan
else
echo "Not setting computer as hidden" >> $LOGFILE
fi
# Turn volume to max
volume_max
# Detect BT Mac Address from input devices
detect_mac_from_input
OK=$?
# Detect BT Mac address from device path on a bluetooth event
if [ $OK != 0 ]; then
if [ "$SUBSYSTEM" == "bluetooth" ]; then
detect_mac_from_devpath
OK=$?
fi
fi
# Check if the add was successfull, otherwise display all available sources
if [ $OK != 0 ]; then
echo "Your bluetooth device is not detected !" >> $LOGFILE
echo "Available sources are:" >> $LOGFILE
sudo -u $USER pactl list short sources >> $LOGFILE
else
echo "Device successfully added " >> $LOGFILE
fi
;;
"remove")
# Turn off bluetooth discovery before connecting existing BT device to $
if [ $ENABLE_BT_DISCOVER == 1 ]
then
echo "Set computer as visable" >> $LOGFILE
sudo hciconfig hci0 piscan
else
echo "Not setting computer as visible" >> $LOGFILE
fi
echo "Removed Audio Device" >> $LOGFILE
;;
#
*)
echo "Unsuported action $action" >> $LOGFILE
;;
esac
echo "--" >> $LOGFILE
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.