Tuesday, April 20, 2010

Bluetooth Headphones with Stepmania

Maaaaaybe, you play Stepmania. Lovely Stepmania. However, you can't bother folks with the driving thumping beat, so you want to wear headphones. Lovely headphones. But wait! You can't wear wired headphones while dancing! So you want to wear wireless headphones. Bluetooth headphones even. Lovely bluetooth headphones. So how is such a thing done?

To begin, we start with a working Debian system, because that is god's chosen distribution. Lovely Debian.

You need some bluetooth packages, specifically:
bluez
bluez-alsa
libbluetooth3
You need to have a bluetooth adapter in your system, and you'll need to know the bluetooth adapter ID:
root@thorn 1> hcitool dev
Devices:
hci0 00:02:76:09:69:4A
We'll need that bluetooth adapter ID a little later.

And, of course, you need some bluetooth headphones: I'm using Motorola S9-HD headphones, others should work.

Ok, make sure the bluetooth daemon is running:
root@thorn 2> ps -ef | grep bluetoothd
root 5342 5263 0 00:53 pts/16 00:00:00 grep bluetoothd
And we turn on the headphones, and put them in pairing mode. For the S9-HD headphones, they automatically go into pairing mode when turned on (and unpaired), indicated by the LED remaining a solid blue.

First, to confirm that the bluetooth adapter can see the headphones, we query the list of pairable devices that you can see with:
root@thorn 3> hcitool scan
Scanning ...
00:0D:FD:2D:51:88 Motorola S9-HD
This means your computer can see the headphones, and that the bluetooth ID of those phones is 00:0D:FD:2D:51:88. Yay! Then we connect them like this:
root@thorn 4> hcitool cc 00:0D:FD:2D:51:88
Can't create connection: Input/output error
Oh noes! Something is wrong! The reason is that you need to tell your bluetooth adapter what the pin code is. One way is to run the bluetooth-agent, with the (common) pin code of 0000:
root@thorn 5> bluetooth-agent 0000
With this, when bluetoothd attempts to connect, it will use that pin code. However, it's a pain in the ass to have to run that, so instead we stick that code into a configuration file. The configuration file is:
/var/lib/bluetooth/ADAPTERID/pincodes
and the format is:
DEVICEID PIN
So, if our adapter bluetooth ID (shown with the hcitool dev command) is 00:02:76:09:69:4A, and the device bluetooth ID (shown with the hictool scan command) is 00:0D:FD:2D:51:88, then the file to store the pin code is:
/var/lib/bluetooth/00:02:76:09:69:4A/pincodes
and the line in that file showing the pin for the headphones is;
00:0D:FD:2D:51:88 0000
Once that is set up, we should be able to connect:
root@thorn 6> hcitool cc 00:0D:FD:2D:51:88
Hmm, I don't know how to query connected devices though. hcitool con seems like it should show something, but it doesn't. Hmm.

Anyway, now that we've connected our bluetooth device, we add a bluetooth clause to the ALSA config file, ~/.asoundrc:
pcm.bluetooth {
type bluetooth
device 00:0D:FD:2D:51:88
}
Make sure you change the device bluetooth ID as needed.

Now, you can test this out with mplayer:
mplayer -ao alsa:device=bluetooth foo.mp3
Ok, finally, we modify the Stepmania config file, ~/.stepmania/Save/preferences.ini, changing the SoundDevice line:
SoundDevice=bluetooth
Once that is done, running Stepmania should bring the sound through the headphone. Violet!

Blog Archive