Raspbian Wheezy Installation Guide
From a fresh installation of Wheezyrun the following as the default user pi:
Add the user pi to the group lp. This is so the user pi can get access to the bluetooth daemon. If this isn’t done, the bluez source will not show up in pulse audio.
The bluetooth stack has to be configured for A2DP.
Add the following line after [General]:
Change the resampling algorithm for pulse audio
resample-method = trivial ; ADD THIS LINE
Reboot at this point!
NOTE: Do not run these with sudo… Run as pi!
Boot up, log-in, and run:
Makes the Pi BT discoverable
Connect with your smart phone and enter pin / authorize.
After authorization is complete, CTRL-C out.
You can then trust the phone by using the command:
It shouldn’t ask for a pin in the future…
After the phone is connected, list the sources. A bluez source should show up:
Note: This command will start the PulseAudio daemon in per-user mode if it is not already running!
0 alsa_output.platform-bcm2835_AUD0.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
1 bluez_source.B8_C6_8E_52_E8_CA module-bluetooth-device.c s16le 2ch 44100Hz SUSPENDED
List the sinks. An ALSA sink for the BCM codec should show up:
0 alsa_output.platform-bcm2835_AUD0.0.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
Connect the source to the sink manaual as a test:
If you play the song on the phone, it should be played on the connected speakers. If not, maybe the default output isn’t the line out jack or the volume is muted or too low.
The audio defaults to the HDMI output. I wanted it to play over the headphone jack.
Here is the command to set the source to headphone jack:
turn up the ALSA volume:
Also turn up the PulseAudio volume:
The 0 is the sink channel number for alsa_output.platform-bcm2835_AUD0.0.analog-stereo as found with the command: “pactl list sinks short”.
Script to automate this when a bluetooth device attaches
To trigger the udev script when a new device connects create (or modify) an even handler in /etc/udev/rules.d/99-input.rules adding the following content rules
KERNEL=="input[0-9]*", RUN+="/usr/lib/udev/bluetooth"
Now let’s actually save that script that the above rule file is referencing. First check to see if a udev/ directory exists. Do sudo ls –la /usr/lib/udev . If it doesn’t exist go ahead and create it like so: sudo mkdir /usr/lib/udev
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 | #!/bin/bash # This script belongs in /usr/lib/udev/bluetooth and should be executable #PA_SINK='alsa_output.platform-bcm2835_AUD0.0.analog-stereo' #Change SINK to 0 as name of sink changes on hardware change. 0 Is default PA_SINK=0 LOG_FILE=/var/log/bluetooth_a2dp MAC=$(echo "$NAME" | sed 's/:/_/g' | sed 's/"//g') # Set the user you want to run as, 'pi' would be fine for most. BT_USER=pi function checkSource { # Get the current sources local _sources=$(sudo -u "$BT_USER" pactl list sources short) # Check if any sources are currently running # and that our new device is valid. if [[ ! "$_sources" =~ RUNIING ]] && [[ "$_sources" =~ "$1" ]] ; then echo "Validated new source: $1" >> "$LOG_FILE" echo "$1" fi } function setVolume { # Set our volume to max sudo -u "$BT_USER" pacmd set-sink-volume 0 65537 sudo -u "$BT_USER" amixer set Master 100% } function connect { # Connect source to sink sudo -u "$BT_USER" pactl load-module module-loopback \ source="$1" sink="$PA_SINK" rate=44100 adjust_time=0 } echo "Change for device $MAC detected, running $ACTION" >> "$LOG_FILE" if [ "$ACTION" = "add" ] then incoming=bluez_source."$MAC" if [ ! -z $(checkSource "$incoming") ] ; then connect "$incoming" setVolume fi fi |
disable timeouts by adding a line in ‘/etc/pulse/client.conf’
The Pi User has to be logged in for this to work. In a headless setup you can get the Pi to auto login by editing the /etc/inittab file and replacing the line
with
Also forget about auto pairing, not going to happen. BLUEZ 4 onwards disabled this as a security measure. Your gonna have to SSH in and pair your devices