diff --git a/apps/bootgatthrm/boot.js b/apps/bootgatthrm/boot.js index 9f1ec1584..391f245b9 100644 --- a/apps/bootgatthrm/boot.js +++ b/apps/bootgatthrm/boot.js @@ -1,8 +1,9 @@ (() => { function setupHRMAdvertising() { /* - * This function preparse BLE heart rate Advertisement. + * This function prepares BLE heart rate Advertisement. */ + NRF.setServices({ 0x180D: { // heart_rate 0x2A37: { // heart_rate_measurement @@ -10,34 +11,44 @@ value: [0x06, 0], } } - }, { advertise: ['180D'] }); + }, { advertise: [0x180d] }); } - function updateBLEHeartRate(hrm) { /* * Send updated heart rate measurement via BLE */ - if (hrm === undefined) return; + if (hrm === undefined || hrm.confidence < 50) return; try { NRF.updateServices({ - '180d': { - '2a37': { + 0x180D: { + 0x2A37: { value: [ 0x06, // - hrm + hrm.bpm ], notify: true } } }); } catch (error) { - // After setupHRMAdvertising() BLE needs to restart. - // We force a disconnect if the Bangle was connected while setting HRM - NRF.disconnect(); + if (error.message.includes("BLE restart")) { + /* + * 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(); - Bangle.on("HRM", function (hrm) { updateBLEHeartRate(hrm.bpm); }); + Bangle.on("HRM", function (hrm) { updateBLEHeartRate(hrm); }); })();