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

时间:2018-03-22 13:19:25

标签: android altbeacon android-8.0-oreo

根据我在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

如果我没有与手机交互并且没有任何信标与受监控区域相匹配,那么它应该在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.

这是预期的行为吗?我可能错误配置了什么吗?

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

我正在尝试的是将连续背景扫描绑定到一个活动。所以我已经放置了从MyApplication延伸的Application类开始测距和监控所需的所有代码。

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

当我打开信标并且首次检测到信号时,监控似乎工作正常。但是在我拿起电话之前不会调用范围。

1 个答案:

答案 0 :(得分:2)

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

一些提示:

  • 如果在内部使用Region bootstrap,请不要手动绑定。删除:

    beaconManager.bind(this);

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

    onBeaconServiceConnect

  • 必须先设置配置更改,然后才能通过RegionBootstrap进行绑定。所以将这些行移到该行之上:

    beaconManager.setBackgroundScanPeriod(3000l);
    beaconManager.setBackgroundBetweenScanPeriod(15000l);
    beaconManager.setRegionStatePersistenceEnabled(false);
    
  • 了解在操作系统将扫描周期夹持到约15秒的背景下,Android 8上无法遵守上述背景扫描周期。我会一次在后台扫描超过3秒。考虑保持10秒的默认值。

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

  • 了解测距只会在后台扫描周期结束时为您提供一次回调。因此,如果你正确设置它,你将在后台每15分钟进行一次测量回调。

  • 如果你真的想要在Android 8的后台持续扫描,请考虑创建一个前台服务。只需拥有一个就能解锁让您的应用程序无限期运行的能力。