Skip to content

Instantly share code, notes, and snippets.

@ericbolo
Last active December 4, 2017 13:08
Show Gist options
  • Save ericbolo/f57a044f98e89152c09a3d38497d86a7 to your computer and use it in GitHub Desktop.
Save ericbolo/f57a044f98e89152c09a3d38497d86a7 to your computer and use it in GitHub Desktop.
Bluetooth audio receiver with Rapsberry PI 3 (Raspbian Jessie)

Introduction

This walks you through setting up your Raspberry PI 3 (Raspbian Jessie) as a bluetooth audio receiver.

Credits https://www.raspberrypi.org/forums/viewtopic.php?t=68779. Parts of this tutorial did not work for me, so I'm noting my fixes here. Also, I added instructions for setting up a custom USB sound card for better quality.

Set up USB sound card on Raspberry PI

List audio devices

aplay -l

This will list the available audio devices. In my case:

**** List of PLAYBACK Hardware Devices ****
card 1: Set [C-Media USB Headphone Set], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
card 2: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 2: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

Configure ALSA

We need to tell ALSA (Advanced Linux Sound Architecture) to use the USB sound card.

sudo nano /usr/share/alsa/alsa.conf

Change

defaults.ctl.card 0

to

default.ctl.card 1

and

defaults.pcm.card 0

to

defaults.pcm.card 1

Install dependencies

sudo apt-get update

sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2

Note: bluez is used to manage bluetooth and pulse-audio is used for sound-management.

Change audio default settings

Add "pi" user (or whichever is your user) to the lp group to allow it to manage bluetooth

sudo usermod -a -G lp pi

Change the default bluetooth audio settings

The original tutorial says to edit sudo nano /etc/bluetooth/audio.conf, but no such file in latest RPI. Instead, edit /etc/bluetooth/main.conf.

Add complete the following line in the [General] section:

Enable=Source,Sink,Media,Socket

Configure pulse-audio resampling algorithms

sudo nano /etc/pulse/daemon.conf

Add or edit the following line:

resample-method = trivial

Reboot:

sudo reboot

Pair with your bluetooth device

The RPI 3 comes with bluetooth.

I used the GUI and followed these steps:

1. Right click the bluetooth systeay icon at the top right and click "Make discoverable"
2. Right click the bluetooth systray icon at the top right. Add device , then select the appropriate device from the scanned list, and connect. This will ask to confirm a code on both the RPI and the bluetooth device. Confirm on both sides.
3. Connect via the Bluetooth manager: Menu > Preferences > Bluetooth Manager, right click on the device and click "Connect"

Note: I found the above process to work for me, but not sure I need to connect twice.

The device needs to be paired and trusted.

Connect bluetooth source with local sink with pulse-audio

Make sure pulseaudio daemon is running

pulseaudio -D

List pulse-audio controller sources

pactl list sources short

In the list you should see something like bluez_source.AA_AA_AA_AA_AA_AA where AA_AA_AA_AA_AA_AA is the MAC address of your bluetooth device.

List pulse-audio controller sinks

pactl list sinks short

Couple sink and source

Note: you must change the name of the sink and source to the names of your own bluetooth device and sink:

pactl load-module module-loopback source=bluez_source.AA_AA_AA_AA_AA_AA sink=alsa_output.usb-0d8c_C-Media_USB_Headphone_Set-00-Set.analog-stereo

You can test by playing a youtube video, music file, etc. on your bluetooth-connected phone.

TODO: add instructions for automating connection and sink/source coupling. Haven't gotten to that part yet.

@abhishekr700
Copy link

possible reason if I don't see a bluez device in
pactl list sources short
??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment