可以通过在BeaconManager类中设置RangeNotifier的时间间隔来更改区域通知器更新的时间间隔。以下是设置时间间隔为5秒的示例代码:
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.setForegroundScanPeriod(5000); //设置时间间隔为5秒
beaconManager.setForegroundBetweenScanPeriod(0); //扫描间隔为0ms
Region region = new Region("myRegion", null, null, null);
beaconManager.startRangingBeaconsInRegion(region);
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection beacons, Region region) {
if (beacons.size() > 0) {
Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");
}
}
});
在此示例中,我们将setForegroundScanPeriod()方法的参数设为5000,从而设置了时间间隔为5秒。beaconManager.setForegroundBetweenScanPeriod(0)方法将扫描间隔设置为0,这意味着在前台扫描期间将不会有任何非扫描时间。
请注意,在后台扫描期间,时间间隔将自适应地更改。如果要更改后台扫描间隔,请使用setBackgroundScanPeriod()和setBackgroundBetweenScanPeriod()方法。
上一篇:androidbatteryhistorian总是显示ble扫描。
下一篇:AndroidBeaconlibrarybackgroundscanningviasetIntentScanningStrategyEnabled(true)notdetectingbeacon