bthrm - Allow scanning for sensors in settings

master
Martin Boonk 2022-03-09 20:30:39 +01:00
parent 24a0496109
commit 7250f4f230
4 changed files with 118 additions and 54 deletions

View File

@ -20,3 +20,4 @@
0.07: Recorder icon only blue if values actually arive 0.07: Recorder icon only blue if values actually arive
Adds some preset modes and a custom one Adds some preset modes and a custom one
Restructure the settings menu Restructure the settings menu
0.08: Allow scanning for devices in settings

View File

@ -23,7 +23,10 @@
} }
function getCache(){ function getCache(){
return require('Storage').readJSON("bthrm.cache.json", true) || {}; var cache = require('Storage').readJSON("bthrm.cache.json", true) || {};
if (settings.btname && settings.btname == cache.name) return cache;
clearCache();
return {};
} }
function addNotificationHandler(characteristic){ function addNotificationHandler(characteristic){
@ -361,7 +364,13 @@
var promise; var promise;
if (!device){ if (!device){
promise = NRF.requestDevice({ filters: serviceFilters }); var filters = serviceFilters;
if (settings.btname){
log("Configured device name", settings.btname);
filters = [{name: settings.btname}];
}
log("Requesting device with filters", filters);
promise = NRF.requestDevice({ filters: filters });
if (settings.gracePeriodRequest){ if (settings.gracePeriodRequest){
log("Add " + settings.gracePeriodRequest + "ms grace period after request"); log("Add " + settings.gracePeriodRequest + "ms grace period after request");
@ -488,11 +497,15 @@
if (gatt) { if (gatt) {
if (gatt.connected){ if (gatt.connected){
log("Disconnect with gatt: ", gatt); log("Disconnect with gatt: ", gatt);
try{
gatt.disconnect().then(()=>{ gatt.disconnect().then(()=>{
log("Successful disconnect"); log("Successful disconnect");
}).catch((e)=>{ }).catch((e)=>{
log("Error during disconnect", e); log("Error during disconnect promise", e);
}); });
} catch (e){
log("Error during disconnect attempt", e);
}
} }
} }
} }

View File

@ -2,7 +2,7 @@
"id": "bthrm", "id": "bthrm",
"name": "Bluetooth Heart Rate Monitor", "name": "Bluetooth Heart Rate Monitor",
"shortName": "BT HRM", "shortName": "BT HRM",
"version": "0.07", "version": "0.08",
"description": "Overrides Bangle.js's build in heart rate monitor with an external Bluetooth one.", "description": "Overrides Bangle.js's build in heart rate monitor with an external Bluetooth one.",
"icon": "app.png", "icon": "app.png",
"type": "app", "type": "app",

View File

@ -17,6 +17,7 @@
var settings; var settings;
readSettings(); readSettings();
function buildMainMenu(){
var mainmenu = { var mainmenu = {
'': { 'title': 'Bluetooth HRM' }, '': { 'title': 'Bluetooth HRM' },
'< Back': back, '< Back': back,
@ -57,14 +58,32 @@
} }
writeSettings("mode",v); writeSettings("mode",v);
} }
}, }
'Custom Mode': function() { E.showMenu(submenu_custom); },
'Debug': function() { E.showMenu(submenu_debug); }
}; };
if (settings.btname){
var name = "Clear " + settings.btname;
mainmenu[name] = function() {
E.showPrompt("Clear current device name?").then((r)=>{
if (r) {
writeSettings("btname",undefined);
}
E.showMenu(buildMainMenu());
});
};
}
mainmenu["BLE Scan"] = ()=> createMenuFromScan();
mainmenu["Custom Mode"] = function() { E.showMenu(submenu_custom); };
mainmenu.Debug = function() { E.showMenu(submenu_debug); };
return mainmenu;
}
var submenu_debug = { var submenu_debug = {
'' : { title: "Debug"}, '' : { title: "Debug"},
'< Back': function() { E.showMenu(mainmenu); }, '< Back': function() { E.showMenu(buildMainMenu()); },
'Alert on disconnect': { 'Alert on disconnect': {
value: !!settings.warnDisconnect, value: !!settings.warnDisconnect,
format: v => settings.warnDisconnect ? "On" : "Off", format: v => settings.warnDisconnect ? "On" : "Off",
@ -82,9 +101,40 @@
'Grace periods': function() { E.showMenu(submenu_grace); } 'Grace periods': function() { E.showMenu(submenu_grace); }
}; };
function createMenuFromScan(){
E.showMenu();
E.showMessage("Scanning");
var submenu_scan = {
'' : { title: "Scan"},
'< Back': function() { E.showMenu(buildMainMenu()); }
};
var packets=10;
var scanStart=Date.now();
NRF.setScan(function(d) {
packets--;
if (packets<=0 || Date.now() - scanStart > 5000){
NRF.setScan();
E.showMenu(submenu_scan);
} else if (d.name){
print("Found device", d);
submenu_scan[d.name] = function(){
E.showPrompt("Set "+d.name+"?").then((r)=>{
if (r) {
writeSettings("btname",d.name);
}
E.showMenu(buildMainMenu());
});
};
}
}, { filters: [{services: [ "180d" ]}]});
}
var submenu_custom = { var submenu_custom = {
'' : { title: "Custom mode"}, '' : { title: "Custom mode"},
'< Back': function() { E.showMenu(mainmenu); }, '< Back': function() { E.showMenu(buildMainMenu()); },
'Replace HRM': { 'Replace HRM': {
value: !!settings.custom_replace, value: !!settings.custom_replace,
format: v => settings.custom_replace ? "On" : "Off", format: v => settings.custom_replace ? "On" : "Off",
@ -165,7 +215,7 @@
var submenu = { var submenu = {
'' : { title: "Grace periods"}, '' : { title: "Grace periods"},
'< Back': function() { E.showMenu(mainmenu); }, '< Back': function() { E.showMenu(buildMainMenu()); },
'Request': { 'Request': {
value: settings.gracePeriodRequest, value: settings.gracePeriodRequest,
min: 0, min: 0,
@ -208,5 +258,5 @@
} }
}; };
E.showMenu(mainmenu); E.showMenu(buildMainMenu());
}) })