我是测距信号,我的目标是处理didRangeBeaconsInregion中的信标集合,以便我在集合中获得最接近的信标,并在屏幕上显示与信标itselt相关的字符串(beacon1 = red,beacon2 = blue ...) . 我目前的ibeacons广告费率为1Hz(我无法配置它们来增加它) .
我尝试过以下方法:
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Beacon closest = null;
double distance = 100.0;
logToDisplay(beacons.size() + " beacons in collection");
for (Beacon beacon : beacons) {
if (distance > beacon.getDistance()) {
closest = beacon;
distance = beacon.getDistance();
}
}
if (closest == null) {
logToDisplay(getCurrentTimeStamp() + " | Closest = null");
}
else if (closest.getId3().toInt() == 1) {
logToDisplay(getCurrentTimeStamp() + " | BLUE");
}
else if (closest.getId3().toInt() == 3) {
logToDisplay(getCurrentTimeStamp() + " | RED");
}
else if (closest.getId3().toInt() == 4) {
logToDisplay(getCurrentTimeStamp() + " | GREEN");
} // ... and so on with some more colours
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
据我所知,didRangeBeaconsinRegion有一个信标集合,它是最后一秒看到的信标的缓冲区 . 由于我的信标只有1Hz的广告费率,所以收藏量非常小,我无法获得一致的结果 .
我认为有两种解决方案:
-
提高广告费率(暂时无法实现) .
-
从didRangeBeaconsinRegion方法增加1秒周期,以便信标集合缓冲最近5秒内看到的信标(例如) .
是否有可能做到这一点?我该怎么做?这个设置是否像 beaconManager.setForegroundScanPeriod(5000l);
?
1 回答
是的,你可以做你说的,它应该有助于使结果更加一致 .
在第一次获得
beaconManager
实例后立即添加您提到的行:beaconManager.setForegroundScanPeriod(5000l);