-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patha2dp-autoconnect
executable file
·65 lines (51 loc) · 1.64 KB
/
a2dp-autoconnect
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
#!/bin/bash
set -eu
# The original script: http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html.
## EDIT: change these variable appropriately for your setup
# Find the right sink with `pactl list sources short`.
PA_SINK="alsa_output.platform-snd_usb_ipod.analog-stereo" # the fake ipod device
# your bluetooth device's MAC address (yes its hard-coded for now)
NAME="<<YOUR MAC ADDRESS>>"
BT_MAC=$(echo "$NAME" | sed 's/:/_/g' | sed 's/\"//g')
BT_USER=pi
function log {
echo "[$(date)]: $*" # >> /var/log/a2dp-autoconnect
}
function checkSource {
# Get the current sources
local _sources=$(sudo su - "$BT_USER" -c "pactl list sources short")
# Check if any sources are currently running and that our new device is valid.
if [[ "$_sources" =~ RUNNING ]]; then
log "Source is already RUNNING. Available sources:"
log "$_sources"
return
fi
if [[ ! "$_sources" =~ "$1" ]] ; then
log "Unrecognized source. Available sources:"
log "\n$_sources"
return
fi
log "Validated new source: $1."
echo "$1"
}
function setVolume {
log "Setting volume levels."
# Set our volume to max
sudo su - "$BT_USER" -c "pacmd set-sink-volume 0 65537"
sudo su - "$BT_USER" -c "amixer set Master 100%"
}
function connect {
log "Connecting $1 to $PA_SINK"
cmd=$(printf 'pactl load-module module-loopback source="%s" sink="%s" rate=44100 adjust_time=0' $1 $PA_SINK)
# Connect source to sink
sudo su - "$BT_USER" -c "$cmd"
}
log "Change for device $BT_MAC detected, running $ACTION."
if [ "$ACTION" = "add" ]
then
incoming=bluez_source."$BT_MAC".a2dp_source
if [[ ! -z $(checkSource "$incoming") ]] ; then
connect "$incoming"
setVolume
fi
fi