• Home
  • Blogger
  • Github
  • Travel
  • The Tank Project
skip to main | skip to sidebar

Steven Occhipinti

A braindump.

Bluetooth audio streaming from phone to Ubuntu

I was toying with the idea of making my own "Car Computer" and one of the crucial parts is a Bluetooth connection, but not for P2P file transfer (although that could be useful), I would want this computer to appear the same way a Bluetooth stereo or headset does, so once connected it will stream the audio from my phone through the car speakers. Turns out it's quite easy with Ubuntu :)

PulseAudio is pretty awesome!
First step is ensure Pulse has the Bluetooth module loaded (mine did by default), you can check this by looking at this file /etc/pulse/default.pa

The next step is to configure the Bluetooth daemon to enable "Source" (and/or "Gateway" mode).
To do this, I added the following line to /etc/bluetooth/audio.conf:

Enable=Gateway,Source,Socket

Then restart the Bluetooth daemon:
$> sudo service bluetooth restart


Now you'll have to pair the computer with the phone as per normal.

Once the devices are paired, an "Audio Source" connection needs to be created.
For this, the easiest way is via DBus, but so we don't have to write a script and easy way to test this in an ad-hoc manner is to use a tool called 'd-feet', which is available in the Ubuntu repos.
$> sudo apt-get install d-feet

Using d-feet, navigate to the system bus and find the org.bluez key.
In here, there will be another key that has a device number in it. (for me this was /org/bluez/1002/hci0/dev_xx_xx_xx_xx_xx_xx)
Under this key, execute org.bluez.AudioSource > methods > Connect() without any parameters and your phone should now indicate that it is connected to the computer as an audio device.

At this point, I started playing some music so I could tell when it was working, but you probably won't be able to hear anything just yet.
That is because Pulse does not automatically send every source to every output - that could get messy!
To configure pulse to send the audio from the Bluetooth connection to your speakers you need to determine the name of the audio source and the name of the "sink" (or output) and join them together.
You can do this with the PulseAudio shell like this:

$> pacmd
>>> list-sources
>>> list-sinks
>>> load-module module-loopback source=bluez_source.xxxx sink=alsa_output.pci-xxxx

(Replace the underlined parts with the names from the preceding two commands)

Now all audio from your phone should be streaming through your computer.
DBus makes it really simple to do this programmatically, so it could easily be (mostly) automated and even controlled by a nice touch friendly GUI to pick the source device, etc..

I thought I would write up a quick post on this because when I finally got my music streaming through my laptop, it was exciting to know that I could actually use this to make a car computer with bluetooth functionality quite easily.
I used "AudioSource" in this example, but the other fun option is the "HandsFreeGateway", where instead of streaming stereo sound for music, it will also connect the microphone from the computer to the phone to be used in-call, etc.

The resources that helped me with this where:
http://jprvita.wordpress.com/2009/
http://ubuntuforums.org/showthread.php?t=1464189
http://ubuntuforums.org/showthread.php?t=1904447
Posted by Steve at 22:27
Email This BlogThis! Share to X Share to Facebook

5 comments:

pablofabregat said...

Beautiful, I will try the handsfreegateway later today, but the AudioSource worked like a charm.
Ubuntu 13.04 here :D :D

It's a little uncomfortable to do this every time I boot the laptop, but the result is fun :D

Thanks for this!!

1 May 2013 at 03:58
Steve said...

No problem, glad it helped.

I used d-feet because I was just playing around, but if you want a more permanent solution, I would recommend writing a script to do it.
Most languages have a DBus library and you could even get that script to run at startup.

Good luck.

1 May 2013 at 13:22
Anonymous said...

Hi,
It worked for me but didn't have to use the load-module stuff, the sound was doubled, but I don't have a sterio output, why?
is it because btheadsets are only mono?

20 June 2013 at 22:30
Steve said...

My understanding was that "AudioSource" was Stereo (A2DP) and "HandsFreeGateway" was mono, but I'm not 100% sure.

21 June 2013 at 14:41
Anonymous said...

Well, it didn't work sterio for me :/ and I can't connect it after I rebooted the computer : Method Output: 'GDBus.Error:org.bluez.Error.Failed: Stream setup failed'

22 June 2013 at 23:49

Post a Comment

Newer Post Older Post Home

Blog Archive

  • ►  2013 (4)
    • ►  June (1)
    • ►  May (1)
    • ►  March (1)
    • ►  January (1)
  • ▼  2012 (17)
    • ►  December (1)
    • ▼  October (1)
      • Bluetooth audio streaming from phone to Ubuntu
    • ►  September (2)
    • ►  August (2)
    • ►  July (1)
    • ►  May (2)
    • ►  April (2)
    • ►  March (3)
    • ►  February (3)
  • ►  2011 (33)
    • ►  December (1)
    • ►  November (3)
    • ►  October (3)
    • ►  September (4)
    • ►  August (3)
    • ►  July (4)
    • ►  June (6)
    • ►  May (6)
    • ►  April (3)

Labels

android (5) apache (1) arch linux (1) arduino (1) bash (11) calendar (1) compiz (1) design (1) diff (1) email (1) gimp (1) git (3) github (2) gnome3 (1) Google (2) hacking (1) hardware (4) howto (1) htpc (1) java (1) lamp (1) linux (28) Mac (2) minecraft (2) mysql (1) netduino (1) nfs (1) parallel port (1) patch (1) photography (4) php (1) pidgin (2) printer (1) programming (6) python (1) rails (1) regex (5) review (3) ruby (3) Samsung Galaxy S3 (2) Samsung Series 9 (1) security (1) sed (3) ssh (1) sudo (1) tank (2) Toshiba Portege (1) troubleshooting (1) ubuntu (16) ui (2) unity (2) vim (5) webcam (1) websites (3) xbmc (1) xclip (1) xul (1)

Total Pageviews

Sparkline
 
Copyright (c) 2010 Steven Occhipinti. Designed by Conveyancing
High Deductible Health Insurance, Purchase Beats