Skip to content

Instantly share code, notes, and snippets.

@AndrewReitz
Created November 19, 2018 22:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewReitz/2868e3aecaecbb419b6c3f2cf33389e8 to your computer and use it in GitHub Desktop.
Save AndrewReitz/2868e3aecaecbb419b6c3f2cf33389e8 to your computer and use it in GitHub Desktop.
Cadence Speed Sensor
const noble = require('noble');
noble.on('stateChange', state => {
if (state === 'poweredOn') {
noble.startScanning(['1816']);
} else {
noble.stopScanning();
}
});
noble.on('discover', peripheral => {
console.log("discovered :" + peripheral.advertisement.localName);
noble.stopScanning();
explore(peripheral);
});
function explore(peripheral) {
console.log('services and characteristics:');
peripheral.on('disconnect', () => {
console.log("Disconnected");
process.exit(0);
});
peripheral.connect(error => {
if (error) {
console.log("there was an error " + error);
return
}
// uuid = 1816 speed and cadence
// uuid = 2a5b CSC Measurement
// https://github.com/sputnikdev/bluetooth-gatt-parser/blob/master/src/main/resources/gatt/characteristic/org.bluetooth.characteristic.csc_measurement.xml
peripheral.discoverSomeServicesAndCharacteristics(["1816"], ["2a5b"], (error, services, characteristics) => {
if (error) {
console.log("there was an error " + error);
return
}
characteristics[0].subscribe(error => {
if (error) {
console.log("there was an error " + error);
return
}
characteristics[0].on('data', (data) => {
// 0 is data present if 3 all are available if 2 only wheel data and only 1 cadence only
// 1 - 4 ints = wheel revolutions
// 5 - 6 ints = last wheel event time.
// 7 - 8 ints = crank revolutions
// 9 - 10 ints = last crank time
const first = data[4] << 24;
const second = data[3] << 16;
const third = data[2] << 8;
const forth = data[1];
console.log("first: " + first);
console.log("second: " + second);
console.log("third: " + third);
console.log("forth: " + forth);
console.log("total: " + (first + second + third + forth));
const event1 = data[5];
const event2 = data[6] << 8;
console.log("event1: " + event1);
console.log("event2: " + event2);
console.log("total: " + (event1 + event2));
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment