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
    2016-09-23-raspbian-jessie-lite.img # tested with: RPi2B, RPi 3B
    2017-04-10-raspbian-jessie-lite.img # tested with: RPi2B, RPi 3B, RPi 0W
    2017-04-10-raspbian-jessie-lite.img, remember, create 2 files on SD card:
    - empty file 'ssh' to enable ssh;
    - 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
          [Unit]
          Description=Start the bluez-alsa service
          After=bluetooth.service
          After=systemd-remount-fs.service
          [Service]
          ExecStart=/usr/bin/bluealsa
          [Install]
          WantedBy=bluetooth.target


        2. Start/Enable bluealsa service
          Code
          sudo systemctl daemon-reload
          sudo systemctl start bluealsa
          sudo systemctl enable bluealsa
      2. Connect bluetooth device
        with sudo bluetoothctl -a   
        Code
        # scan on
        # scan off
        # pair <device-mac-addr> (tab completion works)
        # trust <device-mac-addr>
        # connect <device-mac-addr>
        # 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
          #!/bin/bash
          # connect bluetooth device
          bluetoothctl << EOF
          connect 11:22:33:44:55:66
          quit
          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
          [Unit]
          Description=Connect bluetooth headset
          After=bluealsa.service
          [Service]
          ExecStart=/usr/local/bin/headset
          [Install]
          WantedBy=bluetooth.target


        3. Start/Enable headset service headset

          Code
          sudo systemctl daemon-reload
          sudo systemctl start headset
          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
      audio_output {
      type "alsa"
      name "JBL Go" # please change
      device "headset"
      mixer_type "software"
      auto_resample "no"
      auto_format "no"
      enabled "no"
      }

      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
      mpc clear
      mpc ls # lists your playlists
      mpc load <playlist>
      mpc enable <output_no>
      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
      # send mpd commands as user pi
      su pi -c 'mpc enable <output_no>; mpc play;'
      exit 0

      :danke_ATDE: Linus

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