I don't seem to really understand the difference between the MonitoringListener and the RangingListener.
In my particular use case I would like to constantly know all beacons within range, and would like to know when a region associated with any of them is exited.
This is a snippet of what I'm talking about:
beaconManager.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
}
});
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> beacons) {
}
@Override
public void onExitedRegion(Region region) {
}
});
I don't really get the difference between the onBeaconsDiscovered and onEnteredRegion methods. When you start listening for any one of them you pass a Region as a parameter, so that confuses me a little bit more, since at first glance I assumed the first one was just constantly searching while the other one just looked for a specific Region.
Thanks!
The difference is not really in the callbacks themselves, but rather in how and when they get called.
MonitoringListener.onEnteredRegion
and MonitoringListener.onExitedRegion
are triggered whenever you cross the boundary of the region passed to BeaconManager.startMonitoring
. Once you enter the region and onEnteredRegion
gets called, you won't get another notification unless you exit and then re-enter the region.
On the contrary, RangingListener.onBeaconsDiscovered
is being triggered continuously (by default: every 1 second) and gets you a list of beacons discovered by your Android device - as long as they match the region passed to BeaconManager.startRanging
.
Imagine you have a beacon with UUID = X, major = Y and minor = Z. You define your region as follows:
Region region = new Region("myRegion", X, Y, Z)
And then start both ranging and monitoring:
beaconManager.startRanging(region);
beaconMangeer.startMonitoring(region);
The timeline of callbacks invocations may look like this:
# assuming you start outside the range of the beacon
1s: onBeaconsDiscovered(<myRegion>, <empty list of beacons>)
2s: onBeaconsDiscovered(<myRegion>, <empty list of beacons>)
3s: onBeaconsDiscovered(<myRegion>, <empty list of beacons>)
# now you move closer to the beacon, ending up in its range
4s: *onEnteredRegion*(<myRegion>, <a list with the "X/Y/Z" beacon>)
+ onBeaconsDiscovered(<myRegion>, <a list with the "X/Y/Z" beacon>)
5s: onBeaconsDiscovered(<myRegion>, <a list with the "X/Y/Z" beacon>)
6s: onBeaconsDiscovered(<myRegion>, <a list with the "X/Y/Z" beacon>)
7s: onBeaconsDiscovered(<myRegion>, <a list with the "X/Y/Z" beacon>)
# now you move out of the beacon's range again
8s: *onExitedRegion*(<myRegion>)
+ onBeaconsDiscovered(<myRegion>, <empty list of beacons>)
9s: onBeaconsDiscovered(<myRegion>, <empty list of beacons>)
10s: onBeaconsDiscovered(<myRegion>, <empty list of beacons>)
Note that in reality, Bluetooth scanning might not be as responsive as in the example above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With