ScanJob 在 15 分钟后继续调用,并在区域匹配时停止

ScanJob continues beign invoked after the 15 min period and stops when a region matches

根据我在 AltBeacon 库文档中的理解,ScanJob 应该在与应用程序最后一次交互 15 分钟后停止调用。

我已经开始监视和测距某些区域,并且我已禁用所有信标,因此未检测到任何信标。启动应用程序后,我将其终止,并且在 logcat 中看到即使在 15 分钟后仍会连续调用 ScanJob。

03-22 14:17:46.047 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:46.061 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:17:46.061 10755-10755 W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:46.061 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:46.061 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:46.062 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:46.178 10755-10755/ I/CycledLeScanner: Using Android O scanner
03-22 14:17:46.180 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:46.180 10755-10755/ I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@6c0d823
03-22 14:17:46.181 10755-10755/ I/ScanJob: scanJob version 2.13.1 is starting up on the main process
03-22 14:17:46.184 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:46.184 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:46.184 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:46.234 10755-10755/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:46.244 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:46.246 10755-12617/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:46.250 10755-10755/ I/ScanJob: Scan job running for 10000 millis
03-22 14:17:46.250 10755-11219/ D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=6 mScannerId=0
03-22 14:17:46.725 10755-12622/ I/ScanHelper: Non-distinct packets detected in a single scan.  Restarting scans unecessary.
03-22 14:17:56.251 10755-10755/ I/ScanJob: Scan job runtime expired: org.altbeacon.beacon.service.ScanJob@6c0d823
03-22 14:17:56.262 10755-12617/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.299 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:56.302 10755-10755/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.533 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:56.542 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:56.663 10755-10755/ I/CycledLeScanner: Using Android O scanner
03-22 14:17:56.666 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:56.667 10755-10755/ I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@ab5d8a
03-22 14:17:56.668 10755-10755/ I/ScanJob: scanJob version 2.13.1 is starting up on the main process
03-22 14:17:56.669 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:56.670 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:56.671 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:56.718 10755-10755/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.730 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:56.733 10755-12627/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.739 10755-10755/ I/ScanJob: Scan job running for 10000 millis
03-22 14:17:56.739 10755-11219/ D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=6 mScannerId=0

如果我没有与phone互动,而且没有任何信标与监控区域相匹配,它不是应该在15分钟后停止吗?

此外,当我连接信标时,它被检测到,但随后 ScanJob 停止调用。

03-22 14:19:55.150 10755-10755/ D/.MyApplication: I have just switched from seeing/not seeing beacons: 1
03-22 14:19:55.152 10755-10755/ D/.MyApplication: did enter Bootstrap region.id1: 0040c159-12f6-4fc3-9189-87c069ffe5cf id2: 5 id3: null
03-22 14:19:55.152 10755-10755/ D/.MyApplication: isAnyConsumerBound? -->false
03-22 14:19:55.186 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.188 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.232 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.233 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.276 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.278 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.321 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.322 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.368 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.369 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.412 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.413 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.468 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.469 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.527 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.529 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.584 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.585 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.638 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.639 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.639 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.639 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.640 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.640 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.659 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.659 10755-10755/ I/ScanJob: onStopJob called for immediate scan org.altbeacon.beacon.service.ScanJob@e0ea913
03-22 14:19:55.663 10755-12713/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:19:55.664 10755-10755/ I/ScanJob: We are inside a beacon region.  We will not scan between cycles.

这是预期的行为吗?我可能配置有误吗?

编辑以添加一些代码和信息

我正在尝试的是不绑定到 activity 的连续后台扫描。所以我把开始测距和监控所需的所有代码都放在了从 Application.

扩展的 MyApplication class 中
@Override
public void onCreate() {
   if (Utils.checkPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
        beaconManager = BeaconManager.getInstanceForApplication(this);
        initBeaconParsers(); //In this method I add some custo beacon parsers

        //wake up the app when any beacon is seen
        List<Region> regions = setupBeaconRegions();
        regionBootstrap = new RegionBootstrap(this, regions);

        backgroundPowerSaver = new BackgroundPowerSaver(this);
        beaconManager.setBackgroundScanPeriod(3000l);
        beaconManager.setBackgroundBetweenScanPeriod(15000l);

        beaconManager.setRegionStatePersistenceEnabled(false);
        beaconManager.bind(this);
    }
}

private List<Region> setupBeaconRegions(){
    List<Region> regions = new ArrayList<>();

    region = new Region(Constants.REGION, Identifier.parse(BuildConfig.UUID_1), null, null);
    region2= new Region(Constants.REGION_2, Identifier.parse(BuildConfig.UUID_2), null, null);
    region3= new Region(Constants.REGION_3, Identifier.parse(BuildConfig.UUID_3), null, null);
    region4 = new Region(Constants.REGION_4, Identifier.parse(BuildConfig.UUID_4), null, null);
    region5= new Region(Constants.REGION_5, Identifier.parse(BuildConfig.UUID_5), null, null);

    pushRegion = new Region(Constants.PUSH_REGION, Identifier.parse(BuildConfig.PUSH_UUID_1), null, null);
    pushRegion2= new Region(Constants.PUSH_REGION_2, Identifier.parse(BuildConfig.PUSH_UUID_2), null, null);
    pushRegion3= new Region(Constants.PUSH_REGION_3, Identifier.parse(BuildConfig.PUSH_UUID_3), null, null);
    pushRegion4= new Region(Constants.PUSH_REGION_4, Identifier.parse(BuildConfig.PUSH_UUID_4), null, null);
    pushRegion5= new Region(Constants.PUSH_REGION_5, Identifier.parse(BuildConfig.PUSH_UUID_5), null, null);

    regions.add(region);
    regions.add(region2);
    regions.add(region3);
    regions.add(region4);
    regions.add(regio5);
    regions.add(pushRegion);
    regions.add(pushRegion2);
    regions.add(pushRegion3);
    regions.add(pushRegion4);
    regions.add(pushRegion5);

    return regions;
}

@Override
public void didEnterRegion(Region region) {
    Log.d(TAG, "did enter Bootstrap region." + region);
    startRangingBeacons();
}

private void startRangingBeacons(){
    if (!isBeaconScanInitialized && !beaconManager.isAnyConsumerBound()) {
        try {
            Log.d(TAG, "isAnyConsumerBound? -->" + beaconManager.isAnyConsumerBound());
            beaconManager.startRangingBeaconsInRegion(region);
            beaconManager.startRangingBeaconsInRegion(region2);
            beaconManager.startRangingBeaconsInRegion(region3);
            beaconManager.startRangingBeaconsInRegion(region4);
            beaconManager.startRangingBeaconsInRegion(region5);

            beaconManager.startRangingBeaconsInRegion(pushRegion);
            beaconManager.startRangingBeaconsInRegion(pushRegion2);
            beaconManager.startRangingBeaconsInRegion(pushRegion3);
            beaconManager.startRangingBeaconsInRegion(pushRegion4);
            beaconManager.startRangingBeaconsInRegion(pushRegion5);

            isBeaconScanInitialized = true;
        } catch (RemoteException e) {
            Log.d(TAG, e.getMessage());
        }
    }
}

@Override
public void didExitRegion(Region region) {
    Log.d(TAG, "didExitRegion-->" + region.getUniqueId() + " - " + region.getId1() + " - " + region.getId2() + " - " + region.getId3());
   //Here is some custom code to deal on region exit
}

@Override
public void didDetermineStateForRegion(int state, Region region) {
    Log.d(TAG,"I have just switched from seeing/not seeing beacons: " + state);
}

@Override
public void onBeaconServiceConnect() {
    Log.d(TAG, "onBeaconServiceConnect");
    beaconManager.addRangeNotifier(new RangeNotifier() {

        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
            // Some code to deal ranging
       }
    });
    startRangingBeacons();
}

当我打开信标并首次检测到它时,我接到了一个电话,因此监控似乎工作正常。但是在我唤醒 phone.

之前不会调用测距

很抱歉听到这个问题。目前还不清楚究竟是什么导致扫描以意外的间隔发生,但我怀疑这可能与双重绑定有关。

几点提示:

  • 如果使用区域 bootstrap,请不要手动绑定,因为它在内部这样做。删除:

    beaconManager.bind(this);

  • 同时删除以下方法,将范围通知程序设置移动到 onCreate

    onBeaconServiceConnect

  • 配置更改必须在通过 RegionBootstrap 绑定之前设置。所以将这些行移到该行上方:

    beaconManager.setBackgroundScanPeriod(3000l);
    beaconManager.setBackgroundBetweenScanPeriod(15000l);
    beaconManager.setRegionStatePersistenceEnabled(false);
    
  • 了解上述后台扫描周期不能在 Android 8 后台运行,因为操作系统会将扫描周期限制在 ~15 秒。我一次会在后台扫描 3 秒以上。考虑保留默认值 10 秒。

  • 您应该避免在 Android 8 上使用 isAnyConsumerBound() 方法,因为最新的库版本有一个错误,使其在该平台上不可靠。 (该错误已修复,等待下一个版本。)。无论如何,您不需要此逻辑并且可以在没有语句的情况下立即安全地开始测距。

  • 了解测距只会在后台扫描期结束时给您一个回调。因此,如果您设置得当,您将在后台每 15 分钟收到一次测距回调。

  • 如果您真的想在 Android 8 上在后台持续扫描,请考虑创建一个前台服务。只需拥有一个即可解锁无限期保留您的应用 运行 的能力。