Skip to content

Instantly share code, notes, and snippets.

@peak-load
Forked from mill1000/README.md
Created August 5, 2018 11:42
Show Gist options
  • Select an option

  • Save peak-load/0c007e31a543cec16f8fb0b09cb83ae2 to your computer and use it in GitHub Desktop.

Select an option

Save peak-load/0c007e31a543cec16f8fb0b09cb83ae2 to your computer and use it in GitHub Desktop.
Headless A2DP Audio Streaming on Raspbian Stretch

About

This gist will show how to setup Raspbian Stretch as a headless Bluetooth A2DP audio sink. This will allow your phone, laptop or other Bluetooth device to play audio wirelessly through a Rasperry Pi.

Motivation

A quick search will turn up a plethora of tutorials on setting up A2DP on the Raspberry Pi. However, I felt this gist was necessary beacuse this solution is:

  • Automatic & Headless - Once setup, the system is entirely automatic. No user iteration is required to pair, connect or start playback. Therefore the Raspberry Pi can be run headless.
  • Simple - This solution has few dependencies, readily available packages and minimal configuration.
  • Up to date - As of December 2017. Written for Raspbian Stretch & Bluez 5.43

Pre-requisites

Disabling Integrated Bluetooth

If you are using a separate USB Bluetooth dongle, disbale the integrated Bluetooth to prevent conflicts.

To disable the integrated Bluetooth Add the

# Disable onboard Bluetooth
dtoverlay=pi3-disable-bt

to /boot/config.txt and execute the following command

sudo systemctl disable hciuart.service

Initial Setup

First execute the following commands to make sure the system is up to date.

sudo apt-get update
sudo apt-get upgrade

Then reboot the Pi to ensure the latest kernal is loaded.

Now install the pre-requisite packages.

sudo apt-get install bluealsa python-dbus

Make Bluetooth Discoverable

Normally a Bluetooth device is only discoverable for a limited amount of time. Since this is a headless setup we want the device to always be discoverable.

  • Set the DiscoverableTimeout in /etc/bluetooth/main.conf to 0
# How long to stay in discoverable mode before going back to non-discoverable
# The value is in seconds. Default is 180, i.e. 3 minutes.
# 0 = disable timer, i.e. stay discoverable forever
DiscoverableTimeout = 0
  • Enable discovery on the Bluetooth controller
sudo bluetoothctl
power on
discoverable on
exit
@peak-load
Copy link
Author

peak-load commented Aug 5, 2018

On recent raspbian:

No LSB modules are available.
Distributor ID:	Raspbian
Description:	Raspbian GNU/Linux 9.4 (stretch)
Release:	9.4
Codename:	stretch

Using a2dp-playback.service with pi user not works, because of permissions issues for that user:

pi@raspberrypi:~ $ /usr/bin/bluealsa-aplay --profile-a2dp 00:00:00:00:00:00
/usr/bin/bluealsa-aplay: BlueALSA connection failed: Permission denied

Adding to lp and bluetooth not enough too... I changed to run as root for now. Please check.

@ValentinFunk
Copy link

ValentinFunk commented Dec 7, 2019

If you want to have a fixed pincode to connect to bluetoothe for the raspberrypi, use this a2dp-agent: https://gist.github.com/Kamshak/55558bdde6aaa54c942132b39301d466/raw/a7dee305c0ea074a9c1f1988cee7b17c0bc4867a/a2dp-agent

you can change the code at the top of the file

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