I'm trying to fix a bug that occurs when, running my app (ionic 4) on an android device and monitoring for beacons regions i turn off the device bluetooth and then turn it on again.
This the process:
- run my app with bluetooth on
- startMonitoringForRegion
- didDetermineStateForRegion --> works
- Turn Off device bluetooth
- stopMonitoringForRegion
- Turn On device bluetooth
- didDetermineStateForRegion is not fired
IMPORTANT --> Ugly solution with setTimeout --> SOLVE THE ISSUE --> Here a stackoverflow with this workaround.
---------- HERE MY BEACONS OBJECT
this.beaconsFromJson = [
{
identifier: 'Mini S/N 018727',
uuid: 'my-beacon-uuid',
major: 1,
minor: 18727,
notifyEntryStateOnDisplay: false
},
{
identifier: 'Mini S/N 018730',
uuid: 'my-beacon-uuid',
major: 1,
minor: 18730,
notifyEntryStateOnDisplay: false
}
];
---------- HERE MY SCAN FUNCTION
scan() {
_.forEach( this.beaconsFromJson, beacon => {
const beaconRegion = this.ibeacon.BeaconRegion(
beacon.identifier,
beacon.uuid,
beacon.major,
beacon.minor,
beacon.notifyEntryStateOnDisplay
)
this.ibeacon.startMonitoringForRegion(beaconRegion).then(
() => console.log('Start monitoring' + beaconRegion.identifier),
(error) =>
console.error('Native layer failed to begin monitoring: ', error)
);
}) // end foreach
setTimeout( () => {
this.delegate.didDetermineStateForRegion().subscribe((data: IBeaconPluginResult) => {
switch (data.state) {
case 'CLRegionStateInside':
this.beacons.push(data.region);
this.loadingService.loading$.next({status: false});
break;
case 'CLRegionStateOutside':
this.loadingService.loading$.next({status: false});
this.beacons = this.beacons.filter(beacon => beacon.identifier !== data.region.identifier);
break;
default:
break;
}
})
}, 1); // Ugly but it works --> https://stackoverflow.com/a/25211343/4197536
}
- When i start my app with bluetooth OFF and then i turn it ON -> It works as expected.
- When i start my app with bluetooth ON and then i turn it OFF and ON again --> Cannot read property 'subscribe' of undefined (that subscription i do to didDetermineStateForRegion())
Nb: This script works, but i'm looking for a better solution!