2

我试图beacon在后台每 30 秒检测一次,当应用程序在前台时,它应该不断检测信标,没有任何间隔。为此,这是我编写的代码,

public void onCreate() {
    super.onCreate();
    mBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
    mBeaconManager.getBeaconParsers().clear();
    mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
    Identifier myBeaconNamespaceId = Identifier.parse("0xe2bfcc3cc2370789caef");
    Region region = new Region("my-beacon-region", myBeaconNamespaceId, null, null);

    mBeaconManager.setBackgroundBetweenScanPeriod(25000l);
    mBeaconManager.setBackgroundScanPeriod(5000l);
    mBeaconManager.setForegroundScanPeriod(60000l);
    mBeaconManager.setForegroundBetweenScanPeriod(0l);

    try {
        mBeaconManager.updateScanPeriods();
    } catch (RemoteException e) {
        e.printStackTrace();
    }

    regionBootstrap = new RegionBootstrap(this, region);
    backgroundPowerSaver = new BackgroundPowerSaver(this);

}

即使我设置了特定的扫描周期,它仍然会以默认的时间间隔检测信标。谢谢。

4

1 回答 1

0

了解将前台扫描间隔设置为:

mBeaconManager.setForegroundScanPeriod(60000l);

不会不断地交付结果。它将扫描 60 秒,然后仅在该间隔结束时提供结果。

此外,调用updateScanInterval将始终抛出异常,因为服务尚未绑定。这应该没问题,因为代码预先初始化了新的扫描间隔,因此当服务绑定稍后发生时,扫描间隔将生效。

于 2015-12-07T12:34:06.367 回答