Tuesday, 29 January 2013

Using bluetooth audio in Linux

I just spent a while trying to find out how to get my laptop to play audio to a Bluetooth Headset. It seems a lot of the info out there is out of date. Here are some instructions that work with bluez version 4.9X on Ubuntu 12.04.

Firstly make sure you've got the following packages e.g.:
% sudo apt-get install bluez alsa-utils

To do it you need to make sure all the bluetooth kernel modules are loaded - the following command should show the bluetooth modules if they're loaded e.g.:
% lsmod | grep blue
bluetooth 158479  23 bnep,rfcomm,btusb

Then you can check the bluetooth adapter is operational by showing its address info e.g.:
$ hciconfig
hci0:    Type: BR/EDR  Bus: USB
    BD Address: 00:02:7e:16:0D:0a  ACL MTU: 384:8  SCO MTU: 64:8
    UP RUNNING PSCAN ISCAN 
    RX bytes:58444 acl:314 sco:0 events:6846 errors:0
    TX bytes:4821276 acl:17765 sco:0 commands:152 errors:0

Next you need to switch on your headset and put it into discovery mode (e.g. push and hold the power button or something). Then you get your laptop to scan for it e.g.:
% hcitool scan
Scanning ...
    00:0B:E4:9A:15:E4    Nokia BH-214

Then you need to pair with this device as follows - It will ask you for the PIN code for your device (e.g. 0000 or 1111 or something) and then it will add it to the list on your machine (you'll need to replace the bluetooth interface 'hci#' with your one in this part e.g. hci0) e.g.:
% sudo bluez-simple-agent hci# 00:0B:E4:9A:15:E4
RequestPinCode (/org/bluez/9261/hci0/dev_00_0B_E4_9A_15_E4)
Enter PIN Code: 0000
Release
New device (/org/bluez/9261/hci0/dev_00_0B_E4_9A_15_E4)

Now you need to set up the device for use by ALSA sound system - create a file called ~/.asoundrc containing the following (you'll need to replace the bluetooth address with your one in this part) e.g.:
pcm.bluetooth {
   type bluetooth
   device 00:0B:E4:9A:15:E4
}

Finally you can now play audio to your headset:
aplay -D bluetooth myaudio.wav

Or you can select the bluetooth audio device in other audio apps.