Say what?

Kipibenkipod view of the world

Polar Wearlink Bluetooth on Linux

| Comments

Intro

In the preparations for getting my first smart phone, mainly to use it as a heart rate monitor and GPS for my workouts, I opted for buying a bluetooth heart rate chest strap. Until I’ll get new phone, I want to connect the chest strap to my laptop, for logging heart rate when exercising at home.

Searching the net for Linux HRM (heart rate monitor) implementation, I have found an interesting post Listening to a Polar Bluetooth HRM in Linux. The author wrote a Ruby script that listens to the Polar bluetooth device. So I opted to buy the chest strap he uses in the blog post - Polar WearLink+ transmitter with Bluetooth. Pay attention this is not the Polar H7 which is the newer and support Bluetooth 4.0, but have a lot of issues connecting to android devices. I paid 80$ include shipping to Israel.

After getting the Polar strap, I notice problem connecting the device. Searching Google, I found that there are issues with the battery, that drains too fast. Here is the thread about this problem. I took the battery out, and tried to replace it with other batteries, but nothing. My computer couldn’t find the device. Next day I tried again, and replaced the battery again to the original. Then by magic, the computer did found the device. I don’t know how or why this happened, but I’ll keep track of the battery life and post back.

My heart rate on the screen ;-)

In his blog post Daniel created a small program that listen to the heart rate belt, then prints the data to the screen. Here is a sample output of the program, and the column :hr=> shows my heart rate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{:len=>10, :chk=>245, :seq=>9, :status=>209, :hr=>74, :rr=>[793], :time=>2012-10-27 17:21:25 +0200}
{:len=>8, :chk=>247, :seq=>10, :status=>209, :hr=>74, :rr=>[811], :time=>2012-10-27 17:21:26 +0200}
{:len=>10, :chk=>245, :seq=>11, :status=>209, :hr=>73, :rr=>[845], :time=>2012-10-27 17:21:27 +0200}
{:len=>8, :chk=>247, :seq=>12, :status=>209, :hr=>73, :rr=>[816], :time=>2012-10-27 17:21:29 +0200}
{:len=>10, :chk=>245, :seq=>13, :status=>209, :hr=>73, :rr=>[810], :time=>2012-10-27 17:21:30 +0200}
{:len=>8, :chk=>247, :seq=>14, :status=>209, :hr=>74, :rr=>[800], :time=>2012-10-27 17:21:31 +0200}
{:len=>10, :chk=>245, :seq=>15, :status=>209, :hr=>74, :rr=>[779], :time=>2012-10-27 17:21:32 +0200}
{:len=>10, :chk=>245, :seq=>0, :status=>209, :hr=>75, :rr=>[776], :time=>2012-10-27 17:21:34 +0200}
{:len=>8, :chk=>247, :seq=>1, :status=>209, :hr=>76, :rr=>[769], :time=>2012-10-27 17:21:36 +0200}
{:len=>10, :chk=>245, :seq=>2, :status=>209, :hr=>75, :rr=>[833], :time=>2012-10-27 17:21:36 +0200}
{:len=>8, :chk=>247, :seq=>3, :status=>209, :hr=>74, :rr=>[837], :time=>2012-10-27 17:21:38 +0200}
{:len=>10, :chk=>245, :seq=>4, :status=>209, :hr=>75, :rr=>[800], :time=>2012-10-27 17:21:39 +0200}
{:len=>10, :chk=>245, :seq=>5, :status=>209, :hr=>75, :rr=>[783], :time=>2012-10-27 17:21:40 +0200}
{:len=>8, :chk=>247, :seq=>6, :status=>209, :hr=>75, :rr=>[814], :time=>2012-10-27 17:21:41 +0200}

Few steps to get it working

Compile kernel Bluetooth drivers

make menuconfig

and in Networking support -> Bluetooth subsystem support (NEW) I just marked everything as module. After you understand what driver you need, you can unmark the drivers that are not needed by your system. This procedure is for Gentoo users, or people that use custom kernels. For other OSs Bluetooth is usually provided with the standard kernel.

Install software

You will need to install bluez for handling the Bluetooth service:

emerge -av net-wireless/bluez

Next, if you have Broadcom BCM203x and STLC2300 Bluetooth chips, you should install their firmware:

emerge -av net-wireless/bluez-firmware 

For debugging the connection you can use hcidump similar to tcpdump:

emerge -av net-wireless/bluez-hcidump

And last, but very important for providing PIN to the Bluetooth device:

emerge -av net-wireless/blueman 

Enable Bluetooth

I have an Lenovo X201 laptop, so I enable and disable the Bluetooth with this command:

echo enable > /proc/acpi/ibm/bluetooth
echo disable > /proc/acpi/ibm/bluetooth

If you don’t have a Thikpad, this method will not work, as this command relays on thinkpad.ko kernel module. Please Google to find out how to enable Bluetooth on your setup.

Now start the bluez service:

/etc/init.d/bluetooth start

Search the Polar device

In order to search the chest strap, you will need to run:

hcitool scan

and the output should look similar to this:

Scanning ...
        00:22:D0:00:95:31       Polar iWL

The mac address is important, as you need to supply it as a parameter to polar.rb.

I found that my laptop will not find the Polar device on the first scan, so I ran the following command to scan few times:

while true ; do hcitool scan ; sleep 1 ; done

It took a while, but it finds the Polar device eventually.

Pairing laptop with Polar strap

Ok, I must admit, this was quit challenging. I didn’t install the bluman package at first, and saw that running

ruby polar.rb 00:22:D0:00:95:31

result with connection refused. Using hcidump, I saw that the Bluetooth on the chest strap is aborting the connection due to lack of PIN code. The manual states that the PIN is 0000 (4 times zero), so I searched for a way to add this to the program. It wasn’t straight forward task, so I read again Daniel’s post and noticed that he uses the blueman package. I installed it. Then ran the command:

blueman-applet

Hit the Search button and it should find the Polar device, then hit the Pair button to allow the device to request the PIN. This provided the initial pairing of the device with the laptop.

After this procedure, I was able to run the polar.rb using the command:

ruby polar.rb 00:22:D0:00:95:31

and my heart rate beats started appearing on the laptop screen.

Comments