From 1499f67e4cee48a933752023cdf8863f008ae06e Mon Sep 17 00:00:00 2001 From: deirdreobyrne Date: Wed, 26 Jun 2024 18:01:18 +0100 Subject: [PATCH 1/5] Initial version --- apps/bootbthomebatt/ChangeLog | 1 + apps/bootbthomebatt/README.md | 13 +++++++++++++ apps/bootbthomebatt/bluetooth.png | Bin 0 -> 1119 bytes apps/bootbthomebatt/boot.js | 10 ++++++++++ apps/bootbthomebatt/metadata.json | 15 +++++++++++++++ 5 files changed, 39 insertions(+) create mode 100644 apps/bootbthomebatt/ChangeLog create mode 100644 apps/bootbthomebatt/README.md create mode 100644 apps/bootbthomebatt/bluetooth.png create mode 100644 apps/bootbthomebatt/boot.js create mode 100644 apps/bootbthomebatt/metadata.json diff --git a/apps/bootbthomebatt/ChangeLog b/apps/bootbthomebatt/ChangeLog new file mode 100644 index 00000000000..2a37193a3e7 --- /dev/null +++ b/apps/bootbthomebatt/ChangeLog @@ -0,0 +1 @@ +0.01: Initial release. diff --git a/apps/bootbthomebatt/README.md b/apps/bootbthomebatt/README.md new file mode 100644 index 00000000000..990dcb16b0b --- /dev/null +++ b/apps/bootbthomebatt/README.md @@ -0,0 +1,13 @@ +# BLE BTHome Battery Service + +Broadcasts battery remaining percentage over BLE using the BTHome protocol + +## Usage + +This boot code runs in the background and has no user interface. + +Based on [BootGATTBat](https://github.com/espruino/BangleApps/tree/master/apps/bootgattbat) by [Jonathan Jefferies](https://github.com/jjok). + +## Creator + +[Deirdre O'Byrne](https://github.com/deirdreobyrne) diff --git a/apps/bootbthomebatt/bluetooth.png b/apps/bootbthomebatt/bluetooth.png new file mode 100644 index 0000000000000000000000000000000000000000..1a884a62c1029ae43243f8f58ef9dc3a3327e437 GIT binary patch literal 1119 zcmV-l1fctgP)cE5(396`@Dz$wLqIPzYppJ*B(brZX|I zHChYW;Hm1lL~9feUi^283bm!!Ka0DG`v(zZvk8h?W%2@c?XT zES*ZPJU3a{7h{cB0RRrPTh=dGr*a^!0&xQX?DMczGEQwQ4)Y`c0EQJR_Oa?r)W%5x z0HhI_x1HK2pc0j7k^sKW*iQXQ=2YX6D9n-q_%qO+(!1;R%(3!ggBm9SkZ!j|fYm^E zR%O?(qZ5_=gLo$b@WZ#`wXJ`4=)@t~Y>Yv)Y!7y;OB zeO8rs?l*_RK!7NCKLhRU0}||esEhzCyx)O;I=Y5XtC(@B$NTljy45^tT?SHJ10ruX zOS$(<@@!=?P@`0+S)vnkL!=bB)DOg{Q*}L+GO)XAK;&$g@DSo22n#XlR9!)?07D(! zDxz;SOSunBbNCAVm!5U2c~9jVx@WU3=u3)pJ!ur3cu;sm-)NQ!pM}i;0{{TnZpA^Z ztASvff%b#?JdoF$<=hv8)Q159pyx{LBoBD4SyIzhb(HlW;>}!J+1=AqCDO_A6&XN}=e)0!pcibn z_HtD9d_@BAknp}zDCb9=>MK#y^fmCZ_8GoYkv>KTT7e$nHy?0mXP*UpX*>1PgVgRc z3#Fdn#d~2}5mATky^{qxZ#%U!Ve5AonQN!;&C-!_@cJGbKmk6^xYakqWbkDSU>e?6 zF9=onXw<2mHO=A62q0{DUp*iYB(+@Ja7a-n2aQU$C-1do)^M)j_l7Z`m-DHf;N2WNgeIsEr6@kFmJ z8_YbwGn3bL?QPY+LOBr_TDRcEMmfJ|;s=HR0IQ!ry9xAti1(G5Y&@#1^&${_&Hfi+ z9c`2jUpMuHhg{we{KebpwXs3NLqaRiAr)b6sg!$n>kZxDN)lj0k<-mm?qZatNdeqJ z)Lky+8&Ml40dUjvB)_tlzY&Ld+&A&{bh~wEWib~^c!+ZagoS&X-t=l^d_A@r#J2*U lKABp3ezkHW*6{xc{R@3@kib`1LqGrk002ovPDHLkV1l684mkh- literal 0 HcmV?d00001 diff --git a/apps/bootbthomebatt/boot.js b/apps/bootbthomebatt/boot.js new file mode 100644 index 00000000000..b585fb3c56c --- /dev/null +++ b/apps/bootbthomebatt/boot.js @@ -0,0 +1,10 @@ +(() => { + var btHomeSequenceNo = 0; + function advertiseBTHomeBattery() { + require("ble_advert").set(0xFCD2, [0x40,0x00,btHomeSequenceNo,0x01,E.getBattery()]); + btHomeSequenceNo = (btHomeSequenceNo + 1) & 255; + } + + setInterval(advertiseBTHomeBattery, 5 * 60 * 1000); + advertiseBattery(); +})(); diff --git a/apps/bootbthomebatt/metadata.json b/apps/bootbthomebatt/metadata.json new file mode 100644 index 00000000000..9deebfdc071 --- /dev/null +++ b/apps/bootbthomebatt/metadata.json @@ -0,0 +1,15 @@ +{ + "id": "bootbthomebatt", + "name": "BLE BTHome Battery Service", + "shortName": "BTHome Battery Service", + "version": "0.01", + "description": "Broadcasts battery remaining over bluetooth using the BTHome protocol.\n", + "icon": "bluetooth.png", + "type": "bootloader", + "tags": "battery,ble,bluetooth,bthome", + "supports": ["BANGLEJS2"], + "readme": "README.md", + "storage": [ + {"name":"bthomebat.boot.js","url":"boot.js"} + ] +} From 850f18e1167168fae348ae429aa7e8dffbea06d4 Mon Sep 17 00:00:00 2001 From: deirdreobyrne Date: Wed, 26 Jun 2024 18:53:59 +0100 Subject: [PATCH 2/5] Attempt 2 --- apps/bootbthomebatt/boot.js | 45 ++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/apps/bootbthomebatt/boot.js b/apps/bootbthomebatt/boot.js index b585fb3c56c..e13135b45ef 100644 --- a/apps/bootbthomebatt/boot.js +++ b/apps/bootbthomebatt/boot.js @@ -1,10 +1,39 @@ -(() => { - var btHomeSequenceNo = 0; - function advertiseBTHomeBattery() { - require("ble_advert").set(0xFCD2, [0x40,0x00,btHomeSequenceNo,0x01,E.getBattery()]); - btHomeSequenceNo = (btHomeSequenceNo + 1) & 255; +var btHomeBatterySequence = 0; + +function advertiseBTHomeBattery() { + var advert = [ 0x40, /* BTHome Device Information + bit 0: "Encryption flag" + bit 1-4: "Reserved for future use" + bit 5-7: "BTHome Version" */ + + 0x00, // Sequence number + btHomeBatterySequence, + + 0x01, // Battery, 8 bit + E.getBattery() + ]; + + 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; } + NRF.setAdvertising(Bangle.bleAdvert); + btHomeBatterySequence = (btHomeBatterySequence + 1) & 255; +} + +if (!Bangle.bleAdvert) Bangle.bleAdvert = {}; +setInterval(function() { + advertiseBTHomeBattery(); +}, 300000); // update every 5 min - setInterval(advertiseBTHomeBattery, 5 * 60 * 1000); - advertiseBattery(); -})(); +advertiseBTHomeBattery(); From 3cf61e6c073c3e1fa2bf113174f99007684d0214 Mon Sep 17 00:00:00 2001 From: deirdreobyrne Date: Wed, 26 Jun 2024 19:18:34 +0100 Subject: [PATCH 3/5] Update README.md Adding link to the protocol description --- apps/bootbthomebatt/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/bootbthomebatt/README.md b/apps/bootbthomebatt/README.md index 990dcb16b0b..b3e10e1cfe9 100644 --- a/apps/bootbthomebatt/README.md +++ b/apps/bootbthomebatt/README.md @@ -1,6 +1,6 @@ # BLE BTHome Battery Service -Broadcasts battery remaining percentage over BLE using the BTHome protocol +Broadcasts battery remaining percentage over BLE using the [BTHome protocol](https://bthome.io/) ## Usage From 29c6e385e223b1e0bfd7058a4de5c7ba6be497c0 Mon Sep 17 00:00:00 2001 From: deirdreobyrne Date: Wed, 26 Jun 2024 19:30:14 +0100 Subject: [PATCH 4/5] Cleanup --- apps/bootbthomebatt/README.md | 4 +--- apps/bootbthomebatt/boot.js | 12 +----------- apps/bootbthomebatt/metadata.json | 2 +- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/apps/bootbthomebatt/README.md b/apps/bootbthomebatt/README.md index b3e10e1cfe9..79c379f33ba 100644 --- a/apps/bootbthomebatt/README.md +++ b/apps/bootbthomebatt/README.md @@ -1,13 +1,11 @@ # BLE BTHome Battery Service -Broadcasts battery remaining percentage over BLE using the [BTHome protocol](https://bthome.io/) +Broadcasts battery remaining percentage over BLE using the [BTHome protocol](https://bthome.io/) - which makes for easy integration into [Home Assistant](https://www.home-assistant.io/) ## Usage This boot code runs in the background and has no user interface. -Based on [BootGATTBat](https://github.com/espruino/BangleApps/tree/master/apps/bootgattbat) by [Jonathan Jefferies](https://github.com/jjok). - ## Creator [Deirdre O'Byrne](https://github.com/deirdreobyrne) diff --git a/apps/bootbthomebatt/boot.js b/apps/bootbthomebatt/boot.js index e13135b45ef..64275ed312d 100644 --- a/apps/bootbthomebatt/boot.js +++ b/apps/bootbthomebatt/boot.js @@ -1,17 +1,7 @@ var btHomeBatterySequence = 0; function advertiseBTHomeBattery() { - var advert = [ 0x40, /* BTHome Device Information - bit 0: "Encryption flag" - bit 1-4: "Reserved for future use" - bit 5-7: "BTHome Version" */ - - 0x00, // Sequence number - btHomeBatterySequence, - - 0x01, // Battery, 8 bit - E.getBattery() - ]; + var advert = [0x40, 0x00, btHomeBatterySequence, 0x01, E.getBattery()]; if(Array.isArray(Bangle.bleAdvert)){ var found = false; diff --git a/apps/bootbthomebatt/metadata.json b/apps/bootbthomebatt/metadata.json index 9deebfdc071..0992edc2451 100644 --- a/apps/bootbthomebatt/metadata.json +++ b/apps/bootbthomebatt/metadata.json @@ -3,7 +3,7 @@ "name": "BLE BTHome Battery Service", "shortName": "BTHome Battery Service", "version": "0.01", - "description": "Broadcasts battery remaining over bluetooth using the BTHome protocol.\n", + "description": "Broadcasts battery remaining over bluetooth using the BTHome protocol - makes for easy integration with Home Assistant.\n", "icon": "bluetooth.png", "type": "bootloader", "tags": "battery,ble,bluetooth,bthome", From e48d253701867567c4039bc0ddd57021406874e5 Mon Sep 17 00:00:00 2001 From: deirdreobyrne Date: Wed, 26 Jun 2024 23:26:08 +0100 Subject: [PATCH 5/5] Using the ble_advert module --- apps/bootbthomebatt/boot.js | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/apps/bootbthomebatt/boot.js b/apps/bootbthomebatt/boot.js index 64275ed312d..a9bdcd4d3db 100644 --- a/apps/bootbthomebatt/boot.js +++ b/apps/bootbthomebatt/boot.js @@ -3,25 +3,10 @@ var btHomeBatterySequence = 0; function advertiseBTHomeBattery() { var advert = [0x40, 0x00, btHomeBatterySequence, 0x01, E.getBattery()]; - 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; - } - NRF.setAdvertising(Bangle.bleAdvert); + require("ble_advert").set(0xFCD2, advert); btHomeBatterySequence = (btHomeBatterySequence + 1) & 255; } -if (!Bangle.bleAdvert) Bangle.bleAdvert = {}; setInterval(function() { advertiseBTHomeBattery(); }, 300000); // update every 5 min