我正在我的安卓设备上使用 AltBeacon 示例应用程序 - altbeacon.org 提供的示例应用程序在这里: https://github.com/AltBeacon/android-beacon-library-reference
但是, 启动时应用程序仅检测并显示一个信标.我的安卓设备附近有大约 5 个信标.如何检测所有信标?
在RrangeingActivity.java中,我注意到当信标出现在视线中时调用的此方法:
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() > 0) {
EditText editText = (EditText) RangingActivity.this.findViewById(R.id.rangingText);
Beacon firstBeacon = beacons.iterator().next();
logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
}
}
}
我修改了迭代器以在 while 循环中从集合中读取,如下所示:
Beacon firstBeacon;
while(beacons.iterator().hasNext()){
firstBeacon = beacons.iterator().next();
logToDisplay("The first beacon " + firstBeacon.toString() + " is about " + firstBeacon.getDistance() + " meters away.");
}
但是,应用程序会通过此修改崩溃。
我的问题:
(1) 如何显示安卓设备附近的所有信标?
(2) 如何检测超出区域的信标?
对于 1。 我认为您需要使用 For 循环。 喜欢这个。
for (Beacon beacon : beacons) {
logToDisplay("The beacon " + beacon.toString() + " is about " + beacon.getDistance() + " meters away.");
}
对于 2. 我无法检测到这一点,但这可能是一个很长的超时时间。 所以要非常耐心。 我认为可以更改监视活动中的代码以显示一条消息。 或者,您可以从设备查看日志。 一个简单的logToDisplay可以在BeaconReferenceApplication的didExitRegion部分使用。
public void didExitRegion(Region region) {
if (monitoringActivity != null) {
monitoringActivity.logToDisplay("I no longer see a beacon in the "+region.getUniqueId());
}
}