我似乎不太了解MonitoringListener和RangingListener之间的区别。
在我的特定用例中,我想不断了解范围内的所有信标,并想知道何时退出与它们中的任何一个相关联的区域。
这是我正在谈论的内容的摘要:
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) {
}
});
我并没有真正理解onBeaconsDiscovered和onEnteredRegion方法之间的区别。当您开始收听其中的任何一个时,都将Region作为参数传递给我,这使我感到困惑,因为乍一看,我认为第一个只是在不断搜索,而另一个则只是在寻找特定的Region(区域)。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
谢谢!
最佳答案
真正的区别不是回调本身,而是调用方式和调用时间。
每当您越过传递给MonitoringListener.onEnteredRegion
的区域的边界时,就会触发MonitoringListener.onExitedRegion
和BeaconManager.startMonitoring
。输入区域并调用onEnteredRegion
后,除非退出然后重新进入该区域,否则不会再收到其他通知。
相反,RangingListener.onBeaconsDiscovered
会连续触发(默认情况下:每1秒一次),并为您提供Android设备发现的信标列表,只要它们与传递给BeaconManager.startRanging
的区域匹配即可。
例子:
假设您有一个UUID = X,major = Y和minor = Z的信标。您可以如下定义区域:
Region region = new Region("myRegion", X, Y, Z)
然后开始测距和监视:
beaconManager.startRanging(region);
beaconMangeer.startMonitoring(region);
回调调用的时间线可能如下所示:
# 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>)
请注意,实际上,蓝牙扫描的响应速度可能不如上例所示。
关于android - Estimote信标-监听和测距监听器之间的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25586830/