From b4dffbfd0e76df82385a1610f7ec29347297d8b9 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Fri, 24 May 2024 17:18:35 +0100 Subject: [PATCH] Use `ble_advert` module --- apps/bootgattbat/ChangeLog | 1 + apps/bootgattbat/boot.js | 20 +------------------- apps/bootgattbat/metadata.json | 2 +- apps/bootgatthrm/ChangeLog | 1 + apps/bootgatthrm/boot.js | 20 +++++++------------- apps/bootgatthrm/metadata.json | 2 +- apps/btadv/ChangeLog | 1 + apps/btadv/app.ts | 2 ++ apps/btadv/metadata.json | 2 +- apps/bthome/ChangeLog | 3 ++- apps/bthome/boot.js | 18 +----------------- apps/bthome/metadata.json | 2 +- 12 files changed, 20 insertions(+), 54 deletions(-) diff --git a/apps/bootgattbat/ChangeLog b/apps/bootgattbat/ChangeLog index df07f6ad0..3df6422e5 100644 --- a/apps/bootgattbat/ChangeLog +++ b/apps/bootgattbat/ChangeLog @@ -1,2 +1,3 @@ 0.01: Initial release. 0.02: Handle the case where other apps have set bleAdvert to an array +0.03: Use the bleAdvert module diff --git a/apps/bootgattbat/boot.js b/apps/bootgattbat/boot.js index 34d9f8d93..f9b5969e2 100644 --- a/apps/bootgattbat/boot.js +++ b/apps/bootgattbat/boot.js @@ -1,26 +1,8 @@ (() => { function advertiseBattery() { - if(Array.isArray(Bangle.bleAdvert)){ - // ensure we're in the cycle - var found = false; - for(var ad in Bangle.bleAdvert){ - if(ad[0x180F]){ - ad[0x180F] = [E.getBattery()]; - found = true; - break; - } - } - if(!found) - Bangle.bleAdvert.push({ 0x180F: [E.getBattery()] }); - }else{ - // simple object - Bangle.bleAdvert[0x180F] = [E.getBattery()]; - } - - NRF.setAdvertising(Bangle.bleAdvert); + require("ble_advert").set(0x180F, [E.getBattery()]); } - if (!Bangle.bleAdvert) Bangle.bleAdvert = {}; setInterval(advertiseBattery, 60 * 1000); advertiseBattery(); })(); diff --git a/apps/bootgattbat/metadata.json b/apps/bootgattbat/metadata.json index f67b4507d..b53708c2e 100644 --- a/apps/bootgattbat/metadata.json +++ b/apps/bootgattbat/metadata.json @@ -2,7 +2,7 @@ "id": "bootgattbat", "name": "BLE GATT Battery Service", "shortName": "BLE Battery Service", - "version": "0.02", + "version": "0.03", "description": "Adds the GATT Battery Service to advertise the percentage of battery currently remaining over Bluetooth.\n", "icon": "bluetooth.png", "type": "bootloader", diff --git a/apps/bootgatthrm/ChangeLog b/apps/bootgatthrm/ChangeLog index 2512ed877..205f036b5 100644 --- a/apps/bootgatthrm/ChangeLog +++ b/apps/bootgatthrm/ChangeLog @@ -1,3 +1,4 @@ 0.01: Initial release. 0.02: Added compatibility to OpenTracks and added HRM Location 0.03: Allow setting to keep BLE connected +0.04: Use the bleAdvert module diff --git a/apps/bootgatthrm/boot.js b/apps/bootgatthrm/boot.js index 0fceb86d9..efba1f453 100644 --- a/apps/bootgatthrm/boot.js +++ b/apps/bootgatthrm/boot.js @@ -4,18 +4,13 @@ * This function prepares BLE heart rate Advertisement. */ - NRF.setAdvertising( - { - 0x180d: undefined - }, - { - // We need custom Advertisement settings for Apps like OpenTracks - connectable: true, - discoverable: true, - scannable: true, - whenConnected: true, - } - ); + require("ble_advert").set(0x180d, undefined, { + // We need custom Advertisement settings for Apps like OpenTracks + connectable: true, + discoverable: true, + scannable: true, + whenConnected: true, + }); NRF.setServices({ 0x180D: { // heart_rate @@ -28,7 +23,6 @@ } } }); - } const keepConnected = (require("Storage").readJSON("gatthrm.settings.json", 1) || {}).keepConnected; diff --git a/apps/bootgatthrm/metadata.json b/apps/bootgatthrm/metadata.json index dc831ab7d..d32b51601 100644 --- a/apps/bootgatthrm/metadata.json +++ b/apps/bootgatthrm/metadata.json @@ -2,7 +2,7 @@ "id": "bootgatthrm", "name": "BLE GATT HRM Service", "shortName": "BLE HRM Service", - "version": "0.03", + "version": "0.04", "description": "Adds the GATT HRM Service to advertise the measured HRM over Bluetooth.\n", "icon": "bluetooth.png", "type": "bootloader", diff --git a/apps/btadv/ChangeLog b/apps/btadv/ChangeLog index 07e67157c..245f4fca6 100644 --- a/apps/btadv/ChangeLog +++ b/apps/btadv/ChangeLog @@ -1,2 +1,3 @@ 0.01: New app! 0.02: Advertise accelerometer data and sensor location +0.03: Use the bleAdvert module diff --git a/apps/btadv/app.ts b/apps/btadv/app.ts index 713172ca1..b56546ba1 100644 --- a/apps/btadv/app.ts +++ b/apps/btadv/app.ts @@ -667,6 +667,8 @@ const getBleAdvert = (map: (s: BleServ) => T, all = false) => { // done via advertise in setServices() //const updateBleAdvert = () => { +// require("ble_advert").set(...) +// // let bleAdvert: ReturnType>; // // if (!(bleAdvert = (Bangle as any).bleAdvert)) { diff --git a/apps/btadv/metadata.json b/apps/btadv/metadata.json index efe024a2f..060c2b498 100644 --- a/apps/btadv/metadata.json +++ b/apps/btadv/metadata.json @@ -2,7 +2,7 @@ "id": "btadv", "name": "btadv", "shortName": "btadv", - "version": "0.02", + "version": "0.03", "description": "Advertise & export live heart rate, accel, pressure, GPS & mag data over bluetooth", "icon": "icon.png", "tags": "health,tool,sensors,bluetooth", diff --git a/apps/bthome/ChangeLog b/apps/bthome/ChangeLog index ca91e0f34..82c93ee1f 100644 --- a/apps/bthome/ChangeLog +++ b/apps/bthome/ChangeLog @@ -2,4 +2,5 @@ 0.02: Fix double-button press if you press the next button within 30s (#3243) 0.03: Cope with identical duplicate buttons (fix #3260) Set 'n' for buttons in Bangle.btHomeData correctly (avoids adding extra buttons on end of advertising) -0.04: Fix duplicate button on edit->save \ No newline at end of file +0.04: Fix duplicate button on edit->save +0.05: Use the bleAdvert module diff --git a/apps/bthome/boot.js b/apps/bthome/boot.js index 7c9e37f77..00e08df90 100644 --- a/apps/bthome/boot.js +++ b/apps/bthome/boot.js @@ -1,5 +1,3 @@ -// Ensure we have the bleAdvert global (to play well with other stuff) -if (!Bangle.bleAdvert) Bangle.bleAdvert = {}; Bangle.btHomeData = []; { require("BTHome").packetId = 0|(Math.random()*256); // random packet id so new packets show up @@ -39,20 +37,6 @@ Bangle.btHome = function(extras, options) { if (bat) bat.v = E.getBattery(); var advert = require("BTHome").getAdvertisement(Bangle.btHomeData)[0xFCD2]; // Add to the list of available advertising - if(Array.isArray(Bangle.bleAdvert)){ - var found = false; - for(var ad in Bangle.bleAdvert){ - if(ad[0xFCD2]){ - ad[0xFCD2] = advert; - found = true; - break; - } - } - if(!found) - Bangle.bleAdvert.push({ 0xFCD2: advert }); - } else { - Bangle.bleAdvert[0xFCD2] = advert; - } var advOptions = {}; var updateTimeout = 10*60*1000; // update every 10 minutes if (options.event) { // if it's an event... @@ -60,7 +44,7 @@ Bangle.btHome = function(extras, options) { advOptions.whenConnected = true; updateTimeout = 30000; // slow down in 30 seconds } - NRF.setAdvertising(Bangle.bleAdvert, advOptions); + require("ble_advert").set(0xFCD2, advert, advOptions); if (Bangle.btHomeTimeout) clearTimeout(Bangle.btHomeTimeout); Bangle.btHomeTimeout = setTimeout(function() { delete Bangle.btHomeTimeout; diff --git a/apps/bthome/metadata.json b/apps/bthome/metadata.json index 1ccc19316..0156a5d32 100644 --- a/apps/bthome/metadata.json +++ b/apps/bthome/metadata.json @@ -1,7 +1,7 @@ { "id": "bthome", "name": "BTHome", "shortName":"BTHome", - "version":"0.04", + "version":"0.05", "description": "Allow your Bangle to advertise with BTHome and send events to Home Assistant via Bluetooth", "icon": "icon.png", "type": "app",