I am using the altbeacon library for the BLE scan, on Android O+, BLE is in foreground mode, all other pre-Android O devices run on background. Scan started no problem, and another background job will change the scan period in certain situation, the code executed and i see the setting applied, but i dont see the scan rate get reduced.
Here is the code i use to refresh the scan rate (after BLE is activated and scanning):
public static int SHORT_SCAN_PERIOD = 2 * 1000;
public static int SHORT_BETTWEEN_SCAN_PERIOD = 4 * 1000;
public static int LONG_BETWEEN_SCAN_PERIOD = 3600 * 1000;
private void refreshBLEScanPeriod() {
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Log.d(logTag, "Refreshing BLE scan period");
//beaconManager.setForegroundScanPeriod(Config.SHORT_SCAN_PERIOD);
beaconManager.setForegroundBetweenScanPeriod(Config.SHORT_BETTWEEN_SCAN_PERIOD);
} else {
//beaconManager.setBackgroundScanPeriod(Config.SHORT_SCAN_PERIOD);
beaconManager.setBackgroundBetweenScanPeriod(Config.SHORT_BETTWEEN_SCAN_PERIOD);
}
try {
beaconManager.applySettings();
beaconManager.updateScanPeriods();
Log.d(logTag, "BLE scan period updated to SHORT");
AppDefaultNotification appDefaultNotification = new AppDefaultNotification(getApplicationContext());
appDefaultNotification.buildNotification();
} catch (RemoteException e) {
Log.e(logTag, "Unable to reset scan period " + e.getMessage());
}
}
And here is the code how i started the BLE:
beaconScanner = BeaconScanner.getInstance(getApplicationContext());
beaconManager = BeaconManager.getInstanceForApplication(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
appDefaultNotification = new AppDefaultNotification(this);
Log.d(logTag, "starting BLE foreground service");
beaconManager.setForegroundScanPeriod(Config.SHORT_SCAN_PERIOD);
beaconManager.setForegroundBetweenScanPeriod(Config.SHORT_BETTWEEN_SCAN_PERIOD);
/**
* For the above foreground scanning service to be useful, you need to disable
* JobScheduler-based scans (used on Android 8+) and set a fast background scan
* cycle that would otherwise be disallowed by the operating system.
*/
beaconManager.setEnableScheduledScanJobs(false);
beaconManager.setBackgroundBetweenScanPeriod(0);
beaconManager.setBackgroundScanPeriod(1100);
beaconManager.enableForegroundServiceScanning(appDefaultNotification.buildNotification(), Constants.NOTIFICATION_ID.FOREGROUND_SERVICE);
} else {
beaconManager.setEnableScheduledScanJobs(false);
/**
* Every 60s scan for 20s
*/
// set the duration of the scan to be 1.1 seconds
beaconManager.setBackgroundScanPeriod(Config.SHORT_SCAN_PERIOD);
// set the time between each scan to be 30 seconds
beaconManager.setBackgroundBetweenScanPeriod(Config.SHORT_BETTWEEN_SCAN_PERIOD);
setBackgroundBetweenScanPeriod(sharedPreferences.getInt(Config.KEY_BLE_BETWEEN_SCAN_PERIOD, Config.SHORT_BACKGROUND_BETWEEN_SCAN_PERIOD));
}
/**
* simply constructing this class and holding a reference to it in your custom Application
* class will automatically cause the BeaconLibrary to save battery whenever the application
* is not visible. This reduces bluetooth power usage by about 60%
*/
backgroundPowerSaver = new BackgroundPowerSaver(this);
beaconManager.getBeaconParsers().clear();
beaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); // iBeacon Layout
here is the log:
09-21 22:00:36.594 5776-5949: BeaconManager Before udpating scan period, beaconMgr between scan period 4000
09-21 22:00:36.594 5776-5949: BeaconManager Refreshing BLE scan period to LONG
09-21 22:00:36.595 5776-5949: BeaconManager BLE scan period updated
09-21 22:00:36.689 5776-5949: BeaconManager Current beaconMgr between scan period 3600000
And here is the long showing BLE is still actively scanning:
09-21 22:03:59.821 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='45.70308117629712}
09-21 22:03:59.827 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='45.70308117629712}
09-21 22:04:00.960 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='47.328112013435565}
09-21 22:04:00.965 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='47.328112013435565}
09-21 22:04:02.082 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='47.88079569698942}
09-21 22:04:02.086 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='47.88079569698942}
09-21 22:04:03.225 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.0029516131644}
09-21 22:04:03.232 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.0029516131644}
09-21 22:04:05.462 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.57251774840043}
09-21 22:04:05.467 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.57251774840043}
09-21 22:04:59.948 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.0029516131644}
09-21 22:04:59.956 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.0029516131644}
09-21 22:05:01.073 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='56.22605173071153}
09-21 22:05:01.083 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='56.22605173071153}
09-21 22:05:15.786 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.0029516131644}
09-21 22:05:15.794 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='49.0029516131644}
09-21 22:05:18.031 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='50.7288634831964}
09-21 22:05:18.036 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='50.7288634831964}
09-21 22:05:20.267 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='56.22605173071153}
09-21 22:05:20.272 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='56.22605173071153}
09-21 22:05:21.400 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='50.7288634831964}
09-21 22:05:21.404 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='50.7288634831964}
09-21 22:05:22.528 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='45.70308117629712}
09-21 22:05:22.533 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='45.70308117629712}
09-21 22:05:23.657 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='44.126620660525674}
09-21 22:05:23.661 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='44.126620660525674}
09-21 22:05:24.772 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='43.465561542707945}
09-21 22:05:24.773 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='43.465561542707945}
09-21 22:05:28.180 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='44.51624911719877}
09-21 22:05:28.189 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='44.51624911719877}
09-21 22:05:34.975 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='43.985672374000856}
09-21 22:05:34.977 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='43.985672374000856}
09-21 22:05:36.107 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='43.20355217003139}
09-21 22:05:36.112 5776-5776: Detected beacon BeaconSignal{uuid='50765cb7-d9ea-4e21-99a4-fa879613a492', major='52259', minor='29402', distance='43.20355217003139}