ScanJob continues beign invoked after the 15 min p

2019-09-20 10:01发布

问题:

From what I understand in the documentation of AltBeacon library, the ScanJob should stop beign called after 15min of last interaction with the application.

I have started to monitor and range some regions and I have disabled all the beacons, so no beacons are beign detected. After starting the application, I have killed it and I am seeign in the logcat that the ScanJob is beign invoked continuosly even after 15 minutes.

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

Shouldn't it have stopped after the 15 minutes if I haven't interacted with the phone and there are not any beacon matching the monitored regions?

Also, when I connect the beacon, it is detected, but then the ScanJob stops beign called.

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.

Is this the expected behaviour? May I have misconfigured something?

Edit to add some code and info

What I am trying is to have a continuos background scan not bound to an activity. So I have put all the code needed to start ranging and monitoring in MyApplication class that extends from 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();
}

Monitoring seems to be working fine as I get a call when I switch on the beacons and it's first detected. But ranging is not invoked until I wake up the phone.

回答1:

Sorry to hear of the trouble. It is unclear precisely what is causing the scans to happen at the unexpected intervals, but I suspect it may have to do with double binding.

A few tips:

  • Do not bind manually if using Region bootstrap as it does so internally. Remove:

    beaconManager.bind(this);

  • Also remove the following method, moving your range notifier setup to onCreate

    onBeaconServiceConnect

  • Configuration changes must be set up before binding through RegionBootstrap. So move these lines above that line:

    beaconManager.setBackgroundScanPeriod(3000l);
    beaconManager.setBackgroundBetweenScanPeriod(15000l);
    beaconManager.setRegionStatePersistenceEnabled(false);
    
  • Understand that the above background scan periods cannot be respected on Android 8 in the background where between scan periods will be clamped to ~15 secs by the operating system. I would scan in the background for more than 3 secs at a time. Consider keeping the default of 10 secs.

  • You should avoid using the isAnyConsumerBound() method on Android 8 as the latest library version has a bug making it unreliable on that plaform. (The bug has been fixed awaiting the next release.). Regardless, you should not need this logic and can safely start ranging immediately without that of statement.

  • Understand that ranging will only give you a single callback at the end of the background scan period. So if you have it set up properly, you will get one ranging callback every 15 minutes in the background.

  • If you really want constant scanning in the background on Android 8, consider creating a foreground service. Simply having one will unlock the ability to keep your app running indefinitely.