forked from noble/noble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenter-exit.js
54 lines (41 loc) · 1.27 KB
/
enter-exit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Continously scans for peripherals and prints out message when they enter/exit
In range criteria: RSSI < threshold
Out of range criteria: lastSeen > grace period
based on code provided by: Mattias Ask (http://www.dittlof.com)
*/
var noble = require('../index');
var RSSI_THRESHOLD = -90;
var EXIT_GRACE_PERIOD = 2000; // milliseconds
var inRange = [];
noble.on('discover', function(peripheral) {
if (peripheral.rssi < RSSI_THRESHOLD) {
// ignore
return;
}
var id = peripheral.id;
var entered = !inRange[id];
if (entered) {
inRange[id] = {
peripheral: peripheral
};
console.log('"' + peripheral.advertisement.localName + '" entered (RSSI ' + peripheral.rssi + ') ' + new Date());
}
inRange[id].lastSeen = Date.now();
});
setInterval(function() {
for (var id in inRange) {
if (inRange[id].lastSeen < (Date.now() - EXIT_GRACE_PERIOD)) {
var peripheral = inRange[id].peripheral;
console.log('"' + peripheral.advertisement.localName + '" exited (RSSI ' + peripheral.rssi + ') ' + new Date());
delete inRange[id];
}
}
}, EXIT_GRACE_PERIOD / 2);
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning([], true);
} else {
noble.stopScanning();
}
});