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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.blakequ.bluetooth_manager_lib.scan;
 
import android.Manifest;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Handler;
import android.os.SystemClock;
import androidx.core.content.ContextCompat;
 
import com.blakequ.bluetooth_manager_lib.scan.bluetoothcompat.BluetoothLeScannerCompat;
import com.blakequ.bluetooth_manager_lib.scan.bluetoothcompat.ScanCallbackCompat;
import com.blakequ.bluetooth_manager_lib.scan.bluetoothcompat.ScanFilterCompat;
import com.blakequ.bluetooth_manager_lib.scan.bluetoothcompat.ScanSettingsCompat;
import com.blakequ.bluetooth_manager_lib.util.BluetoothUtils;
import com.blankj.utilcode.util.LogUtils;
 
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
 
/**
 * Copyright (C) BlakeQu All Rights Reserved <blakequ@gmail.com>
 * <p/>
 * Licensed under the blakequ.com License, Version 1.0 (the "License");
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <p/>
 * author  : quhao <blakequ@gmail.com> <br>
 * date     : 2016/8/18 11:54 <br>
 * last modify author : <br>
 * version : 1.0 <br>
 * description:
 */
@TargetApi(18)
public class CycledLeScanner {
    private boolean isPrintCycleTime = true;
    private final Context mContext;
    private long scanPeriod;
    private long betweenScanPeriod;
    private boolean mBackgroundFlag = false;
    private ScanCallbackCompat scanCallbackCompat;
    private final BluetoothUtils mBluetoothUtils;
    private ScanOverListener scanOverListener;
    private long nextScanStartTime = 0;
    private long scanStopTime = 0;
    private long lastScanEndTime = 0;
    private boolean mScanning = false;
    private boolean isPauseScan = false; //pause scan or restart
    private boolean isOnceScan = false; //scan only once
    private boolean isStartNow = false;
    private final Handler mHandler = new Handler();
    private boolean isSetScanSetting = false;
    private ScanSettingsCompat scanSettings;
    private final List<ScanFilterCompat> scanFilterCompats = new CopyOnWriteArrayList<>();
 
    public CycledLeScanner(Context context, long scanPeriod, long betweenScanPeriod, boolean backgroundFlag, ScanCallbackCompat callbackCompat){
        this.mContext = context;
        this.scanPeriod = scanPeriod;
        this.betweenScanPeriod = betweenScanPeriod;
        this.scanCallbackCompat = callbackCompat;
        this.mBackgroundFlag = backgroundFlag;
        this.mBluetoothUtils = BluetoothUtils.getInstance(context);
    }
 
    /**
     * invoke at the end of scan(every scan cycle over)
     * @param scanOverListener
     */
    public void setScanOverListener(ScanOverListener scanOverListener) {
        this.scanOverListener = scanOverListener;
    }
 
    /**
     * add scan filter
     * @param scanFilter
     */
    public void addScanFilterCompats(ScanFilterCompat scanFilter){
        scanFilterCompats.add(scanFilter);
    }
 
    public void setScanSettings(ScanSettingsCompat scanSettings) {
        isSetScanSetting = true;
        this.scanSettings = scanSettings;
    }
 
    public void startOnceScan() {
        isOnceScan = true;
        scanLeDevice(true);
    }
 
    /**
     * pause or restart scan device cycle
     * @param isPauseScan
     */
    public void setPauseScan(boolean isPauseScan) {
        this.isPauseScan = isPauseScan;
        if (!isPauseScan){
            scanLeDevice(true);
        }else {
            scanLeDevice(false);
        }
    }
 
    /**
     * start scan device
     */
    public void startScan(){
        isPauseScan = false;
        scanLeDevice(true);
    }
 
    /**
     * start scan device right now
     */
    public void startScanNow(){
        isStartNow = true;
        isPauseScan = false;
        scanLeDevice(true);
    }
 
    /**
     * Tells the cycler the scan rate and whether it is in operating in background mode.
     * Background mode flag  is used only with the Android 5.0 scanning implementations to switch
     * between LOW_POWER_MODE vs. LOW_LATENCY_MODE
     * @param backgroundFlag is running background
     */
    public void setBackgroundMode(long scanPeriod, long betweenScanPeriod, boolean backgroundFlag) {
        if (android.os.Build.VERSION.SDK_INT < 18) {
            LogUtils.w("Not supported prior to API 18.  Method invocation will be ignored");
            return;
        }
 
        this.scanPeriod = scanPeriod;
        this.betweenScanPeriod = betweenScanPeriod;
        if (backgroundFlag != mBackgroundFlag) {
            LogUtils.d("restart polling task scanPeriod:" + scanPeriod + " betweenScanPeriod:" + betweenScanPeriod + " backgroundFlag:" + backgroundFlag + " mode:" + mBackgroundFlag);
            mBackgroundFlag = backgroundFlag;
            long now = SystemClock.elapsedRealtime();
 
            //update next scan start time(在等待开始扫描时修正下一次开始时间,提前开始)
            if (nextScanStartTime > now){
                long proposedNextScanStartTime = lastScanEndTime + betweenScanPeriod;
                if (proposedNextScanStartTime < nextScanStartTime){
                    LogUtils.d("Waiting...Adjusted nextScanStartTime to be" + (proposedNextScanStartTime - now) + " old:" + (nextScanStartTime - now));
                    nextScanStartTime = proposedNextScanStartTime;
                }
            }
 
            //update current scan stop time(如果在扫描中则修正本次的结束时间,提前结束)
            if (scanStopTime > now){
                long proposedStopTime = nextScanStartTime + scanPeriod;
                if (proposedStopTime < scanStopTime){
                    LogUtils.d("Scanning...Adjusted scanStopTime to be " + (proposedStopTime - now) + " old:" + (scanStopTime - now));
                    scanStopTime = proposedStopTime;
                }
            }
 
            //set scan setting params
            if (!isSetScanSetting || scanSettings == null){
                if (mBackgroundFlag) {
                    LogUtils.d("starting filtered scan in SCAN_MODE_LOW_POWER");
                    scanSettings = (new ScanSettingsCompat.Builder().setScanMode(ScanSettingsCompat.SCAN_MODE_LOW_POWER)).build();
                } else {
                    LogUtils.d("starting non-filtered scan in SCAN_MODE_LOW_LATENCY");
                    scanSettings = (new ScanSettingsCompat.Builder().setScanMode(ScanSettingsCompat.SCAN_MODE_LOW_LATENCY)).build();
                }
            }
        }
    }
 
    public boolean isScanning() {
        return mScanning;
    }
 
    public boolean isPauseScan() {
        return isPauseScan;
    }
 
    /**
     * start or stop scan
     * @param enable true-start scan right now,false-stop scan
     */
    private void scanLeDevice(boolean enable) {
        BluetoothAdapter mAdapter = mBluetoothUtils.getBluetoothAdapter();
        if (mBluetoothUtils == null || !mBluetoothUtils.isBluetoothIsEnable()){
            LogUtils.e("ScanDevice: Scanning fail! BluetoothAdapter is null");
            return;
        }
 
 
        if (enable) {
            //is delay scan
            if (deferScanIfNeeded()){
                if (!isStartNow){
                    return;
                }else{
                    LogUtils.i("ScanDevice: Scan right now!");
                    isStartNow = false;
                }
            }
 
            if (mScanning) {
                LogUtils.d("ScanDevice: Scanning is running now !");
                return;
            }
            LogUtils.d("ScanDevice: Starting Scanning scanPeriod:"+scanPeriod+", between:"+betweenScanPeriod);
            mScanning = true;
            if (!isPauseScan || isOnceScan){
                try {
                    if (android.os.Build.VERSION.SDK_INT < 23 || checkLocationPermission()) {
                        if (android.os.Build.VERSION.SDK_INT >= 23 && !isGpsProviderEnabled(mContext)){
                            LogUtils.e("If SDK>=23, current SDK=" + android.os.Build.VERSION.SDK_INT+", Location info not open and can not scan any device!");
                            scanCallbackCompat.onScanFailed(ScanCallbackCompat.SCAN_FAILED_LOCATION_CLOSE);
                        }else {
                            LogUtils.i("ScanDevice: Start scan...");
                            BluetoothLeScannerCompat.startScan(mAdapter, scanFilterCompats, getScanSettings(), scanCallbackCompat);
                        }
                    }else{
                        scanCallbackCompat.onScanFailed(ScanCallbackCompat.SCAN_FAILED_LOCATION_PERMISSION_FORBID);
                        LogUtils.e("If SDK>=23, current SDK="+android.os.Build.VERSION.SDK_INT+", Please check the location permission is enabled(ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION)");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    LogUtils.e("Internal Android exception scanning for beacons "+e.toString());
                }
 
                if (isOnceScan){
                    LogUtils.d("ScanDevice: Scanning once");
                    isOnceScan = false;
                }
            }else{
                LogUtils.d("ScanDevice: Pause Scanning");
            }
            scanStopTime = SystemClock.elapsedRealtime() + scanPeriod;
            nextScanStartTime = scanStopTime + betweenScanPeriod;
            scheduleScanStop();
        } else {
            LogUtils.d("ScanDevice: Stopping Scan");
            stopScan();
        }
    }
 
    private void scheduleScanStop(){
        // Stops scanning after a pre-defined scan period.
        long millisecondsUntilStop = scanStopTime - SystemClock.elapsedRealtime();
        if (millisecondsUntilStop > 0) {
            if (isPrintCycleTime){
                LogUtils.d("Waiting to stop scan cycle for another " + millisecondsUntilStop + " milliseconds");
            }
 
            if (!isPauseScan) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        scheduleScanStop();
                    }
                }, millisecondsUntilStop > 1000 ? 1000 : millisecondsUntilStop);
            }
        } else {
            LogUtils.d("Stop cycle scan");
            stopScan();
        }
    }
 
    private void stopScan(){
        if (mScanning) {
            BluetoothAdapter mAdapter = mBluetoothUtils.getBluetoothAdapter();
            if (mAdapter != null && mBluetoothUtils.isBluetoothIsEnable()) {
                try {
                    BluetoothLeScannerCompat.stopScan(mAdapter, scanCallbackCompat);
                    lastScanEndTime = SystemClock.elapsedRealtime();
                    LogUtils.d("stopping bluetooth le scan "+lastScanEndTime);
                } catch (Exception e) {
                    LogUtils.w("Internal Android exception scanning for beacons "+e.toString());
                }
            } else {
                LogUtils.d("Bluetooth is disabled.  Cannot scan for beacons.");
            }
            nextScanStartTime = SystemClock.elapsedRealtime() + betweenScanPeriod;
            //start next scan cycle
            if (!isPauseScan){
                scanLeDevice(true);
            }
        }
        mScanning = false;
        //// FIXME: 2017/6/22 将其调整到mScanning后面
        if (scanOverListener != null){
            scanOverListener.onScanOver();
        }
    }
 
    /**
     * check is defter scan
     * @return
     */
    private boolean deferScanIfNeeded(){
        long millisecondsUntilStart = nextScanStartTime - SystemClock.elapsedRealtime();
        if (millisecondsUntilStart > 0) {
            if (isPrintCycleTime){
                LogUtils.d("Waiting to start next Bluetooth scan for another "+millisecondsUntilStart+" milliseconds");
            }
            // Don't actually wait until the next scan time -- only wait up to 1 second.  This
            // allows us to start scanning sooner if a consumer enters the foreground and expects
            // results more quickly.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (!isPauseScan) {
                        scanLeDevice(true);
                    }
                }
            }, millisecondsUntilStart > 1000 ? 1000 : millisecondsUntilStart);
            return true;
        }
        LogUtils.d("Start cycle scan");
        return false;
    }
 
 
    /**
     * get scan settings
     * @return
     */
    private ScanSettingsCompat getScanSettings() {
        if (scanSettings == null){
            if (mBackgroundFlag) {
                LogUtils.d("starting filtered scan in SCAN_MODE_LOW_POWER");
                scanSettings = (new ScanSettingsCompat.Builder().setScanMode(ScanSettingsCompat.SCAN_MODE_LOW_POWER)).build();
            } else {
                LogUtils.d("starting non-filtered scan in SCAN_MODE_LOW_LATENCY");
                scanSettings = (new ScanSettingsCompat.Builder().setScanMode(ScanSettingsCompat.SCAN_MODE_LOW_LATENCY)).build();
            }
        }
        return scanSettings;
    }
 
    /**
     * is open GPS
     * @param context
     * @return
     */
    public static boolean isGpsProviderEnabled(Context context){
        LocationManager service = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
        return service.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
 
    /**
     * when API>=23, if the location disabled, can not scan any devices
     * @return
     */
    private boolean checkLocationPermission() {
        return checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION) || checkPermission(Manifest.permission.ACCESS_FINE_LOCATION);
    }
 
    private boolean checkPermission(final String permission) {
        return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_GRANTED;
    }
}