【问题标题】:setBackgroundBetweenScanPeriod() not taking effectsetBackgroundBetweenScanPeriod() 没有生效
【发布时间】:2015-12-07 07:26:01
【问题描述】:

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

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);

}

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

【问题讨论】:

    标签: android beacon eddystone


    【解决方案1】:

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

    mBeaconManager.setForegroundScanPeriod(60000l);
    

    不会持续提供结果。它将扫描 60 秒,然后仅在该间隔结束时提供结果。

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

    【讨论】:

    • 谢谢,现在我知道我必须将前台扫描周期设置为 2000 毫秒左右。但是如何让我的应用在后台每 30 秒查找一次信标,它仍然在后台每约 5 分钟查找一次信标……我应该对现有代码进行哪些更改?
    • mBeaconManager.setBackgroundBetweenScanPeriod(30000l); /* 30 秒 */
    • 那也行不通。但是我在这里注意到了一件事。我的应用程序中有一个警报管理器,每当警报触发时,就会检测到信标。所以现在我要发出一个警报,每 30 秒触发一次。这可能不是理想的解决方案......但它仍然有效:)
    猜你喜欢
    • 2019-09-26
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 2016-08-25
    • 2020-02-01
    • 2019-10-02
    相关资源
    最近更新 更多