Can I use my computer as an A2DP receiver / bluetooth speaker?

Solution 1:

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.

Solution 2:

I use blueman, installed from Ubuntu software center. Right-clicking on the blueman icon*-> "local services" -> audio-> Check "advanced audio reception" is tipped. Btw I use it to listen in my Ubuntu 11.10 laptop what I play on my android phone.

*The blueman icon is showed in bottom-right corner in gnome shell + ubuntu 11.10, that is my case. I hope this is useful in some way for you.

Solution 3:

It's really simple now in Ubuntu 12.04

Connect your bluetooth device

Search for your bluetooth device and take note of it's source number:

pactl list sources 

Make a loopback for the bluetooth device:

pactl load-module module-loopback source=$SOURCE_NUMBER  

Where $SOURCE_NUMBER is the number you previously took note of.

For example, I entered the following to get audio playback to work:

$ pactl load-module module-loopback source=3  

That's it! I found the solution in the top answer to be a bit complicated, so I simplified it this way (much less typing and copy/pasting). Though my solution will probably break if you remove any audio devices listed before the bluetooth device by pactl list sources.

Also, don't forget to remove the loopback module before disconnecting the bluetooth device:

Take note of the module number of the loopback device you created:

pactl list short modules

Remove the loopback device:

pactl unload-module $MODULE_NUMBER

The previous answers were written prior to 12.04's release, so I'm sure there were some changes. For example, I didn't have to manually enable using bluetooth devices as an audio source (it was enabled by default). However, I do sometimes have to toggle the "Media Audio" profile in my bluetooth settings on my phone (Samsung Captivate running CyanogenMod 9.1.0).

There's probably a way to leverage dbus-monitor by using a shell script to watch for bluetooth A2DP device connections and automatically load a loopback module for it, but that's a bit over my head.

Hopefully we won't need to do this manually any more in Ubuntu 12.10.

Solution 4:

For 13.04, pair your source device with the ubuntu laptop and it just works! Very cool to find that it was that easy now.

Solution 5:

In Ubuntu 16.04 this works out-of-the-box.