通常,进行新扫描需要 1 秒。
如果扫描是1sec,如果我想解析数据,我可能不会做太多事情。
如何更改该值?
另外,对于后台服务,如何更改 freq 的值?
如果你想改变Android iBeacon Library的默认扫描间隔,你可以使用如下代码:
try {
iBeaconManager.setForegroundScanPeriod(1100l); // 1100 mS
iBeaconManager.setForegroundBetweenScanPeriod(0l); // 0ms
iBeaconManager.updateScanPeriods();
}
catch (RemoteException e) {
Log.e(TAG, "Cannot talk to service");
}
上面的代码将扫描设置为持续 1100 毫秒,并在扫描之间等待 0 毫秒。这是默认行为。
如果您希望减慢扫描速度,通常将 设置betweenScanPeriod
为非零值,例如 30 秒。执行此操作时,您应该将 设置scanPeriod
为至少您的 iBeacons 广播的时间间隔——1100 毫秒通常足以捕捉 1Hz 速率的 iBeacons 广告。
try {
iBeaconManager.setForegroundScanPeriod(1100l); // 1100 mS
iBeaconManager.setForegroundBetweenScanPeriod(30000l); // 30,000ms = 30 seconds
iBeaconManager.updateScanPeriods();
}
catch (RemoteException e) {
Log.e(TAG, "Cannot talk to service");
}
值得注意的是,在 上实际上有两种这样的设置iBeaconManager
,一种用于前景,一种用于背景。这纯粹是为了方便,因为您通常希望在您的应用程序处于后台时不那么频繁地进行扫描。无论您自定义前台和后台设置还是接受默认设置,您都可以通过以下方式激活后台模式设置:
try {
iBeaconManager.setBackgroundMode(myActivity, true);
}
catch (RemoteException e) {
Log.e(TAG, "Cannot talk to service");
}
There is also a Pro version of the Android iBeacon Library which builds on the above to automatically put your app into foreground or background mode depending on whether an Activity is visible. This is the easiest way to set up background power savings on Android.