Connect Polar H10 Bluetooth LE Heart Rate Sensor to Ubuntu
done on Ubuntu 17.04


All made possible by this very good and helpfull post: http://blog.akhq.net/2014/11/polar-h7-bluetooth-le-heart-rate-sensor.html
All I did was try it with the newer Polar H10 sensor.

I was getting an error with the BLE LE connection:

Error connect: Connection refused (111)

so from this post (https://stackoverflow.com/questions/32947807/cannot-connect-to-ble-device-on-raspberry-pi)
I copied into /etc/bluetooth/main.conf the following 3 lines:

EnableLE = true           // Enable Low Energy support. Default is false.
AttributeServer = true    // Enable the GATT attribute server. Default is false.
DisablePlugins=pnat

Get list of bluetooth devices on computer:

sudo hcitool dev

List bluetooth devices in range (scan for bluetooth devices):

sudo hcitool -i <your divice id (mine was hci0)> lescan

Connect to the Polar H10 device:

sudo gatttool -t random -b <MAC ADDRESS> -I
[MAC ADDRESS][LE]> connect
Attempting to connect to <MAC ADDRESS>
Connection successful

List services:

[MAC ADDRESS][LE]> primary
attr handle: 0x0001, end grp handle: 0x0009 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000a, end grp handle: 0x000d uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x000e, end grp handle: 0x0013 uuid: 0000180d-0000-1000-8000-00805f9b34fb
attr handle: 0x0014, end grp handle: 0x0029 uuid: 0000181c-0000-1000-8000-00805f9b34fb
attr handle: 0x002a, end grp handle: 0x0038 uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x0039, end grp handle: 0x003e uuid: 0000fea5-0000-1000-8000-00805f9b34fb
attr handle: 0x003f, end grp handle: 0x0042 uuid: 0000180f-0000-1000-8000-00805f9b34fb
attr handle: 0x0043, end grp handle: 0x0048 uuid: 6217ff4b-fb31-1140-ad5a-a45545d7ecf3
attr handle: 0x0049, end grp handle: 0xffff uuid: 0000feee-0000-1000-8000-00805f9b34fb

Where (according to https://www.bluetooth.com/specifications/gatt/services):

attr handle: 0x0001, end grp handle: 0x0009 uuid: 00001800-0000-1000-8000-00805f9b34fb -- Generic Access (org.bluetooth.service.generic_access)
attr handle: 0x000a, end grp handle: 0x000d uuid: 00001801-0000-1000-8000-00805f9b34fb -- Generic Attribute (org.bluetooth.service.generic_attribute)
attr handle: 0x000e, end grp handle: 0x0013 uuid: 0000180d-0000-1000-8000-00805f9b34fb -- Heart Rate (org.bluetooth.service.heart_rate)
attr handle: 0x0014, end grp handle: 0x0029 uuid: 0000181c-0000-1000-8000-00805f9b34fb -- User Data (org.bluetooth.service.user_data)
attr handle: 0x002a, end grp handle: 0x0038 uuid: 0000180a-0000-1000-8000-00805f9b34fb -- Device Information (org.bluetooth.service.device_information)
attr handle: 0x0039, end grp handle: 0x003e uuid: 0000fea5-0000-1000-8000-00805f9b34fb 
attr handle: 0x003f, end grp handle: 0x0042 uuid: 0000180f-0000-1000-8000-00805f9b34fb -- Battery Servie (org.bluetooth.service.battery_service)
attr handle: 0x0043, end grp handle: 0x0048 uuid: 6217ff4b-fb31-1140-ad5a-a45545d7ecf3
attr handle: 0x0049, end grp handle: 0xffff uuid: 0000feee-0000-1000-8000-00805f9b34fb

Get Battery service:

[MAC ADDRESS][LE]> char-desc 0x003f 0x0042
handle: 0x003f, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0040, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0041, uuid: 00002a19-0000-1000-8000-00805f9b34fb
handle: 0x0042, uuid: 00002902-0000-1000-8000-00805f9b34fb
[MAC ADDRESS][LE]> char-read-hnd 0x0042
Characteristic value/descriptor: 00 00

Get Heart Rate service:

[MAC ADDRESS][LE]> char-desc 0x000e 0x0013
handle: 0x000e, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0010, uuid: 00002a37-0000-1000-8000-00805f9b34fb
handle: 0x0011, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0012, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0013, uuid: 00002a38-0000-1000-8000-00805f9b34fb
[MAC ADDRESSS][LE]> char-write-req 0x0011 0100
Characteristic value was written successfully
Notification handle = 0x0010 value: 10 45 80 03 
Notification handle = 0x0010 value: 10 44 85 03 
Notification handle = 0x0010 value: 10 44 b2 03 
Notification handle = 0x0010 value: 10 44 f1 03 

Working epic one liner for Polar H10 Heart Rate readings:

gatttool -t random -b <MAC ADDRESS> --char-write-req --handle=0x0011 --value=0100 --listen | perl -ne 'if(/.*value: (\w+) (\w+) (\w+) (\w+)/) { ($x,$y,$z,$a) = ($1,$2,$3, $4);$rr = hex("$a$z"); printf ("%d, %f, %x, %x, %d\n", hex($y), $rr/1024, $a, $z, $rr);}'
70, 0.871094, 3, 7, 892
70, 0.837891, 3, 5, 858
70, 0.848633, 3, 41, 869
70, 0.957031, 3, 0, 980
70, 0.939453, 3, 0, 962
69, 0.807617, 3, 3, 827
70, 0.754883, 3, 5, 773
71, 0.900391, 3, 9, 922
71, 0.905273, 3, 9, 927
70, 0.885742, 3, 8, 907

Logging to a file can be done using script:

script -c "myprogram" file.log

If bluetooth device is busy you may sometimes get the following error:

connect: Device or resource busy (16)

I paired the Polar H10 strap using network manager and clicked Bluetooth -> the device -> Connection –> OFF and ran the script again.


Posted on:

August 5, 2017
586 words

Contact Me:

If you are interested in contacting me feel free to do so by mail or online:

nob {at} nob.ro