Bluetooth Audio for (headless) Raspbian Jessie

  • # start edit 21.04.2018 16:52 ->

    Preface

    Why now a tutorial for Raspbian Jessie?
    There is hardware (e.g. adafruit 3.5" pitft touchscreen) and software (e.g. Peppy Player) which doesn't run with Raspbian Stretch.

    Introduction

    I saw GitHub "Bluetooth Audio for (headless) Raspbian systems" from Bernhard Bablok .

    The instructions work with the Raspbian Stretch package 'bluealsa'. (Info: for Jessie there are only 'bluealsa' sources.)


    Quote from Bernhard Bablok

    Audio on Linux systems has always been rather complicated. ALSA (Advanced Linux Sound Architecture) is advanced indeed and thus even simple setups are complicated.


    Bluetooth adds another level of complexity. And therefore the forums are full of posts asking for help on how to connect a Linux system with a bluetooth speaker or headset. This is no different with Raspbian.


    Until Raspbian-Jessie, for bluetooth-devices you additionally needed PulseAudio, making things even more complicated. With Stretch this has changed and PulseAudio was replaced with a rather simple helper daemon called bluealsa. Nevertheless, a simple setup is still complicated, partly because the maintainer of the bluealsa package installs the daemon only for grafical environments.


    My instructions cover:

    • build and install bluealsa from the sources.
    • create, start and enable service bluealsa.
    • create headset script to connect bluetooth audio device.
    • create, start and enable service headset.
    • configure mpd.


    In Raspbian-Jessie you can use bluetooth audio devices only with PulseAudio.

    In Raspbian-Stretch PulseAudio is replaced by bluealsa.

    Here is an howto for using bluealsa in Raspbian-Jessie.

    # <- end edit 21.04.2018 16:52


    I tested the tutorial with the following images with mpd, mpg123, mplayer and cvlc.

    Code
    1. 2016-09-23-raspbian-jessie-lite.img # tested with: RPi2B, RPi 3B
    2. 2017-04-10-raspbian-jessie-lite.img # tested with: RPi2B, RPi 3B, RPi 0W
    3. 2017-04-10-raspbian-jessie-lite.img, remember, create 2 files on SD card:
    4. - empty file 'ssh' to enable ssh;
    5. - file 'wpa_supplicant.conf' with ssid and psk... to enable wlan

    This tutorial works also for image pipaos-guarana-console-5.0.img, if you install package pi-bluetooth before.

    Tutorial

    1. Manually build and install bluealsa in jessie with python 2.7.
      ATTENTION: this guide does not change bluetooth desktop controls. Especially the connect BT speaker to sound server in the audio menu will not work.
      1. Check python link with ls -l `which python`.
        If you see /usr/bin/python -> /usr/bin/python2.7
        If you see anything different e.g. /usr/bin/python -> /usr/bin/python3.4 you must follow steps 2, 3 and 4.
      2. Manually build an install bluealsa in jessie
        Reference 1: Required Packages
        Reference 2: Post #2 from Gymnae, see below



    2. Configuration
      I found a runeaudio guide to do that.
      1. Bluealsa service
        1. Create bluealsa service
          with sudo nano /etc/systemd/system/bluealsa.service

          Code: /etc/systemd/system/bluealsa.service
          1. [Unit]
          2. Description=Start the bluez-alsa service
          3. After=bluetooth.service
          4. After=systemd-remount-fs.service
          5. [Service]
          6. ExecStart=/usr/bin/bluealsa
          7. [Install]
          8. WantedBy=bluetooth.target


        2. Start/Enable bluealsa service
          Code
          1. sudo systemctl daemon-reload
          2. sudo systemctl start bluealsa
          3. sudo systemctl enable bluealsa
      2. Connect bluetooth device
        with sudo bluetoothctl -a   
        Code
        1. # scan on
        2. # scan off
        3. # pair <device-mac-addr> (tab completion works)
        4. # trust <device-mac-addr>
        5. # connect <device-mac-addr>
        6. # exit
      3. Test #1 with bluealsa audio device
        ATTENTION: substitute 11:22:33:44:55:66 below with the mac-adr of your bluetooth device.
        aplay -D bluealsa:HCI=hci0,DEV=11:22:33:44:55:66 /usr/share/sounds/alsa/Front_Center.wav
      4. Alsa configuration
        Create an alsa pcm device
        with sudo nano /etc/asound.conf 

        ATTENTION: substitute 11:22:33:44:55:66 below with the mac-adr of your bluetooth device.


      5. Test #2 with alsa pcm device headset
        with aplay -D headset /usr/share/sounds/alsa/Front_Center.wav
        or withmpg123 -a headset -@ http://st01.dlf.de/dlf/01/128/mp3/stream.mp3
        or withmplayer -ao alsa:device=headset http://st01.dlf.de/dlf/01/128/mp3/stream.mp3
        or with cvlc --aout=alsa --alsa-audio-device=headset --verbose=-1 http://st01.dlf.de/dlf/01/128/mp3/stream.mp3
    3. If bluetooth device does not connect after boot and switched on
      1. Headset service
        1. Create bluetooth connect script
          sudo nano /usr/local/bin/headset
          ATTENTION: substitute 11:22:33:44:55:66 below with the mac-adr of your bluetooth device.

          Code: /usr/local/bin/headset
          1. #!/bin/bash
          2. # connect bluetooth device
          3. bluetoothctl << EOF
          4. connect 11:22:33:44:55:66
          5. quit
          6. EOF

          Make script executable

          sudo chmod 755 /usr/local/bin/headset

        2. Create headset service
          sudo nano /etc/systemd/system/headset.service

          Code: /etc/systemd/system/headset.service
          1. [Unit]
          2. Description=Connect bluetooth headset
          3. After=bluealsa.service
          4. [Service]
          5. ExecStart=/usr/local/bin/headset
          6. [Install]
          7. WantedBy=bluetooth.target


        3. Start/Enable headset service headset

          Code
          1. sudo systemctl daemon-reload
          2. sudo systemctl start headset
          3. sudo systemctl enable headset
    4. If you use mpd, add an audio_output in the config
      with sudo nano /etc/mpd.conf
      Code: /etc/mpd.conf
      1. audio_output {
      2. type "alsa"
      3. name "JBL Go" # please change
      4. device "headset"
      5. mixer_type "software"
      6. auto_resample "no"
      7. auto_format "no"
      8. enabled "no"
      9. }

      Info: Bluetooth device cannot connect, if the mpd output is enabled


    5. Run mpd

      <output_no> below is the number of your bluetooth audio device.
      You see it with mpc outputs command.

      ATTENTION: exchange <output_no> below with the mpc output number (e.g. 2).

      Code
      1. mpc clear
      2. mpc ls # lists your playlists
      3. mpc load <playlist>
      4. mpc enable <output_no>
      5. mpc play

      If it works you can 'autostart' mpd.
      With sudo nano /etc/rc.local
      add 2 lines before exit 0.

      ATTENTION: exchange <output_no> below with the mpc output number (e.g. 2).

      Code: /etc/rc.local
      1. # send mpd commands as user pi
      2. su pi -c 'mpc enable <output_no>; mpc play;'
      3. exit 0

      :danke_ATDE: Linus

    Edited 2 times, last by kle: /usr/bin/python is a link to python2.7 ().