Android Altbeacon导致扫描计划任务

时间:2018-11-29 01:13:36

标签: android bluetooth altbeacon

我正在使用altbeacon库中的范围通知程序来对信标进行30秒的范围调整。范围通知程序位于Application类中,并且没有实现引导程序通知程序或信标消费者。该程序在前台运行时会产生以下代码,它永远不会到达后台。

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

关于手机:Android 9.0 Google Pixel 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库相关的代码。

代码上下文:我已经在扩展Application的类中实现了范围通知程序和引导程序通知程序。当Bootstrap通知程序方法被调用时或从有问题的活动中调用StartRanging方法。当用户点击另一个活动中的按钮时,将调用该活动,他们可以随时返回到旧的活动。一旦它们进入这两个活动之间,就会生成上述日志。我只进行30秒的范围调整,一旦这30秒结束,就会调用范围调整。

扩展应用程序的类:

创建时:

    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:检查是否正在发生测距,如果没有,则开始测距。

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

StopRanging:

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

}

扫描频繁发生在代码中的活动:此活动打开的每一秒钟,我们称为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();

    }
};

1 个答案:

答案 0 :(得分:1)

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

因此,即使代码停止在计时器上运行,它也不会停止监视,监视将无限期地继续。这就是为什么日志显示计划安排扫描的连续作业的原因。如果要停止所有扫描,则必须使用regionBootstrap.disable();

禁用监视