Thursday, 22 April 2010

Linux Bluetooth Audio

Recently I've been playing with bluetooth and audio. Thanks in part to finding a supply of very cheep dongles. Here is how I got my Linux computers to talk to a bluetooth audio dog-tag style stereo audio device, this can be used with any bluetooth audio device though including mono hands free kits.



Im using bluez 4.63, the latest available in my distro's ( Arch Linux ) repositories. First we need to find our device, so put the audio device into paring mode. Next we need to scan for our device.




# hcitool scan
Scanning ...
00:00:00:00:00:00 BTS-PHF41


Ok, once we have found the device we need to connect to it, for this we need the device mac address returned from our scan.




# hciconfig hci0 up
# hcitool cc 00:00:00:00:00:00


At this point we will have an open connection to the device, however we are not yet paired, so that's our next step. To do this I'm using one of the utility scripts provided by bluez. This requires Python.




# bluez-simple-agent hci0 00:00:00:00:00:00
RequestPinCOde (/org/bluez/.... )
Enter PIN code: 0000
Release
New Device ( /org/bluez/ .... )


This script will ask for a PIN number, enter one matching your device, given in the documentation that should come with it (0000 in the example above). Now we are all paired up and ready to send audio over to our device. In order to do this we will use ALSA and setup a bluetooth audio configuration. Open up the file ~/.asoundrc in the home folder of your regular user account and add the following to it.




pcm.bluetooth {
type bluetooth
}


That's that, we now have everything we need setup and ready for blutooth audio. However lets go one step more and configure a media player to use our new audio interface. I'm quite fond of MPD so ill show a configuration for that. Edit ~/.mpdconf and scroll down to the audio output section, add the following.




audio_output {
type "alsa"
name "blutooth output"
device "bluetooth"
format "44100:16:2"
mixer_device "defualt"
mixer_control "PCM"
mixer_index "0"
}


Now fire up MPD and enjoy audio with no wires attached ;-) This could also be useful as part of a nice jukebox setup, possibly using PMix from an Android phone as a remote.... sounds like a future project there!