zhuguifei
2026-01-14 82023c98e5c30d36966b85c10c43a6cb11f67e2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.blakequ.bluetooth_manager_lib.scan;
 
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
 
import com.blakequ.bluetooth_manager_lib.BleManager;
import com.blakequ.bluetooth_manager_lib.BleParamsOptions;
import com.blankj.utilcode.util.LogUtils;
 
 
/**
 *
 * Simply creating an instance of this class and holding a reference to it in your Application can
 * improve battery life by 60% by slowing down scans when your app is in the background.
 *
 */
@TargetApi(18)
public class BackgroundPowerSaver implements Application.ActivityLifecycleCallbacks {
    private int activeActivityCount = 0;
    private Context mContext;
    /**
     * The default duration in milliseconds of the Bluetooth scan cycle
     */
    public static final long DEFAULT_FOREGROUND_SCAN_PERIOD = 10000;
    /**
     * The default duration in milliseconds spent not scanning between each Bluetooth scan cycle
     */
    public static final long DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD = 5*1000;
    /**
     * The default duration in milliseconds of the Bluetooth scan cycle when no ranging/monitoring clients are in the foreground
     */
    public static final long DEFAULT_BACKGROUND_SCAN_PERIOD = 10000;
    /**
     * The default duration in milliseconds spent not scanning between each Bluetooth scan cycle when no ranging/monitoring clients are in the foreground
     */
    public static final long DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD = 5 * 60 * 1000;
 
    private long foregroundScanPeriod = DEFAULT_FOREGROUND_SCAN_PERIOD;
    private long foregroundBetweenScanPeriod = DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD;
    private long backgroundScanPeriod = DEFAULT_BACKGROUND_SCAN_PERIOD;
    private long backgroundBetweenScanPeriod = DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD;
 
    /**
     *
     * Constructs a new BackgroundPowerSaver
     *
     * @param context
     * @deprecated the countActiveActivityStrategy flag is no longer used.
     *
     */
    public BackgroundPowerSaver(Context context, boolean countActiveActivityStrategy) {
        this(context);
    }
 
    /**
     *
     * Constructs a new BackgroundPowerSaver using the default background determination strategy
     *
     * @param context
     */
    public BackgroundPowerSaver(Context context) {
        if (android.os.Build.VERSION.SDK_INT < 18) {
            LogUtils.w("BackgroundPowerSaver requires API 18 or higher.");
            return;
        }
        ((Application)context.getApplicationContext()).registerActivityLifecycleCallbacks(this);
        this.mContext = context;
    }
 
    /**
     * Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons.
     * This function is used to setup the period when switching
     * between background/foreground. To have it effect on an already running scan (when the next
     * cycle starts), call {@link BluetoothScanManager#setBackgroundMode}
     *
     * @param p
     */
    @Deprecated
    public void setForegroundScanPeriod(long p) {
        foregroundScanPeriod = p;
    }
 
    /**
     * Sets the duration in milliseconds between each Bluetooth LE scan cycle to look for beacons.
     * This function is used to setup the period when switching
     * between background/foreground. To have it effect on an already running scan (when the next
     * cycle starts), call {@link BluetoothScanManager#setBackgroundMode}
     *
     * @param p
     */
    @Deprecated
    public void setForegroundBetweenScanPeriod(long p) {
        foregroundBetweenScanPeriod = p;
    }
 
    /**
     * Sets the duration in milliseconds of each Bluetooth LE scan cycle to look for beacons.
     * This function is used to setup the period when switching
     * between background/foreground. To have it effect on an already running scan (when the next
     * cycle starts), call {@link BluetoothScanManager#setBackgroundMode}
     *
     * @param p
     */
    @Deprecated
    public void setBackgroundScanPeriod(long p) {
        backgroundScanPeriod = p;
    }
 
    /**
     * Sets the duration in milliseconds spent not scanning between each Bluetooth LE scan cycle when no ranging/monitoring clients are in the foreground
     *
     * @param p
     */
    @Deprecated
    public void setBackgroundBetweenScanPeriod(long p) {
        backgroundBetweenScanPeriod = p;
    }
 
    public long getScanPeriod() {
        BleParamsOptions options = BleManager.getBleParamsOptions();
        if (BluetoothScanManager.getInstance(mContext).isBackgroundMode()) {
            if (options != null){
                return options.getBackgroundScanPeriod();
            }else {
                return backgroundScanPeriod;
            }
        } else {
            if (options != null){
                return options.getForegroundScanPeriod();
            }else {
                return foregroundScanPeriod;
            }
        }
    }
 
    public long getBetweenScanPeriod() {
        BleParamsOptions options = BleManager.getBleParamsOptions();
        if (BluetoothScanManager.getInstance(mContext).isBackgroundMode()) {
            if (options != null){
                return options.getBackgroundBetweenScanPeriod();
            }else {
                return backgroundBetweenScanPeriod;
            }
        } else {
            if (options != null){
                return options.getForegroundBetweenScanPeriod();
            }else {
                return foregroundBetweenScanPeriod;
            }
        }
    }
 
    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
    }
 
    @Override
    public void onActivityStarted(Activity activity) {
    }
 
    @Override
    public void onActivityResumed(Activity activity) {
        activeActivityCount++;
        if (activeActivityCount < 1) {
            LogUtils.d("reset active activity count on resume.  It was " + activeActivityCount);
            activeActivityCount = 1;
        }
        BluetoothScanManager.getInstance(mContext).setBackgroundMode(false);
        LogUtils.d("activity resumed: "+activity+" active activities: "+activeActivityCount);
    }
 
    @Override
    public void onActivityPaused(Activity activity) {
        activeActivityCount--;
        LogUtils.d("activity paused: "+activity+" active activities: "+activeActivityCount);
        if (activeActivityCount < 1) {
            LogUtils.d("setting background mode");
            BluetoothScanManager.getInstance(mContext).setBackgroundMode(true);
        }
    }
 
    @Override
    public void onActivityStopped(Activity activity) {
    }
 
    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
 
    }
 
    @Override
    public void onActivityDestroyed(Activity activity) {
    }
}