Here’s what I’ve been using for enter/exit events in my own projects:
var WHITELISTED_BEACONS = [
'76e88a19542a64f7c32c7af48192d129', // lemon
'3a7dc6f68d15043f9f83097dcb5b5822', // candy
'17d8274ffe6eb174e6dabd081923dd35', // beetroot
];
var BEACON_EXIT_TIMEOUT = 60; // seconds
var beaconsInRange = {};
ble.startScan((scanRecord) => {
var packet = ble.parse(scanRecord);
// ignore non-Estimote-Location packets
if (!packet || packet.type !== 'est_loc') return;
// ignore non-whitelisted beacons
if (WHITELISTED_BEACONS.indexOf(packet.id) === -1) return;
if (beaconsInRange[packet.id]) {
// already in range of this beacon, reset the exit timer
print('still in range: ' + packet.id);
beaconsInRange[packet.id].reset();
} else {
// just came in range of this beacon, trigger an enter...
print('enter: ' + packet.id);
cloud.enqueue('beacon-enter-range', {beaconId: packet.id});
sync.now();
// ...and schedule an exit timer if we don't hear from the beacon for BEACON_EXIT_TIMEOUT seconds
beaconsInRange[packet.id] = timers.single(BEACON_EXIT_TIMEOUT * 1000, () => {
// exit timer kicked in, trigger an exit
print('exit: ' + packet.id);
cloud.enqueue('beacon-exit-range', {beaconId: packet.id});
sync.now();
delete beaconsInRange[packet.id];
});
}
}, 0)