BeaconManager updateScanPeriods不会更改扫描速率

时间:2018-09-22 03:08:54

标签: android altbeacon

我正在使用altbeacon库进行BLE扫描,在Android O +上,BLE处于前台模式,所有其他Android O之前的设备都在后台运行。扫描开始没问题,在某些情况下,另一个后台作业会改变扫描周期,执行代码后,我看到设置已应用,但是我看不到扫描速度降低。

这是我用来刷新扫描速率的代码(在BLE激活并扫描之后):

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

这是我启动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

这是日志:

  

09-21 22:00:36.594 5776-5949:BeaconManager在修改扫描之前   周期,扫描周期4000之间的beaconMgr

     

09-21 22:00:36.594 5776-5949:BeaconManager刷新BLE扫描周期   到长

     

09-21 22:00:36.595 5776-5949:BeaconManager BLE扫描周期已更新

     

09-21 22:00:36.689 5776-5949:BeaconManager当前beaconMgr   扫描周期3600000

     

这是长时间显示的BLE仍在积极扫描:

     

09-21 22:03:59.821 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '45 .70308117629712}

     

09-21 22:03:59.827 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '45 .70308117629712}

     

09-21 22:04:00.960 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '47 .328112013435565}

     

09-21 22:04:00.965 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '47 .328112013435565}

     

09-21 22:04:02.082 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '47 .88079569698942}

     

09-21 22:04:02.086 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '47 .88079569698942}

     

09-21 22:04:03.225 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '49 .0029516131644}

     

09-21 22:04:03.232 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '49 .0029516131644}

     

09-21 22:04:05.462 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '49 .57251774840043}

     

09-21 22:04:05.467 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '49 .57251774840043}

     

09-21 22:04:59.948 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '49 .0029516131644}

     

09-21 22:04:59.956 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '49 .0029516131644}

     

09-21 22:05:01.073 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '56 .22605173071153}

     

09-21 22:05:01.083 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '56 .22605173071153}

     

09-21 22:05:15.786 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '49 .0029516131644}

     

09-21 22:05:15.794 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '49 .0029516131644}

     

09-21 22:05:18.031 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '50 .7288634831964}

     

09-21 22:05:18.036 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '50 .7288634831964}

     

09-21 22:05:20.267 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '56 .22605173071153}

     

09-21 22:05:20.272 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '56 .22605173071153}

     

09-21 22:05:21.400 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '50 .7288634831964}

     

09-21 22:05:21.404 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '50 .7288634831964}

     

09-21 22:05:22.528 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '45 .70308117629712}

     

09-21 22:05:22.533 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '45 .70308117629712}

     

09-21 22:05:23.657 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '44 .126620660525674}

     

09-21 22:05:23.661 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '44 .126620660525674}

     

09-21 22:05:24.772 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '43 .465561542707945}

     

09-21 22:05:24.773 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',distance = '43 .465561542707945}

     

09-21 22:05:28.180 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '44 .51624911719877}

     

09-21 22:05:28.189 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '44 .51624911719877}

     

09-21 22:05:34.975 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '43 .985672374000856}

     

09-21 22:05:34.977 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '43 .985672374000856}

     

09-21 22:05:36.107 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '43 .20355217003139}

     

09-21 22:05:36.112 5776-5776:检测到信标   BeaconSignal {uuid ='50765cb7-d9ea-4e21-99a4-fa879613a492',   major ='52259',minor ='29402',距离= '43 .20355217003139}

0 个答案:

没有答案