Skip to content

Commit b6dc9b3

Browse files
committed
add timeout in case Noble doesn't power on, fix #25
1 parent 51a43e6 commit b6dc9b3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/discovery.js

+16
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,21 @@ function log(x) {
3030
}
3131

3232
// ----------------------------------------------------------------------
33+
var powerOnTimer;
34+
if (config.ble_timeout > 0)
35+
powerOnTimer = setTimeout(function() {
36+
powerOnTimer = undefined;
37+
log("BLE broken? No Noble State Change to 'poweredOn' in "+ config.ble_timeout +" seconds - restarting!");
38+
process.exit(1);
39+
}, config.ble_timeout * 1000)
40+
3341
function onStateChange(state) {
42+
log("Noble StateChange: "+state);
3443
if (state!="poweredOn") return;
44+
if (powerOnTimer) {
45+
clearTimeout(powerOnTimer);
46+
powerOnTimer = undefined;
47+
}
3548
// delay startup to allow Bleno to set discovery up
3649
setTimeout(function() {
3750
log("Starting scan...");
@@ -187,7 +200,10 @@ exports.inRange = inRange;
187200

188201
exports.restartScan = function() {
189202
if (!isScanning) {
203+
log("Restarting scan");
190204
noble.startScanning([], true);
205+
} else {
206+
log("restartScan: already scanning!");
191207
}
192208
}
193209

0 commit comments

Comments
 (0)