0

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}

Kishan Viramgama
  • 893
  • 1
  • 11
  • 23
spiderloop
  • 809
  • 1
  • 9
  • 10
  • Is the problem only on Android 8+ with the foreground scanning service that the new intervals do not get apploed? – davidgyoung Sep 22 '18 at 04:34
  • right now i only change the scan rate on Android 8+ device, and from the log above, seems like the setting is loaded, just not taking effect. 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 – spiderloop Sep 22 '18 at 05:03
  • Hi David, I test again today after i reboot the device and that seems to behave better, at least i see that the scanning rate is different, i will monitor it couple more days. Meanwhile, i have a more general question to ask, i notice that sometime i needed to restart Bluetooth in order to get the scanning working, is that an expected behavior? Or is there any status type of function that i can call and programmatically restart Bluetooth? – spiderloop Sep 22 '18 at 23:16
  • @davidgyoung more update, seems like after i added both setForegroundBetweenScanPeriod and setBackgroundBetweenScanPeriod, the device start reducing the scan rate, if i just do setForegroundBetweenScanPeriod, the app will only reduce the scan rate while app is in foreground, when the app go to background, even BLE is started as foreground mode, it still go back to use the fast scanning rate. Any thought? Also a side question, from time to time, i need to restart bluetooth in order to get the scanning start working (it happened on transmission too, but less often), any idea? – spiderloop Sep 23 '18 at 00:02
  • See here on resolving Bluetooth freezes:. https://stackoverflow.com/a/51450865/1461050 – davidgyoung Sep 23 '18 at 04:04
  • it is important to know if your app is in foreground or background mode so you can control the proper scan period. if you really want it to be the same for both foreground and background, set both scan periods to be the same as you tried. – davidgyoung Sep 23 '18 at 04:05
  • @davidgyoung, i use foreground mode on Android 8+, as i mentioned, not sure why i need to set both foreground and background scan period in order for my Pixel 2 to works. By the way, thanks for the BluetoothMedic – spiderloop Sep 23 '18 at 05:53
  • I added the BluetoothMedic, but there is no disable function, how should i turn it off? – spiderloop Sep 23 '18 at 06:01
  • if you enable power cycling with the medic, the API currently has no way to turn it off. If you enable periodic tests, you can call again with NO_TEST to disable them. I cannot explain why you need to set both foreground and background mode unless you use BackgroundPowerSaver or change the background mode manually. – davidgyoung Sep 23 '18 at 14:02
  • @davidgyoung i do enable backgrounPowerSaver regardless foreground or background, should I just limited it to background only? – spiderloop Sep 23 '18 at 23:32
  • BackgroundPowerSaver is what changes your app between foreground and background mode automatically based on whether an activity is in the foreground. If you want this behavior, understand that the foreground vs. background scan periods may be active based on this automatic change. If you want to manage these scan rates yourself (which it sounds like you do) I suggest you simply do not use BackgroundPowerSaver at all, and simply leave it in foreground mode all the time. This way, you only need to configure the foreground scan periods. – davidgyoung Sep 24 '18 at 17:29

0 Answers0