Android Altbeacon 导致扫描计划任务

Android Altbeacon Causing Scan Scheduled Tasks

我正在使用 altbeacon 库中的 运行ge 通知程序 运行ge 获取信标 30 秒。 运行ge 通知程序在应用程序 class 中并且没有实现 bootstrap 通知程序或 beaconconsumer。当运行在前台的程序产生下面的代码,它应该永远不会到达后台。

如果应用程序 运行 在前台,为什么会发生这种情况?

关于 phone: Android 9.0 Google 像素 2 库版本:Altbeacon 2.15.2

11-28 16:43:44.052 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
 11-28 16:44:24.276 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
11-28 16:44:24.332 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
11-28 16:44:24.426 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
11-28 16:44:25.010 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms

以下是我认为与 altbeacon 库相关的代码。

代码上下文:我在扩展应用程序的 class 中实现了 运行ge 通知程序和 bootstrap 通知程序。 StartRanging 方法在调用 Bootstrap 通知程序方法时或从有问题的 activity 调用。当用户点击另一个 activity 的按钮时,将调用 activity,他们可以随时返回到旧的 activity。一旦他们在这两个活动之间进行,就会生成上述日志。我只 运行ge 30 秒,一旦这 30 秒结束,停止 运行ging 被调用。

Class 扩展应用程序:

创建时:

    beaconManager = BeaconManager.getInstanceForApplication(this);

    //Set the time the app last scanned
    scanningTime = 0;
    backgroundPowerSaver = new BackgroundPowerSaver(this);

    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); //iBeacon

    Region beaconRegion1 = new Region("Beacon1", Identifier.parse("HidingUUID1"), null, null);
    Region beaconRegion2 = new Region("Beacon2", Identifier.parse("HidingUUID2"), null, null);
    Region beaconRegion3 = new Region("Beacon3", Identifier.parse("HidingUUID3"), null, null);
    Region beaconRegion4 = new Region("Beacon4", Identifier.parse("HidingUUID4"), null, null);
    Region beaconRegion5 = new Region("Beacon5", Identifier.parse("HidingUUID5"), null, null);

    rangingRegion = new Region("RangingRegion", null, null, null);
    regionBootstrap = new RegionBootstrap(this, beaconRegion1);
    regionBootstrap.addRegion(beaconRegion2);
    regionBootstrap.addRegion(beaconRegion3);
    regionBootstrap.addRegion(beaconRegion4);
    regionBootstrap.addRegion(beaconRegion5);

Bootstrap 通知方法:

@Override
public void didEnterRegion(Region region) {
    beaconRegionEvent = true;
    StartRanging();
}

@Override
public void didExitRegion(Region region) {
    beaconRegionEvent = true;
    StartRanging();
}

@Override
public void didDetermineStateForRegion(int i, Region region) {
    if (i == 1) {
        beaconRegionEvent = true;
        StartRanging();
    }
}

StartRanging:检查 运行ging 是否正在发生,如果没有则开始 运行ging。

public boolean StartRanging() {

    long now = System.currentTimeMillis();
    if (now - scanningTime <= Globals.MAX_RANGING_PERIOD * 1000 && scanningTime != 0) {
        Timber.e("Minimum time since previous scan has not passed");
        return false;
    }
    try {
        beaconManager.startRangingBeaconsInRegion(rangingRegion);
        Timber.e("Starting Range");

        //Scanning is about to happen so set the scan time
        scanningTime = System.currentTimeMillis();

    } catch (RemoteException e) {
        Timber.e("Unable to initialize ranging");
        e.printStackTrace();
        return false;
    }

    //Add this range notifier to the beacon manager
    if (beaconManager.getRangingNotifiers().size() < 1) {
        beaconManager.addRangeNotifier(this);
    }

    return true;
}

停止测距:

public void StopRanging() {
    try {
        //Remove the regions being ranged
        beaconManager.stopRangingBeaconsInRegion(rangingRegion);
    } catch (RemoteException e) {
        e.printStackTrace();
        Timber.e("Cant stop ranging  " + e.toString());
    }
    Timber.d("Stopped Beacon Ranging");
}

RangeBeaconsInRegion

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
    long now = System.currentTimeMillis();
    if (now - scanningTime > Globals.MAX_RANGING_PERIOD * 1000 && scanningTime != 0) {
        StopRanging();
    }

}

Activity 代码中扫描过于频繁:每秒打开此 activity 我们调用 StartRanging 方法。

Runnable 在 onCreate() 中被调用

Runnable ScanTimer = new Runnable() {
    @Override
    public void run() {
        Timber.d("In Runnable");
        handler.postDelayed(ScanTimer, 1000);

        Timber.d("Checks were good starting ranging");

        App.getInstance().StartRanging();

    }
};

了解信标监控 API(从 RegionBootstrap 开始)和测距 API(从 startRangingBeaconsInRegion 开始)都需要信标扫描,这是由计划作业在库中实现的以进行扫描。

因此,即使代码停止在计时器上进行测量,它也不会停止监视,它会无限期地继续下去。这就是日志显示计划继续执行扫描的作业的原因。如果要停止所有扫描,必须使用 regionBootstrap.disable();

禁用监控