onBeaconData method
dynamic
onBeaconData(
- List<Beacon> beacons
)
override
Implementation
@override
onBeaconData(List<Beacon> beacons) {
// enabled?
if (!enabled) return;
// if no beacons found on last
// iteration and none in this,
// just quit
if (_beaconsFound == 0 && beacons.isEmpty) return _removeExpired();
// rember last count of beacons found
_beaconsFound = beacons.length;
Log().debug('BEACON Scanner -> Found ${beacons.length} beacons');
// remove beacons that dont match our criteria
if (major != null || minor != null || distance != null) {
beacons.removeWhere((element) {
if (major != null && element.major != major) return true;
if (minor != null && element.minor != minor) return true;
if (distance != null && element.accuracy > distance!) return true;
return false;
});
}
// sort by distance
if (beacons.length > 1) {
beacons.sort((a, b) => Comparable.compare(a.accuracy, b.accuracy));
}
Log().debug(
'BEACON Scanner -> ${beacons.length} beacons matching your criteria');
// Build the Data
Data data = Data();
for (var beacon in beacons) {
// calculate age
int age = 0;
if (beacon.macAddress != null) {
var dt = DateTime.now().millisecondsSinceEpoch;
if (!firstSeen.keys.contains(beacon.macAddress!)) {
firstSeen[beacon.macAddress!] = dt;
}
lastSeen[beacon.macAddress!] = dt;
age = lastSeen[beacon.macAddress!]! - firstSeen[beacon.macAddress!]!;
}
Map<dynamic, dynamic> map = <dynamic, dynamic>{};
map["id"] = beacon.proximityUUID;
map["epoch"] = "${DateTime.now().millisecondsSinceEpoch}";
map["age"] = "$age";
map["macaddress"] = beacon.macAddress;
map["rssi"] = "${beacon.rssi}";
map["power"] = "${beacon.txPower ?? 0.0}";
map["minor"] = "${beacon.minor}";
map["major"] = "${beacon.major}";
map["distance"] = "${beacon.accuracy}";
map["proximity"] = "${beacon.proximity}";
data.add(map);
}
// remove any items that haven't been seen in the past 15 seconds
_removeExpired();
// Notify
onSuccess(data);
}