8

我似乎不太了解 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 方法之间的区别。当您开始侦听其中任何一个时,您将区域作为参数传递,这让我更加困惑,因为乍一看,我认为第一个只是不断搜索,而另一个只是在寻找特定的区域。

谢谢!

4

1 回答 1

17

区别并不在于回调本身,而在于它们被调用的方式和时间。

MonitoringListener.onEnteredRegionMonitoringListener.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>)

请注意,实际上,蓝牙扫描的响应速度可能不如上面的示例。

于 2014-09-02T09:19:38.460 回答