From 12d1efec4a0c1770572acbd7827dbd152512b736 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Tue, 8 Aug 2017 13:15:12 -0400 Subject: [PATCH 1/3] Fix scanning being left on with nonzero foreground betweenScanPeriod --- CHANGELOG.md | 12 ++++++++++++ .../scanner/CycledLeScannerForLollipop.java | 18 +++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9501058ac..60b296dc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +### Development + +Enhancements: + +Bug Fixes: + + - Fix "Scanning too frequently" error with non-zero betweenScanPeriod + and scanPeriod+betweenScanPeriod < 6000, and full-power scanning + staying on for foreground scans with a non-zero betweenScanPeriod + (#555, David G. Young) + + ### 2.12 / 2017-08-07 Enhancements: diff --git a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java index 26d0b0744..eca009475 100644 --- a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java +++ b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java @@ -159,11 +159,19 @@ protected void startScan() { List filters = new ArrayList(); ScanSettings settings; - if (mBackgroundFlag && !mMainScanCycleActive) { - LogManager.d(TAG, "starting filtered scan in SCAN_MODE_LOW_POWER"); - settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build(); - filters = new ScanFilterUtils().createScanFiltersForBeaconParsers( - mBeaconManager.getBeaconParsers()); + if (!mMainScanCycleActive) { + // Only scan between cycles if the between can cycle time > 6 seconds. A shorter low + // power scan is unlikely to be useful, and might trigger a "scanning too frequently" + // error on Android N. + if (mBetweenScanPeriod > 6000l) { + LogManager.d(TAG, "starting filtered scan in SCAN_MODE_LOW_POWER"); + settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build(); + filters = new ScanFilterUtils().createScanFiltersForBeaconParsers( + mBeaconManager.getBeaconParsers()); + } + else { + LogManager.d(TAG, "not scanning between cycles because the between scan cycle is too short."); + } } else { LogManager.d(TAG, "starting non-filtered scan in SCAN_MODE_LOW_LATENCY"); settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)).build(); From d84f727f6fe82534b36714a803c59468f4a8d499 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Tue, 8 Aug 2017 13:54:01 -0400 Subject: [PATCH 2/3] don't start scan if settings have not been set --- .../service/scanner/CycledLeScannerForLollipop.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java index eca009475..2016cefa4 100644 --- a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java +++ b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java @@ -157,7 +157,7 @@ protected void startScan() { return; } List filters = new ArrayList(); - ScanSettings settings; + ScanSettings settings = null; if (!mMainScanCycleActive) { // Only scan between cycles if the between can cycle time > 6 seconds. A shorter low @@ -170,14 +170,16 @@ protected void startScan() { mBeaconManager.getBeaconParsers()); } else { - LogManager.d(TAG, "not scanning between cycles because the between scan cycle is too short."); + LogManager.d(TAG, "aborting scan between cycles because the between scan cycle is too short."); } } else { LogManager.d(TAG, "starting non-filtered scan in SCAN_MODE_LOW_LATENCY"); settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)).build(); } - postStartLeScan(filters, settings); + if (settings != null) { + postStartLeScan(filters, settings); + } } @Override From 4bf2d9f9c69270ccb00d43ede4e5ab860b85be81 Mon Sep 17 00:00:00 2001 From: "David G. Young" Date: Thu, 10 Aug 2017 20:01:21 -0400 Subject: [PATCH 3/3] Ensure low power background scans are not started for short between cycles by moving code higher --- .../scanner/CycledLeScannerForLollipop.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java index 2016cefa4..d35031b20 100644 --- a/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java +++ b/src/main/java/org/altbeacon/beacon/service/scanner/CycledLeScannerForLollipop.java @@ -91,11 +91,20 @@ protected boolean deferScanIfNeeded() { if (secsSinceLastDetection > BACKGROUND_L_SCAN_DETECTION_PERIOD_MILLIS) { mBackgroundLScanStartTime = SystemClock.elapsedRealtime(); mBackgroundLScanFirstDetectionTime = 0l; - LogManager.d(TAG, "This is Android L. Doing a filtered scan for the background."); - + LogManager.d(TAG, "This is Android L. Preparing to do a filtered scan for the background."); // On Android L, between scan cycles do a scan with a filter looking for any beacon // if we see one of those beacons, we need to deliver the results - startScan(); + // Only scan between cycles if the between can cycle time > 6 seconds. A shorter low + // power scan is unlikely to be useful, and might trigger a "scanning too frequently" + // error on Android N. + if (mBetweenScanPeriod > 6000l) { + startScan(); + } + else { + LogManager.d(TAG, "Suppressing scan between cycles because the between scan cycle is too short."); + } + + } else { // TODO: Consider starting a scan with delivery based on the filters *NOT* being seen // This API is now available in Android M @@ -160,18 +169,10 @@ protected void startScan() { ScanSettings settings = null; if (!mMainScanCycleActive) { - // Only scan between cycles if the between can cycle time > 6 seconds. A shorter low - // power scan is unlikely to be useful, and might trigger a "scanning too frequently" - // error on Android N. - if (mBetweenScanPeriod > 6000l) { - LogManager.d(TAG, "starting filtered scan in SCAN_MODE_LOW_POWER"); - settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build(); - filters = new ScanFilterUtils().createScanFiltersForBeaconParsers( - mBeaconManager.getBeaconParsers()); - } - else { - LogManager.d(TAG, "aborting scan between cycles because the between scan cycle is too short."); - } + LogManager.d(TAG, "starting filtered scan in SCAN_MODE_LOW_POWER"); + settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)).build(); + filters = new ScanFilterUtils().createScanFiltersForBeaconParsers( + mBeaconManager.getBeaconParsers()); } else { LogManager.d(TAG, "starting non-filtered scan in SCAN_MODE_LOW_LATENCY"); settings = (new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)).build();