I have written a short code in order to scan BLE beacons and send it periodically to our platform.
This code is working well when I use the Estimote IDE interface and while I am connected to the LTE beacon … but not at all as soon as I disconnect the beacon.
Any idea of what can happen ?
Below my code:
var SCAN_INTERVAL = 30 * 1000; // 30 seconds in milliseconds
var SCAN_DURATION = 10 * 1000; // 10 seconds in milliseconds
var TIBIB_SCAN = 0; // 0=False 1=True
var TIBIB_MAX=25; // Maximum nber of detected by sync
app.setErrorHandler((type, msg) => print(msg))
var batteryPercentage=sensors.battery.getPerc()
print('Battery level: ' + batteryPercentage);
var deviceId=sys.getPublicId()
print('DeviceId is : ' + deviceId)
/* sync.setSyncPeriod(60 * 3); //synchro every 3 minutes */
io.press(() => {
TIBIB_SCAN=0;
print('Stop SCAN ... TIBIB_SCAN = '+TIBIB_SCAN);
io.led(io.Led.BLE,false); // turn OFF LED with Bluetooth icon
io.setLedColor(io.Led.BLE,[0,0,0]); // RGB = Blue
sync.now();
}, io.PressType.LONG); // more than 3s
io.press(() => {
TIBIB_SCAN=1;
print('Start SCAN ... TIBIB_SCAN = '+TIBIB_SCAN);
print('Button pressed');
io.led(io.Led.BLE,true); // turn ON LED with Bluetooth icon
io.setLedColor(io.Led.BLE,[0,0,1]); // RGB = Blue
});
var detectedBeacons = {};
timers.repeat(SCAN_INTERVAL, () => {
if (TIBIB_SCAN==1) {
ble.startScan(
// 1st argument -- callback to process each scan result
(scanResult) => {
if (scanResult.name === "ticatag") {
var macAddress='';
var str=scanResult.addr;
/*for (i=str.length; i > 0; i=i-2) {
macAddress=macAddress+str.substring(i-2, i);
}*/
macAddress= str.toUpperCase()
var data = {estimoteHubId: deviceId, addr: macAddress, timestamp: new Date().toISOString()}
detectedBeacons[macAddress]= data
}
},
// 2nd argument -- how long to scan for
SCAN_DURATION
).then(() => {
if (detectedBeacons.length===0) {
print('No beacon to sync');
return;
}
var keys = Object.keys(detectedBeacons);
var beacons = [];
print('Detected Length =' + keys.length);
k=keys.length;
i1=0
i2=k-1
if (i2 >= TIBIB_MAX) i2=TIBIB_MAX-1;
print('k =' + k);
while (i1 < k) {
print(' i1 = '+i1+ ' / i2 = '+i2+ ' k = '+k);
for (i=i1; i < i2; i++) {
var key = keys[i];
var beacon = detectedBeacons[key];
//print(JSON.stringify(beacon));
beacons.push(beacon);
}
j=i2-i1+1;
print('Sending the events to server: ' + j);
cloud.enqueue('assets-update', {'assets': beacons});
sync.now();
print('Syncing data');
beacons=[];
i1=i2+1;
i2=i2+TIBIB_MAX;
if (i2>k) i2=k-1;
}
detectedBeacons = {};
});
}
});