Skip to content

Commit

Permalink
readme and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Lear committed Feb 22, 2016
1 parent d66526f commit dd2b330
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Expand Up @@ -2,24 +2,37 @@ var noble = require('noble');

noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
// Seek for peripherals broadcasting the heart rate service
// This will pick up a Polar H7 and should pick up other ble heart rate bands
// Will use whichever the first one discovered is if more than one are in range
noble.startScanning(["180d"]);
} else {
noble.stopScanning();
}
});

noble.on('discover', function(peripheral) {
// Once peripheral is discovered, stop scanning
noble.stopScanning();

// connect to the heart rate sensor
peripheral.connect(function(error){
// 180d is the bluetooth service for heart rate:
// https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.heart_rate.xml
var serviceUUID = ["180d"];
// 2a37 is the characteristic for heart rate measurement
// https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
var characteristicUUID = ["2a37"];

// use noble's discoverSomeServicesAndCharacteristics
// scoped to the heart rate service and measurement characteristic
peripheral.discoverSomeServicesAndCharacteristics(serviceUUID, characteristicUUID, function(error, services, characteristics){
characteristics[0].notify(true, function(error){
characteristics[0].on('data', function(data, isNotification){
// Upon receiving data, output the BPM
// The actual BPM data is stored in the 2nd bit in data (at array index 1)
// Thanks Steve Daniel: http://www.raywenderlich.com/52080/introduction-core-bluetooth-building-heart-rate-monitor
// Measurement docs here: https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
console.log('data is: ' + data[1]);
});
});
Expand Down
18 changes: 18 additions & 0 deletions readme.md
@@ -0,0 +1,18 @@
# Log heart rate from a Polar H7 using node

A rudamentary example of using [Noble](https://github.com/sandeepmistry/noble) to listen to heart rate data from a Polar H7 bluetooth heart rate sensor.

`index.js` is pretty short and thoroughly commented. The code is pretty naive, it just finds the first bluetooth peripheral transmitting the bluetooth heart rate service and listens for the measurement data.

## How to use this

- [Install node](https://nodejs.org/en/download/)
- Clone this repo
- In the root directory, run `npm install` to install noble
- While wearing the Polar H7, run `node index.js`
- After a few seconds, you should see heart rate data logged into the terminal

## Notes
This _should_ function with other bluetooth heart rate sensors that broadcast the 180d service.
This does not include Fitbit Charge HR - to my knowledge the Charge HR does not broadcast the
heart rate service over bluetooth.

0 comments on commit dd2b330

Please sign in to comment.