Skip to content

Instantly share code, notes, and snippets.

@jason-costello
Forked from oleq/_README.md
Created August 5, 2017 02:19
Show Gist options
  • Select an option

  • Save jason-costello/c8c936ddb2b537d3d9b6cbba1d48d260 to your computer and use it in GitHub Desktop.

Select an option

Save jason-costello/c8c936ddb2b537d3d9b6cbba1d48d260 to your computer and use it in GitHub Desktop.

Revisions

  1. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ function checkSource {
    # Check if any sources are currently running and that our new device is valid.
    if [[ "$_sources" =~ RUNIING ]]; then
    log "Source is already RUNNING. Available sources:"
    log "\n$_sources"
    log "$_sources"
    return
    fi

  2. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -17,13 +17,13 @@ function checkSource {
    # Check if any sources are currently running and that our new device is valid.
    if [[ "$_sources" =~ RUNIING ]]; then
    log "Source is already RUNNING. Available sources:"
    log "$_sources"
    log "\n$_sources"
    return
    fi

    if [[ ! "$_sources" =~ "$1" ]] ; then
    log "Unrecognized source. Available sources:"
    log "$_sources"
    log "\n$_sources"
    return
    fi

  3. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -18,15 +18,17 @@ function checkSource {
    if [[ "$_sources" =~ RUNIING ]]; then
    log "Source is already RUNNING. Available sources:"
    log "$_sources"
    return
    fi

    if [[ ! "$_sources" =~ "$1" ]] ; then
    log "Unrecognized source. Available sources:"
    log "$_sources"
    return
    fi

    log "Validated new source: $1."
    echo "Source is valid"
    echo "$1"
    }

    function setVolume {
  4. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,8 @@ function checkSource {
    }

    function setVolume {
    log "Setting volume levels."

    # Set our volume to max
    sudo su - "$BT_USER" -c "pacmd set-sink-volume 0 65537"
    sudo su - "$BT_USER" -c "amixer set Master 100%"
  5. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -18,17 +18,15 @@ function checkSource {
    if [[ "$_sources" =~ RUNIING ]]; then
    log "Source is already RUNNING. Available sources:"
    log "$_sources"
    return 0
    fi

    if [[ ! "$_sources" =~ "$1" ]] ; then
    log "Unrecognized source. Available sources:"
    log "$_sources"
    return 0
    fi

    log "Validated new source: $1."
    return 1
    echo "Source is valid"
    }

    function setVolume {
    @@ -38,6 +36,8 @@ function setVolume {
    }

    function connect {
    log "Connecting $1."

    # Connect source to sink
    sudo su - "$BT_USER" -c 'pactl load-module module-loopback source="$1" sink="$PA_SINK" rate=44100 adjust_time=0'
    }
    @@ -47,10 +47,8 @@ log "Change for device $BT_MAC detected, running $ACTION."
    if [ "$ACTION" = "add" ]
    then
    incoming=bluez_source."$BT_MAC"
    if [[ $(checkSource "$incoming") -eq 1 ]] ; then
    if [ ! -z $(checkSource "$incoming") ] ; then
    connect "$incoming"
    setVolume
    else
    log "Source $incoming invalid or some source already running."
    fi
    fi
  6. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -16,18 +16,19 @@ function checkSource {

    # Check if any sources are currently running and that our new device is valid.
    if [[ "$_sources" =~ RUNIING ]]; then
    log "Source is already RUNNING."
    return
    log "Source is already RUNNING. Available sources:"
    log "$_sources"
    return 0
    fi

    if [[ ! "$_sources" =~ "$1" ]] ; then
    log "Unrecognized source."
    log "Unrecognized source. Available sources:"
    log "$_sources"
    return
    return 0
    fi

    log "Validated new source: $1."
    log "$1"
    return 1
    }

    function setVolume {
    @@ -46,7 +47,7 @@ log "Change for device $BT_MAC detected, running $ACTION."
    if [ "$ACTION" = "add" ]
    then
    incoming=bluez_source."$BT_MAC"
    if [ ! -z $(checkSource "$incoming") ] ; then
    if [[ $(checkSource "$incoming") -eq 1 ]] ; then
    connect "$incoming"
    setVolume
    else
  7. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ function log {

    function checkSource {
    # Get the current sources
    local _sources=$(sudo -u "$BT_USER" pactl list sources short)
    local _sources=$(sudo su - "$BT_USER" -c "pactl list sources short")

    # Check if any sources are currently running and that our new device is valid.
    if [[ "$_sources" =~ RUNIING ]]; then
    @@ -32,14 +32,13 @@ function checkSource {

    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%
    sudo su - "$BT_USER" -c "pacmd set-sink-volume 0 65537"
    sudo su - "$BT_USER" -c "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
    sudo su - "$BT_USER" -c 'pactl load-module module-loopback source="$1" sink="$PA_SINK" rate=44100 adjust_time=0'
    }

    log "Change for device $BT_MAC detected, running $ACTION."
  8. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 0 additions and 0 deletions.
    Empty file modified a2dp-autoconnect
    100644 → 100755
    Empty file.
  9. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ function checkSource {

    if [[ ! "$_sources" =~ "$1" ]] ; then
    log "Unrecognized source."
    log "$_sources"
    return
    fi

  10. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,18 @@ function checkSource {
    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
    log "Validated new source: $1."
    log "$1"
    if [[ "$_sources" =~ RUNIING ]]; then
    log "Source is already RUNNING."
    return
    fi

    if [[ ! "$_sources" =~ "$1" ]] ; then
    log "Unrecognized source."
    return
    fi

    log "Validated new source: $1."
    log "$1"
    }

    function setVolume {
  11. @oleq oleq revised this gist Jan 31, 2016. No changes.
  12. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions _README.md
    Original file line number Diff line number Diff line change
    @@ -3,19 +3,19 @@
    This tutorial will turn your [Raspberry PI](https://www.raspberrypi.org/) into a simple Bluetooth audio receiver, which plays music through connected speakers. It's like a regular car audio system, but it can be used anywhere and it's a good value.

    ```
    Audio source (i.e. smartphone)
    |
    v
    Audio source (i.e. smartphone)
    |
    v
    ((( Wireless Bluetooth Channel )))
    |
    v
    Raspberry PI
    |
    v
    USB Audio Interface
    |
    v
    Speakers
    |
    v
    Raspberry PI
    |
    v
    USB Audio Interface
    |
    v
    Speakers
    ```

    The Bluetooth profile which does the magic is called [A2DP](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles#Advanced_Audio_Distribution_Profile_.28A2DP.29).
  13. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 17 additions and 3 deletions.
    20 changes: 17 additions & 3 deletions _README.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,19 @@
    This tutorial will turn your [Raspberry PI](https://www.raspberrypi.org/) into a simple Bluetooth audio receiver, which plays music through connected speakers. It's like a regular car audio system, but it can be used anywhere and it's a good value.

    ```
    Audio source (i.e. smartphone) -((( wireless channel )))-> Raspberry PI -> USB Audio -> Speakers
    Audio source (i.e. smartphone)
    |
    v
    ((( Wireless Bluetooth Channel )))
    |
    v
    Raspberry PI
    |
    v
    USB Audio Interface
    |
    v
    Speakers
    ```

    The Bluetooth profile which does the magic is called [A2DP](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles#Advanced_Audio_Distribution_Profile_.28A2DP.29).
    @@ -157,7 +169,7 @@ Pair devices (phones, tablets, PCs) with PI using `bluetoothctl` utility:
    pi@raspberrypi:~ $ bluetoothctl
    ```

    See that your USB dongle is discovered:
    See that your USB dongle is here:

    ```
    [bluetooth]# list
    @@ -240,4 +252,6 @@ in "Boot Options" -> "Console Auto–login".

    ## Enjoy!

    If your device is already paired, simply connect it to Raspberry PI and select Bluetooth audio output. Enjoy your tunes!
    If your device is already paired, simply connect it to Raspberry PI and select Bluetooth audio output. Enjoy your tunes!

    Tested with iPhone, MacbookPro and Windows laptop.
  14. @oleq oleq renamed this gist Jan 31, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  15. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion howto.md
    Original file line number Diff line number Diff line change
    @@ -205,7 +205,7 @@ pi@raspberrypi:~ $ pactl list sinks short

    The whole trick is to redirect the right **source** (i.e. smartphone) the right **sink** (ALSA) each time a new Bluetooth device is connected. In the above case, it would be `bluez_source.A8_88_08_11_AB_4B` to `alsa_output.0.analog-stereo`.

    The good news that it can be automated. Add udev rule which executes [`a2dp-autoconnect`](a2dp-autoconnect) script each time a Bluetooth device is connected:
    The good news that it can be automated. Add udev rule which executes [`a2dp-autoconnect`](#file-a2dp-autoconnect) script each time a Bluetooth device is connected:

    ```
    pi@raspberrypi:~ $ cat /etc/udev/rules.d/99-input.rules
  16. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion howto.md
    Original file line number Diff line number Diff line change
    @@ -212,7 +212,7 @@ pi@raspberrypi:~ $ cat /etc/udev/rules.d/99-input.rules
    KERNEL=="input[0-9]*", RUN+="/home/pi/a2dp-autoconnect"
    ```

    The script I used is an extended version of the http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html. It's pretty straightforward: it redirects a new Bluetooth audio source to the right sink and sets output volume level.
    The script I used is an extended version of http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html. It's pretty straightforward: it redirects a new Bluetooth audio source to the right sink and sets output volume level.

    I located it in `/home/pi/a2dp-autoconnect`, then made it executable:

  17. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion howto.md
    Original file line number Diff line number Diff line change
    @@ -212,7 +212,7 @@ pi@raspberrypi:~ $ cat /etc/udev/rules.d/99-input.rules
    KERNEL=="input[0-9]*", RUN+="/home/pi/a2dp-autoconnect"
    ```

    The script is an extended version of the [original one](http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html). It's pretty straightforward: it redirects a new Bluetooth audio source to the right sink and sets output volume level.
    The script I used is an extended version of the http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html. It's pretty straightforward: it redirects a new Bluetooth audio source to the right sink and sets output volume level.

    I located it in `/home/pi/a2dp-autoconnect`, then made it executable:

  18. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion howto.md
    Original file line number Diff line number Diff line change
    @@ -212,7 +212,7 @@ pi@raspberrypi:~ $ cat /etc/udev/rules.d/99-input.rules
    KERNEL=="input[0-9]*", RUN+="/home/pi/a2dp-autoconnect"
    ```

    The script is an extended version of [this one](http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html). It's pretty straightforward: it redirects a new Bluetooth audio source to the right sink and sets output volume level.
    The script is an extended version of the [original one](http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html). It's pretty straightforward: it redirects a new Bluetooth audio source to the right sink and sets output volume level.

    I located it in `/home/pi/a2dp-autoconnect`, then made it executable:

  19. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -134,7 +134,7 @@ Class = 0x20041C
    Enable = Source,Sink,Media,Socket
    ```

    I'm not quite sure if the following is also needed. But it doesn't hurt:
    I'm not quite sure if the following is also needed. But I added it anyway:

    ```
    pi@raspberrypi:~ $ cat /etc/bluetooth/main.conf
    @@ -151,17 +151,21 @@ Reboot PI:
    sudo reboot
    ```

    Pair devices (phones, tablets, PCs) with PI:
    Pair devices (phones, tablets, PCs) with PI using `bluetoothctl` utility:

    ```
    bluetoothctl
    pi@raspberrypi:~ $ bluetoothctl
    ```

    See that your USB dongle is discovered:

    ```
    [bluetooth]# list
    Controller 00:1A:7D:DA:71:06 raspberrypi [default]
    ```

    Prepare for pairing:

    ```
    [bluetooth]# agent on
    [bluetooth]# default-agent
    @@ -170,14 +174,20 @@ Controller 00:1A:7D:DA:71:06 raspberrypi [default]
    ```

    Then, for each device:

    ```
    pair XX:XX:XX:XX:XX:XX
    ...
    Go through pairing process.
    ...
    trust XX:XX:XX:XX:XX:XX
    ```

    `CTRL(CMD)+D` to exit `bluetoothctl`.

    ## Setup auto connecting

    Given that the phone is already paired and connected to PI, run the following:
    Given that your device is already paired and connected to PI, run the following:

    ```
    pi@raspberrypi:~ $ pactl list sources short
  20. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion howto.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ This tutorial will turn your [Raspberry PI](https://www.raspberrypi.org/) into a
    Audio source (i.e. smartphone) -((( wireless channel )))-> Raspberry PI -> USB Audio -> Speakers
    ```

    The Bluetooth profile which allows that is called [A2DP](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles#Advanced_Audio_Distribution_Profile_.28A2DP.29).
    The Bluetooth profile which does the magic is called [A2DP](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles#Advanced_Audio_Distribution_Profile_.28A2DP.29).

    ## Obtaining peripherals

  21. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 25 additions and 27 deletions.
    52 changes: 25 additions & 27 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,14 @@
    ## What is this all about?

    This tutorial aggregates various articles to turn your Raspberry PI into a Bluetooth receiver, which plays audio wirelessly through connected speakers:
    This tutorial will turn your [Raspberry PI](https://www.raspberrypi.org/) into a simple Bluetooth audio receiver, which plays music through connected speakers. It's like a regular car audio system, but it can be used anywhere and it's a good value.

    ```
    Audio source (i.e. smartphone) -((( wireless channel )))-> Raspberry PI -> USB Audio -> Speakers
    ```

    It works like a regular car audio system with Bluetooth but can be used anywhere and supports multiple audio sources.

    The Bluetooth profile which allows that is called [A2DP](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles#Advanced_Audio_Distribution_Profile_.28A2DP.29).

    ## Initial setup

    I'm using [Raspberry PI 1 Model B](https://www.raspberrypi.org/products/model-b/), running [Raspbian Jessie](https://www.raspberrypi.org/downloads/raspbian/). Make sure your system is up–to–date first:

    ```
    sudo apt-get update
    sudo apt-get upgrade
    ```

    **Note:** It usually takes a while. Get some tee and sandwiches.

    Then install required packages ([related article](http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/?ALLSTEPS)):

    ```
    sudo apt-get install alsa-utils bluez bluez-tools pulseaudio-module-bluetooth python-gobject python-gobject-2
    ```

    Not quite sure it's really needed (?), but it doesn't hurt:

    ```
    sudo usermod -a -G lp pi
    ```

    ## Obtaining hardware
    ## Obtaining peripherals

    ```
    pi@raspberrypi:~ $ lsusb
    @@ -61,6 +36,29 @@ See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) f
    Raspberry PI offers limited power to USB devices (and limited number of ports). You'll need some active (powered) **USB Hub** to keep USB devices stable and working (USB Audio, USB Bluettoth and optional USB WiFi).
    Google to learn more, it's a very common topic when using Raspberry PI.

    ## Initial setup

    I'm using [Raspberry PI 1 Model B](https://www.raspberrypi.org/products/model-b/), running [Raspbian Jessie](https://www.raspberrypi.org/downloads/raspbian/). Make sure your system is up–to–date first:

    ```
    sudo apt-get update
    sudo apt-get upgrade
    ```

    **Note:** It usually takes a while. Get some tee and sandwiches.

    Then install required packages ([related article](http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/?ALLSTEPS)):

    ```
    sudo apt-get install alsa-utils bluez bluez-tools pulseaudio-module-bluetooth python-gobject python-gobject-2
    ```

    Not quite sure it's really needed (?), but it doesn't hurt:

    ```
    sudo usermod -a -G lp pi
    ```

    ## Setup PulseAudio

    Use the following configuration to get most of PulseAudio ([related article](http://www.crazy-audio.com/2014/09/pulseaudio-on-the-raspbery-pi/)):
  22. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 34 additions and 21 deletions.
    55 changes: 34 additions & 21 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,18 @@
    ## What is this all about?

    This tutorial will convert your Raspberry PI into a Bluetooth receiver, which plays audio wirelessly through connected speakers:
    This tutorial aggregates various articles to turn your Raspberry PI into a Bluetooth receiver, which plays audio wirelessly through connected speakers:

    ```
    Audio source (i.e. smartphone) -((( wireless channel )))-> Raspberry PI -> USB Audio -> Speakers
    ```

    It works like a regular car audio system with Bluetooth but can be used anywhere and supports multiple audio sources.

    The Bluetooth profile which allows that is called [A2DP](https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles#Advanced_Audio_Distribution_Profile_.28A2DP.29).

    ## Initial setup

    I'm using Raspberry PI 1 Model B, running Raspbian Jessie. Make sure the system is up–to–date first:
    I'm using [Raspberry PI 1 Model B](https://www.raspberrypi.org/products/model-b/), running [Raspbian Jessie](https://www.raspberrypi.org/downloads/raspbian/). Make sure your system is up–to–date first:

    ```
    sudo apt-get update
    @@ -19,9 +21,7 @@ sudo apt-get upgrade

    **Note:** It usually takes a while. Get some tee and sandwiches.

    Then install required packages:

    http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/?ALLSTEPS
    Then install required packages ([related article](http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/?ALLSTEPS)):

    ```
    sudo apt-get install alsa-utils bluez bluez-tools pulseaudio-module-bluetooth python-gobject python-gobject-2
    @@ -44,11 +44,11 @@ Bus 001 Device 012: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (
    ...
    ```

    ### Audio
    ### Audio interface

    The on–board audio produces low–quality, noisy output, so I decided to use something better. I used external USB **Creative Sound Blaster Play!** interface.
    The on–board audio produces low–quality, noisy output, so I decided to use something better. I chose external USB **Creative Sound Blaster Play!** interface. It costs ~$20.

    ### Bluetooth
    ### Bluetooth dongle

    As for Bluetooth dongle, I used **Digitus Tiny USB-Adapter**, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.

    @@ -63,9 +63,8 @@ Google to learn more, it's a very common topic when using Raspberry PI.

    ## Setup PulseAudio

    Use the following configuration to get most of PulseAudio:
    Use the following configuration to get most of PulseAudio ([related article](http://www.crazy-audio.com/2014/09/pulseaudio-on-the-raspbery-pi/)):

    http://www.crazy-audio.com/2014/09/pulseaudio-on-the-raspbery-pi/
    ```
    pi@raspberrypi:~ $ cat /etc/pulse/daemon.conf
    ...
    @@ -99,9 +98,7 @@ pi@raspberrypi:~ $ cat /proc/asound/modules
    1 snd_usb_audio
    ```

    Some configuration does the trick and from now on RPI uses USB Audio as default:

    http://raspberrypi.stackexchange.com/questions/40831/how-do-i-configure-my-sound-for-jasper-on-raspbian-jessie
    Some configuration does the trick ([related article](http://raspberrypi.stackexchange.com/questions/40831/how-do-i-configure-my-sound-for-jasper-on-raspbian-jessie)):

    ```
    pi@raspberrypi:~ $ cat /etc/modprobe.d/alsa-base.conf
    @@ -119,7 +116,7 @@ Reboot PI:
    sudo reboot
    ```

    See `snd_usb_audio` as a preferred output:
    From now on RPI uses USB Audio as default:

    ```
    pi@raspberrypi:~ $ cat /proc/asound/modules
    @@ -198,25 +195,41 @@ pi@raspberrypi:~ $ pactl list sinks short
    0 alsa_output.0.analog-stereo module-alsa-card.c s16le 2ch 48000Hz RUNNING
    ```

    The whole trick to make a2dp work is to redirect a right **source** (like smartphone) to the right **sink** (ALSA). In the above case, it would be `bluez_source.A8_88_08_11_AB_4B -> alsa_output.0.analog-stereo`.
    The whole trick is to redirect the right **source** (i.e. smartphone) the right **sink** (ALSA) each time a new Bluetooth device is connected. In the above case, it would be `bluez_source.A8_88_08_11_AB_4B` to `alsa_output.0.analog-stereo`.

    Each time a Bluetooth audio source connects, the new redirection must be made. Add udev rule which executes `a2dp-autoconnect` script each time a bluetooth device is connected
    The good news that it can be automated. Add udev rule which executes [`a2dp-autoconnect`](a2dp-autoconnect) script each time a Bluetooth device is connected:

    ```
    pi@raspberrypi:~ $ sudo cat /etc/udev/rules.d/99-input.rules
    pi@raspberrypi:~ $ cat /etc/udev/rules.d/99-input.rules
    KERNEL=="input[0-9]*", RUN+="/home/pi/a2dp-autoconnect"
    ```

    The script is an extended version of http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html. It redirects a new bluetooth device to the right sink and sets the right volume level.
    The script is an extended version of [this one](http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html). It's pretty straightforward: it redirects a new Bluetooth audio source to the right sink and sets output volume level.

    I located it in `/home/pi/a2dp-autoconnect`, then set it executable:
    I located it in `/home/pi/a2dp-autoconnect`, then made it executable:

    ```
    pi@raspberrypi:~ $ chmod +x a2dp-autoconnect
    ```

    To observe connection log "live":
    **Note**: Observe connection log "live" to debug connection issues:

    ```
    pi@raspberrypi:~ $ tail -f /var/log/a2dp-autoconnect
    ```
    ```

    ## Auto–login

    Some people complained that the whole configuration does not work after reboot, unless `pi` user is logged in.

    Auto–login can be enabled using `raspi–config` utility

    ```
    pi@raspberrypi:~ $ sudo raspi-config
    ```

    in "Boot Options" -> "Console Auto–login".

    ## Enjoy!

    If your device is already paired, simply connect it to Raspberry PI and select Bluetooth audio output. Enjoy your tunes!
  23. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -44,19 +44,19 @@ Bus 001 Device 012: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (
    ...
    ```

    ## Audio
    ### Audio

    The on–board audio produces low–quality, noisy output, so I decided to use something better. I used external USB **Creative Sound Blaster Play!** interface.

    ## Bluetooth
    ### Bluetooth

    As for Bluetooth dongle, I used **Digitus Tiny USB-Adapter**, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.

    **Note**: I used another dongle (different manufacturer) also discovered as `Cambridge Silicon Radio` but unable to stream audio. So beware, because different manufacturers use the same hardware in a different way. Or they pretend to use the same hardware for some (compatibility?) reasons. This way or another, if you get garbled audio or no audio at all but everything else is alright, don't worry, just try another dongle – it's cheap.

    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying recommendations. Trial and error is another option, since most devices cost below $10.

    ## USB Hub
    ### USB Hub

    Raspberry PI offers limited power to USB devices (and limited number of ports). You'll need some active (powered) **USB Hub** to keep USB devices stable and working (USB Audio, USB Bluettoth and optional USB WiFi).
    Google to learn more, it's a very common topic when using Raspberry PI.
  24. @oleq oleq revised this gist Jan 31, 2016. 2 changed files with 72 additions and 55 deletions.
    52 changes: 26 additions & 26 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -1,47 +1,47 @@
    #!/bin/bash
    # The original script: http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html.

    # Find the right sink with `pactl list sources short`.
    PA_SINK="alsa_output.0.analog-stereo"
    LOG_FILE=/var/log/a2dp-autoconnect
    MAC=$(echo "$NAME" | sed 's/:/_/g' | sed 's/\"//g')
    BT_MAC=$(echo "$NAME" | sed 's/:/_/g' | sed 's/\"//g')
    BT_USER=pi

    function log {
    echo "[$(date)]: $*" >> $LOG_FILE
    echo "[$(date)]: $*" >> /var/log/a2dp-autoconnect
    }

    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
    log "Validated new source: $1"
    log "$1"
    fi
    # 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
    log "Validated new source: $1."
    log "$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%
    # 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
    # Connect source to sink
    sudo -u "$BT_USER" pactl load-module module-loopback \
    source="$1" sink="$PA_SINK" rate=44100 adjust_time=0
    }

    log "Change for device $MAC detected, running $ACTION"
    log "Change for device $BT_MAC detected, running $ACTION."

    if [ "$ACTION" = "add" ]
    then
    incoming=bluez_source."$MAC"
    if [ ! -z $(checkSource "$incoming") ] ; then
    connect "$incoming"
    setVolume
    else
    log "Source $incoming invalid or some source already running."
    fi
    incoming=bluez_source."$BT_MAC"
    if [ ! -z $(checkSource "$incoming") ] ; then
    connect "$incoming"
    setVolume
    else
    log "Source $incoming invalid or some source already running."
    fi
    fi
    75 changes: 46 additions & 29 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,24 @@
    ## What is this all about?

    This tutorial will convert your Raspberry PI into a Bluetooth receiver, which plays audio wirelessly through connected
    speakers:
    This tutorial will convert your Raspberry PI into a Bluetooth receiver, which plays audio wirelessly through connected speakers:

    ```
    Audio source like iPhone -((( wireless transmission )))-> Raspberry PI -> USB Audio -> Speakers
    Audio source (i.e. smartphone) -((( wireless channel )))-> Raspberry PI -> USB Audio -> Speakers
    ```

    It works like a regular car audio system with Bluetooth but can be used anywhere and supports multiple
    audio sources.
    It works like a regular car audio system with Bluetooth but can be used anywhere and supports multiple audio sources.

    ## Initial setup

    I'm using Raspberry PI 1 Model B, running Raspbian Jessie. Make the system is up–to–date:
    I'm using Raspberry PI 1 Model B, running Raspbian Jessie. Make sure the system is up–to–date first:

    ```
    sudo apt-get update
    sudo apt-get upgrade
    ```

    **Note:** It usually takes a while. Get some tee and sandwiches.

    Then install required packages:

    http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/?ALLSTEPS
    @@ -35,22 +35,6 @@ sudo usermod -a -G lp pi

    ## Obtaining hardware

    I used external USB audio interface **Creative Sound Blaster Play**. The on–board audio produces low–quality, noisy output,
    so I decided to use something better.

    As for Bluetooth dongle, I used **Digitus Tiny USB-Adapter**, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.

    **Note**: I used another dongle (different manufacturer) also discovered as `Cambridge Silicon Radio...`
    but unable to stream audio. So beware, because different manufacturers use the same hardware in a different way.
    Or pretend to use the same hardware for some (compatibility?) reasons. This way or another, if you get garbled audio
    or no audio at all but everything else is alright, then try another dongle – it's cheap.

    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying recommendations. Trial and
    error is another option, since most devices cost below $10.

    **Note**: Raspberry PI offers limited power to USB devices (and limited number of ports). You'll need some active (powered) **USB Hub** to keep USB devices stable and working (USB Audio, USB Bluettoth and optional USB WiFi).
    Google to learn more, it's a very common hardware problem with Raspberry PI.

    ```
    pi@raspberrypi:~ $ lsusb
    ...
    @@ -60,9 +44,26 @@ Bus 001 Device 012: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (
    ...
    ```

    ## Audio

    The on–board audio produces low–quality, noisy output, so I decided to use something better. I used external USB **Creative Sound Blaster Play!** interface.

    ## Bluetooth

    As for Bluetooth dongle, I used **Digitus Tiny USB-Adapter**, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.

    **Note**: I used another dongle (different manufacturer) also discovered as `Cambridge Silicon Radio` but unable to stream audio. So beware, because different manufacturers use the same hardware in a different way. Or they pretend to use the same hardware for some (compatibility?) reasons. This way or another, if you get garbled audio or no audio at all but everything else is alright, don't worry, just try another dongle – it's cheap.

    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying recommendations. Trial and error is another option, since most devices cost below $10.

    ## USB Hub

    Raspberry PI offers limited power to USB devices (and limited number of ports). You'll need some active (powered) **USB Hub** to keep USB devices stable and working (USB Audio, USB Bluettoth and optional USB WiFi).
    Google to learn more, it's a very common topic when using Raspberry PI.

    ## Setup PulseAudio

    Use the following configuration to get most of PulseAudio.
    Use the following configuration to get most of PulseAudio:

    http://www.crazy-audio.com/2014/09/pulseaudio-on-the-raspbery-pi/
    ```
    @@ -112,7 +113,7 @@ options snd_bcm2835 index=1
    options snd slots=snd-usb-audio,snd-bcm2835
    ```

    Reboot the device:
    Reboot PI:

    ```
    sudo reboot
    @@ -129,13 +130,26 @@ pi@raspberrypi:~ $ cat /proc/asound/modules

    ## Setup Bluetooth

    Make sure Bluetooth audio is working and discovered as a car audio system

    ```
    pi@raspberrypi:~ $ cat /etc/bluetooth/audio.conf
    [General]
    Class = 0x20041C
    Enable = Source,Sink,Media,Socket
    ```

    I'm not quite sure if the following is also needed. But it doesn't hurt:

    ```
    pi@raspberrypi:~ $ cat /etc/bluetooth/main.conf
    [General]
    ...
    Name = raspberrypi
    Class = 0x20041C
    ...
    ```

    Reboot PI:

    ```
    @@ -168,22 +182,25 @@ trust XX:XX:XX:XX:XX:XX

    ## Setup auto connecting

    Given that the phone is already paired and connected to PI, run the following:

    ```
    pi@raspberrypi:~ $ pactl list sources short
    0 alsa_output.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 48000Hz IDLE
    1 alsa_input.0.analog-mono module-alsa-card.c s16le 1ch 48000Hz IDLE
    4 bluez_source.A8_88_08_11_AB_4B module-bluez5-device.c s16le 2ch 44100Hz RUNNING
    ```

    and

    ```
    pi@raspberrypi:~ $ pactl list sinks short
    0 alsa_output.0.analog-stereo module-alsa-card.c s16le 2ch 48000Hz RUNNING
    ```

    The whole trick to make a2dp work is to redirect a right source (like iPhone) to the right sink (ALSA).

    In this example it would be `bluez_source.A8_88_08_11_AB_4B -> alsa_output.0.analog-stereo`, given that
    the phone is already paired and connected to PI.
    The whole trick to make a2dp work is to redirect a right **source** (like smartphone) to the right **sink** (ALSA). In the above case, it would be `bluez_source.A8_88_08_11_AB_4B -> alsa_output.0.analog-stereo`.

    Add udev rule which executes `a2dp-autoconnect` script each time a bluetooth device is connected
    Each time a Bluetooth audio source connects, the new redirection must be made. Add udev rule which executes `a2dp-autoconnect` script each time a bluetooth device is connected

    ```
    pi@raspberrypi:~ $ sudo cat /etc/udev/rules.d/99-input.rules
  25. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 17 additions and 5 deletions.
    22 changes: 17 additions & 5 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,25 @@
    ## Initial setup
    ## What is this all about?

    This tutorial will convert your Raspberry PI into a Bluetooth receiver, which plays audio wirelessly through connected
    speakers:

    Raspberry PI running Raspbian Jessie.
    ```
    Audio source like iPhone -((( wireless transmission )))-> Raspberry PI -> USB Audio -> Speakers
    ```

    It works like a regular car audio system with Bluetooth but can be used anywhere and supports multiple
    audio sources.

    ## Initial setup

    Make sure PI is up–to–date:
    I'm using Raspberry PI 1 Model B, running Raspbian Jessie. Make the system is up–to–date:

    ```
    sudo apt-get update
    sudo apt-get upgrade
    ```

    Install required packages:
    Then install required packages:

    http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/?ALLSTEPS

    @@ -28,7 +38,7 @@ sudo usermod -a -G lp pi
    I used external USB audio interface **Creative Sound Blaster Play**. The on–board audio produces low–quality, noisy output,
    so I decided to use something better.

    As for Bluetooth dongle, I used Digitus Tiny USB-Adapter, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.
    As for Bluetooth dongle, I used **Digitus Tiny USB-Adapter**, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.

    **Note**: I used another dongle (different manufacturer) also discovered as `Cambridge Silicon Radio...`
    but unable to stream audio. So beware, because different manufacturers use the same hardware in a different way.
    @@ -38,6 +48,8 @@ or no audio at all but everything else is alright, then try another dongle – i
    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying recommendations. Trial and
    error is another option, since most devices cost below $10.

    **Note**: Raspberry PI offers limited power to USB devices (and limited number of ports). You'll need some active (powered) **USB Hub** to keep USB devices stable and working (USB Audio, USB Bluettoth and optional USB WiFi).
    Google to learn more, it's a very common hardware problem with Raspberry PI.

    ```
    pi@raspberrypi:~ $ lsusb
  26. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -50,10 +50,13 @@ Bus 001 Device 012: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (

    ## Setup PulseAudio

    Use the following configuration to get most of PulseAudio.

    http://www.crazy-audio.com/2014/09/pulseaudio-on-the-raspbery-pi/
    ```
    pi@raspberrypi:~ $ cat /etc/pulse/daemon.conf
    ...
    resample-method=ffmpeg
    enable-remixing = no
    enable-lfe-remixing = no
    default-sample-format = s32le
    @@ -70,6 +73,8 @@ Reboot PI:
    sudo reboot
    ```

    **Note:** PA is pretty CPU–consuming. With the following configuration it uses ~30% of my PI's CPU.
    So if you expect PI to do something else beside A2DP and avoid sound glitches, reasearch different `resample-method`.

    ## Configure USB Audio

  27. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -32,11 +32,11 @@ As for Bluetooth dongle, I used Digitus Tiny USB-Adapter, which is discovered as

    **Note**: I used another dongle (different manufacturer) also discovered as `Cambridge Silicon Radio...`
    but unable to stream audio. So beware, because different manufacturers use the same hardware in a different way.
    Or pretend to use the same hardware for some (compatibility?) reason. This way or another, if you get garbled audio
    Or pretend to use the same hardware for some (compatibility?) reasons. This way or another, if you get garbled audio
    or no audio at all but everything else is alright, then try another dongle – it's cheap.

    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying ideas. Trial and
    error is another option, most devices costs below $10.
    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying recommendations. Trial and
    error is another option, since most devices cost below $10.


    ```
  28. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -17,19 +17,23 @@ http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blue
    sudo apt-get install alsa-utils bluez bluez-tools pulseaudio-module-bluetooth python-gobject python-gobject-2
    ```

    Not quite sure it's really needed (?):
    Not quite sure it's really needed (?), but it doesn't hurt:

    ```
    sudo usermod -a -G lp pi
    ```

    ## Obtaining hardware

    I used external USB audio interface **Creative Sound Blaster Play**. The on–board audio produces low–quality, noisy output,
    so I decided to use something better.

    As for Bluetooth dongle, I used Digitus Tiny USB-Adapter, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.

    **Note**: I used another dongle (different manufacturer), which was also discovered as `Cambridge Silicon Radio...`
    but unable to stream audio. So it's not only about the chip itself but about manufacturers.
    **Note**: I used another dongle (different manufacturer) also discovered as `Cambridge Silicon Radio...`
    but unable to stream audio. So beware, because different manufacturers use the same hardware in a different way.
    Or pretend to use the same hardware for some (compatibility?) reason. This way or another, if you get garbled audio
    or no audio at all but everything else is alright, then try another dongle – it's cheap.

    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying ideas. Trial and
    error is another option, most devices costs below $10.
  29. @oleq oleq revised this gist Jan 31, 2016. 1 changed file with 64 additions and 41 deletions.
    105 changes: 64 additions & 41 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -44,41 +44,6 @@ Bus 001 Device 012: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (
    ...
    ```

    ## Setup Bluetooth

    ```
    pi@raspberrypi:~ $ cat /etc/bluetooth/audio.conf
    [General]
    Class = 0x20041C
    Enable = Source,Sink,Media,Socket
    ```

    ```
    sudo reboot
    ```

    ```
    bluetoothctl
    ```

    ```
    [bluetooth]# list
    Controller 00:1A:7D:DA:71:06 raspberrypi [default]
    ```

    ```
    [bluetooth]# agent on
    [bluetooth]# default-agent
    [bluetooth]# discoverable on
    [bluetooth]# scan on
    ```

    For each device
    ```
    pair XX:XX:XX:XX:XX:XX
    trust XX:XX:XX:XX:XX:XX
    ```

    ## Setup PulseAudio

    http://www.crazy-audio.com/2014/09/pulseaudio-on-the-raspbery-pi/
    @@ -95,16 +60,27 @@ exit-idle-time = -1
    ...
    ```

    ## Configure Sound Blaster Live USB
    Reboot PI:

    http://raspberrypi.stackexchange.com/questions/40831/how-do-i-configure-my-sound-for-jasper-on-raspbian-jessie
    ```
    sudo reboot
    ```


    ## Configure USB Audio

    The problem is that on–board audio ouput is prefered over USB audio interface:

    ```
    pi@raspberrypi:~ $ cat /proc/asound/modules
    0 snd_bcm2835
    1 snd_usb_audio
    ```

    Some configuration does the trick and from now on RPI uses USB Audio as default:

    http://raspberrypi.stackexchange.com/questions/40831/how-do-i-configure-my-sound-for-jasper-on-raspbian-jessie

    ```
    pi@raspberrypi:~ $ cat /etc/modprobe.d/alsa-base.conf
    # This sets the index value of the cards but doesn't reorder.
    @@ -115,19 +91,61 @@ options snd_bcm2835 index=1
    options snd slots=snd-usb-audio,snd-bcm2835
    ```

    Reboot the device:

    ```
    sudo reboot
    ```

    See `snd_usb_audio` as a preferred output:

    ```
    pi@raspberrypi:~ $ cat /proc/asound/modules
    0 snd_usb_audio
    1 snd_bcm2835
    ```

    ## Setup auto connecting

    The trick is to redirect a source (like iPhone) to sink (ALSA):
    ## Setup Bluetooth

    ```
    pi@raspberrypi:~ $ cat /etc/bluetooth/audio.conf
    [General]
    Class = 0x20041C
    Enable = Source,Sink,Media,Socket
    ```

    Reboot PI:

    ```
    sudo reboot
    ```

    Pair devices (phones, tablets, PCs) with PI:

    ```
    bluetoothctl
    ```

    ```
    [bluetooth]# list
    Controller 00:1A:7D:DA:71:06 raspberrypi [default]
    ```

    ```
    [bluetooth]# agent on
    [bluetooth]# default-agent
    [bluetooth]# discoverable on
    [bluetooth]# scan on
    ```

    Then, for each device:
    ```
    pair XX:XX:XX:XX:XX:XX
    trust XX:XX:XX:XX:XX:XX
    ```

    ## Setup auto connecting

    ```
    pi@raspberrypi:~ $ pactl list sources short
    @@ -139,6 +157,11 @@ pi@raspberrypi:~ $ pactl list sinks short
    0 alsa_output.0.analog-stereo module-alsa-card.c s16le 2ch 48000Hz RUNNING
    ```

    The whole trick to make a2dp work is to redirect a right source (like iPhone) to the right sink (ALSA).

    In this example it would be `bluez_source.A8_88_08_11_AB_4B -> alsa_output.0.analog-stereo`, given that
    the phone is already paired and connected to PI.

    Add udev rule which executes `a2dp-autoconnect` script each time a bluetooth device is connected

    ```
    @@ -148,13 +171,13 @@ KERNEL=="input[0-9]*", RUN+="/home/pi/a2dp-autoconnect"

    The script is an extended version of http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html. It redirects a new bluetooth device to the right sink and sets the right volume level.

    I located it in `/home/pi/a2dp-autoconnect` then set it executable:
    I located it in `/home/pi/a2dp-autoconnect`, then set it executable:

    ```
    pi@raspberrypi:~ $ chmod +x a2dp-autoconnect
    ```

    to observe connection log "live":
    To observe connection log "live":

    ```
    pi@raspberrypi:~ $ tail -f /var/log/a2dp-autoconnect
  30. @oleq oleq created this gist Jan 31, 2016.
    47 changes: 47 additions & 0 deletions a2dp-autoconnect
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/bash

    PA_SINK="alsa_output.0.analog-stereo"
    LOG_FILE=/var/log/a2dp-autoconnect
    MAC=$(echo "$NAME" | sed 's/:/_/g' | sed 's/\"//g')
    BT_USER=pi

    function log {
    echo "[$(date)]: $*" >> $LOG_FILE
    }

    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
    log "Validated new source: $1"
    log "$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
    }

    log "Change for device $MAC detected, running $ACTION"

    if [ "$ACTION" = "add" ]
    then
    incoming=bluez_source."$MAC"
    if [ ! -z $(checkSource "$incoming") ] ; then
    connect "$incoming"
    setVolume
    else
    log "Source $incoming invalid or some source already running."
    fi
    fi
    161 changes: 161 additions & 0 deletions howto.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,161 @@
    ## Initial setup

    Raspberry PI running Raspbian Jessie.

    Make sure PI is up–to–date:

    ```
    sudo apt-get update
    sudo apt-get upgrade
    ```

    Install required packages:

    http://www.instructables.com/id/Enhance-your-Raspberry-Pi-media-center-with-Blueto/?ALLSTEPS

    ```
    sudo apt-get install alsa-utils bluez bluez-tools pulseaudio-module-bluetooth python-gobject python-gobject-2
    ```

    Not quite sure it's really needed (?):

    ```
    sudo usermod -a -G lp pi
    ```

    I used external USB audio interface **Creative Sound Blaster Play**. The on–board audio produces low–quality, noisy output,
    so I decided to use something better.

    As for Bluetooth dongle, I used Digitus Tiny USB-Adapter, which is discovered as `Cambridge Silicon Radio, Ltd Bluetooth Dongle`.

    **Note**: I used another dongle (different manufacturer), which was also discovered as `Cambridge Silicon Radio...`
    but unable to stream audio. So it's not only about the chip itself but about manufacturers.

    See [RPi USB Bluetooth adapters](http://elinux.org/RPi_USB_Bluetooth_adapters) for buying ideas. Trial and
    error is another option, most devices costs below $10.


    ```
    pi@raspberrypi:~ $ lsusb
    ...
    Bus 001 Device 008: ID 041e:30d3 Creative Technology, Ltd Sound Blaster Play!
    ...
    Bus 001 Device 012: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
    ...
    ```

    ## Setup Bluetooth

    ```
    pi@raspberrypi:~ $ cat /etc/bluetooth/audio.conf
    [General]
    Class = 0x20041C
    Enable = Source,Sink,Media,Socket
    ```

    ```
    sudo reboot
    ```

    ```
    bluetoothctl
    ```

    ```
    [bluetooth]# list
    Controller 00:1A:7D:DA:71:06 raspberrypi [default]
    ```

    ```
    [bluetooth]# agent on
    [bluetooth]# default-agent
    [bluetooth]# discoverable on
    [bluetooth]# scan on
    ```

    For each device
    ```
    pair XX:XX:XX:XX:XX:XX
    trust XX:XX:XX:XX:XX:XX
    ```

    ## Setup PulseAudio

    http://www.crazy-audio.com/2014/09/pulseaudio-on-the-raspbery-pi/
    ```
    pi@raspberrypi:~ $ cat /etc/pulse/daemon.conf
    ...
    enable-remixing = no
    enable-lfe-remixing = no
    default-sample-format = s32le
    default-sample-rate = 192000
    alternate-sample-rate = 176000
    default-sample-channels = 2
    exit-idle-time = -1
    ...
    ```

    ## Configure Sound Blaster Live USB

    http://raspberrypi.stackexchange.com/questions/40831/how-do-i-configure-my-sound-for-jasper-on-raspbian-jessie

    ```
    pi@raspberrypi:~ $ cat /proc/asound/modules
    0 snd_bcm2835
    1 snd_usb_audio
    ```

    ```
    pi@raspberrypi:~ $ cat /etc/modprobe.d/alsa-base.conf
    # This sets the index value of the cards but doesn't reorder.
    options snd_usb_audio index=0
    options snd_bcm2835 index=1
    # Does the reordering.
    options snd slots=snd-usb-audio,snd-bcm2835
    ```

    ```
    sudo reboot
    ```

    ```
    pi@raspberrypi:~ $ cat /proc/asound/modules
    0 snd_usb_audio
    1 snd_bcm2835
    ```

    ## Setup auto connecting

    The trick is to redirect a source (like iPhone) to sink (ALSA):

    ```
    pi@raspberrypi:~ $ pactl list sources short
    0 alsa_output.0.analog-stereo.monitor module-alsa-card.c s16le 2ch 48000Hz IDLE
    1 alsa_input.0.analog-mono module-alsa-card.c s16le 1ch 48000Hz IDLE
    4 bluez_source.A8_88_08_11_AB_4B module-bluez5-device.c s16le 2ch 44100Hz RUNNING
    pi@raspberrypi:~ $ pactl list sinks short
    0 alsa_output.0.analog-stereo module-alsa-card.c s16le 2ch 48000Hz RUNNING
    ```

    Add udev rule which executes `a2dp-autoconnect` script each time a bluetooth device is connected

    ```
    pi@raspberrypi:~ $ sudo cat /etc/udev/rules.d/99-input.rules
    KERNEL=="input[0-9]*", RUN+="/home/pi/a2dp-autoconnect"
    ```

    The script is an extended version of http://blog.mrverrall.co.uk/2013/01/raspberry-pi-a2dp-bluetooth-audio.html. It redirects a new bluetooth device to the right sink and sets the right volume level.

    I located it in `/home/pi/a2dp-autoconnect` then set it executable:

    ```
    pi@raspberrypi:~ $ chmod +x a2dp-autoconnect
    ```

    to observe connection log "live":

    ```
    pi@raspberrypi:~ $ tail -f /var/log/a2dp-autoconnect
    ```