feat: Added new bootloader "bootgatthrm" which exposes HRM data via BLE

master
André Büsgen 2023-06-09 10:43:50 +02:00
parent f1ae6c9215
commit dbeb391e69
1 changed files with 22 additions and 11 deletions

View File

@ -1,8 +1,9 @@
(() => { (() => {
function setupHRMAdvertising() { function setupHRMAdvertising() {
/* /*
* This function preparse BLE heart rate Advertisement. * This function prepares BLE heart rate Advertisement.
*/ */
NRF.setServices({ NRF.setServices({
0x180D: { // heart_rate 0x180D: { // heart_rate
0x2A37: { // heart_rate_measurement 0x2A37: { // heart_rate_measurement
@ -10,34 +11,44 @@
value: [0x06, 0], value: [0x06, 0],
} }
} }
}, { advertise: ['180D'] }); }, { advertise: [0x180d] });
} }
function updateBLEHeartRate(hrm) { function updateBLEHeartRate(hrm) {
/* /*
* Send updated heart rate measurement via BLE * Send updated heart rate measurement via BLE
*/ */
if (hrm === undefined) return; if (hrm === undefined || hrm.confidence < 50) return;
try { try {
NRF.updateServices({ NRF.updateServices({
'180d': { 0x180D: {
'2a37': { 0x2A37: {
value: [ value: [
0x06, // 0x06, //
hrm hrm.bpm
], ],
notify: true notify: true
} }
} }
}); });
} catch (error) { } catch (error) {
// After setupHRMAdvertising() BLE needs to restart. if (error.message.includes("BLE restart")) {
// We force a disconnect if the Bangle was connected while setting HRM /*
NRF.disconnect(); * BLE has to restart after service setup.
*/
NRF.disconnect();
}
else if (error.message.includes("UUID 0x2a37")) {
/*
* Setup service if it wasn't setup correctly for some reason
*/
setupHRMAdvertising();
} else {
console.log("[bootgatthrm]: Unexpected error occured while updating HRM over BLE! Error: " + error.message);
}
} }
} }
setupHRMAdvertising(); setupHRMAdvertising();
Bangle.on("HRM", function (hrm) { updateBLEHeartRate(hrm.bpm); }); Bangle.on("HRM", function (hrm) { updateBLEHeartRate(hrm); });
})(); })();