Ubuntu – use the computer as an A2DP receiver / bluetooth speaker

bluetooth

First, the problem: I'm using a Cowon MP3 player as my main music player with basic earplugs.

It offers A2DP & I'd like to have my netbook (running UNE 10.04) act as receiver.

Some resources on the web about a2dp, but most are out of date:

Best Answer

To use your Ubuntu machine as an a2dp Bluetooth device, you must first configure it to register as an "a2dp sink" endpoint.

The bluez package in Ubuntu (10.04 and above) includes a utility called sdptool that can be used to check whether a Bluetooth device is configured as an a2dp sink or not. Here is the output from sdptool run against my Bluetooth headset (the address is taking from the "Bluetooth settings" dialog in the gnome-control-center):

$ sdptool search --bdaddr 00:18:16:3A:3B:D4 a2snk
Searching for a2snk on 00:18:16:3A:3B:D4 ...
Service RecHandle: 0x10002
Service Class ID List:
  "Audio Sink" (0x110b)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 25
  "AVDTP" (0x0019)
    uint16: 0x102
Profile Descriptor List:
  "Advanced Audio" (0x110d)
    Version: 0x0102

$

and here is the output when run against my local Ubuntu machine:

$ sdptool search --bdaddr local a2snk
Searching for a2snk on FF:FF:FF:00:00:00 ...
$

This shows that the Ubuntu machine is not advertising itself as an a2dp sink. It is advertising itself as an a2dp source however:

$ sdptool search --bdaddr local a2src
Searching for a2src on FF:FF:FF:00:00:00 ...
Service Name: Audio Source
Service RecHandle: 0x10003
Service Class ID List:
  "Audio Source" (0x110a)
Protocol Descriptor List:
  "L2CAP" (0x0100)
    PSM: 25
  "AVDTP" (0x0019)
    uint16: 0x102
Profile Descriptor List:
  "Advanced Audio" (0x110d)
    Version: 0x0102

This enables the Ubuntu machine to pair correctly with the headset as an audio source, but it does not enable using the Ubuntu machine as an output (sink) for Bluetooth audio.

If you edit /etc/bluetooth/audio.conf, you can enable a2dp sink support by adding this line underneath the [General] section:

  Enable=Source

This is both counterintuitively named--since what we're adding here is Bluetooth sink support, not source support--and in contradiction with the comment in this file that claims all implemented services are enabled by default. :(

After making this change, you will need to restart bluetoothd by running sudo service bluetooth restart.

If you have previously paired your Android device and Ubuntu computer while trying to get this work, you will need to delete the pairing on both sides and re-pair them in order to get Android to recognize Ubuntu as an available audio device.

Once you've done this, the Android device should show up as an input device under PulseAudio. If PulseAudio does not detect the new Bluetooth audio source, you may need to install and load the Bluetooth module, from the command line:

 sudo apt-get install pulseaudio-module-bluetooth
 pactl load-module module-bluetooth-discover

Then you need to tell PulseAudio to route this audio input to your output/sink (such as your speakers, or a Bluetooth headset) using a loopback connection (a straight line from a source to a sink).

Later versions of PulseAudio may have module-bluetooth-policy included and it may already have set up a loopback device for you, but this does not seem to be the most common case.

A semi-automatic way to set up the loopback connection - if you also have the pavucontrol GUI program installed - is to simply load the loopback module and configure it using pavucontrol, since PulseAudio will remember the settings. Loading the module is done from the command using pactl:

 pactl load-module module-loopback

Don't fear if you don't hear anything yet, or if you get strange feedback effects, we need to tell the newly created device which source to get input from and which sink to send the output to first.

Open pavucontrol and open its Configuration tab. Make sure your Bluetooth device shows up here (after pairing with it using blueman-manager or another Bluetooth tool) and the profile is set to A2DP. Switch to the Input devices tab and make sure your device shows up here as well and is not muted. Now switch to the Recording tab and make the newly created loopback connection use your device as a source with the select box next to the mute button. Switch to the Playback tab to select the sink the loopback connection should use in the same way. If the loopback device does not show in the tabs, make sure all streams are displayed using the selectbox at the bottom of each tab.

You should now hear the audio from your device, if it's playing, in your speakers, or whichever output sink you selected. When your device is disconnected the loopback device will fallback to an available sink and source, which may not be desirable, so make sure you mute the loopback device until you need it. The loopback connection will restore the same sink/source the next time the same Bluetooth device is connected thanks to the module-*-restore modules.

If that doesn't work, or you don't have pavucontrol installed, you may instead set up the loopback using the following method:

 pactl load-module module-loopback source_dont_move=yes source=$BTSOURCE sink=$SINK

(Replace $BTSOURCE with the source name for your Bluetooth device as seen by PulseAudio, e.g. bluez_source.14_DA_E9_2A_D7_57; and replace $SINK with the name of the PulseAudio output you want to send the audio stream to, e.g: alsa_output.pci-0000_00_1b.0.analog-stereo. You may leave out the sink argument entirely and have it fallback to an active sink, and change it later via pavucontrol.)

  • You can find $SINK with pactl list sinks, it's shown after Name:
  • Similarly you can see the $BTSOURCE with pactl list sources
  • The source_dont_move argument prevents the loopback connection from falling back to another audio source when the Bluetooth device is disconnected. It is instead removed and you'll have to set it up again next time.

Here's what an example one would look like (Remember to replace the : with _ in the Bluetooth address!):

pactl load-module module-loopback source_dont_move=yes source=bluez_source.14_DA_E9_2A_D7_57 sink=alsa_output.pci-0000_00_14.2.analog-stereo

If loading the fallback module fails, try removing the source_dont_move=yes argument, it was made available first in version 1.0. Then be careful however to remove this loopback connection before you drop this audio, or if for instance your laptop microphone is unmuted, you may get some very bad feedback. To drop this loopback connection manually when you're done, run:

pactl unload-module $(pactl list short modules | grep "loopback.*$BTSOURCE" | cut -f1)

Again, replace $BTSOURCE with the name for the PulseAudio source that refers to your Bluetooth device. You may also unload the module using the id returned by the load-module command:

$ pactl load-module module-loopback source_dont_move=yes source=bluez_source.14_DA_E9_2A_D7_57 sink=alsa_output.pci-0000_00_14.2.analog-stereo
15
$ pactl unload-module 15

References:

  • Blog post outlining some of the details on how this is being fixed in Ubuntu.
Related Question