'Getting duplicate beacon values [Flutter 'beacons_plugin']
We are trying to get beacon values respect to the location point. I have a scan_cubit.dart where I manage the scanning things. I am triggering cubit's startScanning() and endScanning() methods from UI layer. There is scanTime property which is providing from package, inside beacon data. What the problem is that: when I look the values, there are same values twice or thrice. I put the
DateTime _now = DateTime.now();
print(
"Plugin receives: $data\nOn time ${_now.hour}:${_now.minute}:${_now.second}.${_now.millisecond}");
lines and saw the milliseconds of duplicate datas are different. I think I've done things correctly according to the package's example. Also if I put _initPlatformState() and await BeaconsPlugin.startMonitoring() again in startScanning() as stated in example, there is an extreme delay occurs(10-12 secs.) before starting measuring. That's why I didn't put them.
scan_cubit.dart
class ScanCubit extends Cubit<ScanState> {
bool isRunning = false;
.......
some more code unrelated to beacons
.......
ScanCubit({required this.bluetoothStatusCubit}) : super(NotScanning()) {
_monitorBluetoothStatus();
_initPlatformState();
}
void startScanning(int indoorLevel, double lat, double long) async {
currentPoint = PointModel(lat: lat, long: long, indoorLevel: indoorLevel);
//_initPlatformState();
//await BeaconsPlugin.startMonitoring();
isRunning = true;
}
void endScanning() async {
isRunning = false;
//await BeaconsPlugin.stopMonitoring();
_emitEndScanning();
//mini delay to show circle progress
await Future.delayed(const Duration(milliseconds: 300));
_emitNotScanning();
}
Future<void> _initPlatformState() async {
if (Platform.isAndroid) {
//Prominent disclosure
await BeaconsPlugin.setDisclosureDialogMessage(
title: "Background Locations",
message:
"[This app] collects location data to enable [feature], [feature], & [feature] even when the app is closed or not in use");
//Only in case, you want the dialog to be shown again. By Default, dialog will never be shown if permissions are granted.
//await BeaconsPlugin.clearDisclosureDialogShowFlag(false);
}
if (Platform.isAndroid) {
BeaconsPlugin.channel.setMethodCallHandler((call) async {
print("Method: ${call.method}");
if (call.method == 'scannerReady') {
await BeaconsPlugin.startMonitoring();
//isRunning = true;
}
});
} else if (Platform.isIOS) {
await BeaconsPlugin.startMonitoring();
//isRunning = true;
}
BeaconsPlugin.listenToBeacons(beaconEventsController);
await BeaconsPlugin.addRegion(
"BeaconType1", "909c3cf9-fc5c-4841-b695-380958a51a5a");
await BeaconsPlugin.addRegion(
"BeaconType2", "6a84c716-0f2a-1ce9-f210-6a63bd873dd9");
BeaconsPlugin.addBeaconLayoutForAndroid(
"m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
BeaconsPlugin.addBeaconLayoutForAndroid(
"m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
BeaconsPlugin.setForegroundScanPeriodForAndroid(
foregroundScanPeriod: 1100, foregroundBetweenScanPeriod: 0);
BeaconsPlugin.setBackgroundScanPeriodForAndroid(
backgroundScanPeriod: 1100, backgroundBetweenScanPeriod: 0);
beaconEventsController.stream.listen(
(data) {
if (isRunning && data.isNotEmpty) {
DateTime _now = DateTime.now();
print(
"Plugin receives: $data\nOn time ${_now.hour}:${_now.minute}:${_now.second}.${_now.millisecond}\n");
_emitScanningNow(currentPoint, data);
}
},
onDone: () {},
onError: (error) {
print("Error: $error");
});
await BeaconsPlugin.runInBackground(true);
}
And here is the debug console (look carefully scanTime and On time lines):
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.2",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:18 PM",
I/flutter (32085): "rssi": "-46",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:18.281
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.2",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:18 PM",
I/flutter (32085): "rssi": "-46",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:18.285
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
E/ (32085): [ZeroHung]zrhung_get_config: Get config failed for wp[0x0008]
E/ (32085): [ZeroHung]zrhung_get_config: Get config failed for wp[0x0008]
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1F:BE",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.22",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:19 PM",
I/flutter (32085): "rssi": "-64",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:19.378
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1F:BE",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.22",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:19 PM",
I/flutter (32085): "rssi": "-64",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:19.386
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1F:BE",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.22",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:19 PM",
I/flutter (32085): "rssi": "-64",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:19.388
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
E/ (32085): [ZeroHung]zrhung_get_config: Get config failed for wp[0x0008]
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.21",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:20 PM",
I/flutter (32085): "rssi": "-49",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:20.476
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.21",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:20 PM",
I/flutter (32085): "rssi": "-49",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:20.481
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.21",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:20 PM",
I/flutter (32085): "rssi": "-49",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:20.485
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.21",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:21 PM",
I/flutter (32085): "rssi": "-46",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:21.592
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.21",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:21 PM",
I/flutter (32085): "rssi": "-46",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
I/flutter (32085): On time 12:52:21.594
I/flutter (32085): Change { currentState: Instance of 'ScanningNow', nextState: Instance of 'ScanningNow' }
I/flutter (32085): Plugin receives: {
I/flutter (32085): "name": "null",
I/flutter (32085): "uuid": "4152554e-f99b-4a3b-86d0-947070693a78",
I/flutter (32085): "macAddress": "B0:91:22:4B:1A:6A",
I/flutter (32085): "major": "0",
I/flutter (32085): "minor": "0",
I/flutter (32085): "distance": "0.21",
I/flutter (32085): "proximity": "Immediate",
I/flutter (32085): "scanTime": "18 March 2022 12:52:21 PM",
I/flutter (32085): "rssi": "-46",
I/flutter (32085): "txPower": "-60"
I/flutter (32085): }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
