zhuguifei
2026-01-14 b7ee99a71e88a08a09fe9daada6675a175d09be1
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
package com.blakequ.bluetooth_manager_lib;
 
 
import androidx.annotation.IntRange;
 
import com.blakequ.bluetooth_manager_lib.connect.ConnectConfig;
 
import static com.blakequ.bluetooth_manager_lib.scan.BackgroundPowerSaver.DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD;
import static com.blakequ.bluetooth_manager_lib.scan.BackgroundPowerSaver.DEFAULT_BACKGROUND_SCAN_PERIOD;
import static com.blakequ.bluetooth_manager_lib.scan.BackgroundPowerSaver.DEFAULT_FOREGROUND_BETWEEN_SCAN_PERIOD;
import static com.blakequ.bluetooth_manager_lib.scan.BackgroundPowerSaver.DEFAULT_FOREGROUND_SCAN_PERIOD;
 
/**
 * 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/12/1 14:28 <br>
 * last modify author : <br>
 * version : 1.0 <br>
 * description: config of ble connect and scan
 */
 
public class BleParamsOptions {
 
    private final boolean isDebugMode;
 
    //using for scan
    private final long foregroundScanPeriod;
    private final long foregroundBetweenScanPeriod;
    private final long backgroundScanPeriod;
    private final long backgroundBetweenScanPeriod;
 
    //using for connect
    private final int maxConnectDeviceNum; //一次最大连接设备个数
    private final int reconnectStrategy; //重连策略
    private final int reconnectMaxTimes; //最大重连次数
    private final long reconnectBaseSpaceTime; //重连基础时间间隔ms
    private final int reconnectedLineToExponentTimes; //快速重连的次数(线性到指数)
    private final int connectTimeOutTimes; //连接超时时间
 
    public boolean isDebugMode() {
        return isDebugMode;
    }
 
    public long getForegroundScanPeriod() {
        return foregroundScanPeriod;
    }
 
    public long getForegroundBetweenScanPeriod() {
        return foregroundBetweenScanPeriod;
    }
 
    public long getBackgroundScanPeriod() {
        return backgroundScanPeriod;
    }
 
    public long getBackgroundBetweenScanPeriod() {
        return backgroundBetweenScanPeriod;
    }
 
    public int getMaxConnectDeviceNum() {
        return maxConnectDeviceNum;
    }
 
    public int getReconnectStrategy() {
        return reconnectStrategy;
    }
 
    public int getReconnectMaxTimes() {
        return reconnectMaxTimes;
    }
 
    public long getReconnectBaseSpaceTime() {
        return reconnectBaseSpaceTime;
    }
 
    public int getReconnectedLineToExponentTimes() {
        return reconnectedLineToExponentTimes;
    }
 
    public int getConnectTimeOutTimes() {
        return connectTimeOutTimes;
    }
 
    private BleParamsOptions(Builder builder){
        this.isDebugMode = builder.isDebugMode;
        this.foregroundScanPeriod = builder.foregroundScanPeriod;
        this.foregroundBetweenScanPeriod = builder.foregroundBetweenScanPeriod;
        this.backgroundScanPeriod = builder.backgroundScanPeriod;
        this.backgroundBetweenScanPeriod = builder.backgroundBetweenScanPeriod;
        this.maxConnectDeviceNum = builder.maxConnectDeviceNum;
        this.reconnectStrategy = builder.reconnectStrategy;
        this.reconnectMaxTimes = builder.reconnectMaxTimes;
        this.reconnectBaseSpaceTime = builder.reconnectBaseSpaceTime;
        this.reconnectedLineToExponentTimes = builder.reconnectedLineToExponentTimes;
        this.connectTimeOutTimes = builder.connectTimeOutTimes;
    }
 
    public static class Builder {
 
        private boolean isDebugMode = true;
        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;
        private int maxConnectDeviceNum = 5;//一次最大连接设备个数
        private int reconnectStrategy = ConnectConfig.RECONNECT_LINE_EXPONENT; //重连策略
        private int reconnectMaxTimes = Integer.MAX_VALUE; //最大重连次数
        private long reconnectBaseSpaceTime = 8000; //重连基础时间间隔ms
        private int reconnectedLineToExponentTimes = 5; //快速重连的次数(线性到指数)
        private int connectTimeOutTimes = 15000; //连接超时时间15s
 
        /**
         * setting is debug mode, if false then the log will disable
         * @param isDebugMode you can set by BuildConfig.DEBUG, default is true
         * @return
         */
        public Builder setDebugMode(boolean isDebugMode){
            this.isDebugMode = isDebugMode;
            return this;
        }
 
        /**
         * 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)
         *
         * @param foregroundScanPeriod defalut is 10 seconds, you should using milliseconds
         * @return
         */
        public Builder setForegroundScanPeriod(@IntRange(from = 0) long foregroundScanPeriod) {
            if (foregroundScanPeriod < 0){
                throw new IllegalArgumentException("Period time must > 0, now is "+foregroundScanPeriod);
            }
            this.foregroundScanPeriod = foregroundScanPeriod;
            return this;
        }
 
        /**
         * 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)
         * @param foregroundBetweenScanPeriod defalut is 5 seconds, you should using milliseconds
         * @return
         */
        public Builder setForegroundBetweenScanPeriod(@IntRange(from = 0) long foregroundBetweenScanPeriod) {
            if (foregroundBetweenScanPeriod < 0){
                throw new IllegalArgumentException("Period time must > 0, now is "+foregroundBetweenScanPeriod);
            }
            this.foregroundBetweenScanPeriod = foregroundBetweenScanPeriod;
            return this;
        }
 
        /**
         * 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)
         * @param backgroundScanPeriod default is 10 seconds, you should using milliseconds
         * @return
         */
        public Builder setBackgroundScanPeriod(@IntRange(from = 0) long backgroundScanPeriod) {
            if (backgroundScanPeriod < 0){
                throw new IllegalArgumentException("Period time must > 0, now is "+backgroundScanPeriod);
            }
            this.backgroundScanPeriod = backgroundScanPeriod;
            return this;
        }
 
        /**
         * Sets the duration in milliseconds spent not scanning between each Bluetooth LE scan cycle when no ranging/monitoring clients are in the foreground
         * @param backgroundBetweenScanPeriod default is 5 minutes, you should using milliseconds
         * @return
         */
        public Builder setBackgroundBetweenScanPeriod(@IntRange(from = 0) long backgroundBetweenScanPeriod) {
            if (backgroundBetweenScanPeriod < 0){
                throw new IllegalArgumentException("Period time must > 0, now is "+backgroundBetweenScanPeriod);
            }
            this.backgroundBetweenScanPeriod = backgroundBetweenScanPeriod;
            return this;
        }
 
        /**
         * max number of connect ble device
         * @param maxConnectDeviceNum default is 5
         * @return
         */
        public Builder setMaxConnectDeviceNum(@IntRange(from = 1) int maxConnectDeviceNum) {
            if (maxConnectDeviceNum < 1){
                throw new IllegalArgumentException("Connect device number must > 1, now is "+maxConnectDeviceNum);
            }
            this.maxConnectDeviceNum = maxConnectDeviceNum;
            return this;
        }
 
        /**
         * how to reconnect, you have choose reconnectStrategy:
         * <ol>
         * <li>1. ConnectConfig.RECONNECT_LINEAR, each reconnection interval time is the same</li>
         * <li>2. ConnectConfig.RECONNECT_EXPONENT, the reconnect time interval is exponential growth</li>
         * <li>3. ConnectConfig.RECONNECT_LINE_EXPONENT, Start time interval is the same, after reconnectedLineToExponentTimes times then use exponential growth<li/>
         * </ol>
         * @param reconnectStrategy
         * @return
         */
        public Builder setReconnectStrategy(@IntRange(from = 1, to = 4) int reconnectStrategy) {
            if (reconnectStrategy < 1 || reconnectStrategy > 4){
                throw new IllegalArgumentException("reconnectStrategy range is 1 to 4");
            }
            this.reconnectStrategy = reconnectStrategy;
            return this;
        }
 
        /**
         * usable only the reconnect strategy is ConnectConfig.RECONNECT_LINE_EXPONENT
         * @param reconnectedLineToExponentTimes the times from linear to exponential, default is 5 times
         * @return
         */
        public Builder setReconnectedLineToExponentTimes(@IntRange(from = 1) int reconnectedLineToExponentTimes) {
            if (reconnectedLineToExponentTimes < 1){
                throw new IllegalArgumentException("reconnectedLineToExponentTimes value must >= 1, now is "+reconnectedLineToExponentTimes);
            }
            this.reconnectedLineToExponentTimes = reconnectedLineToExponentTimes;
            return this;
        }
 
        /**
         * max reconnect times, if you set 0 will not reconnect
         * @param reconnectMaxTimes default is Integer.MAX_VALUE
         * @return
         */
        public Builder setReconnectMaxTimes(@IntRange(from = 0) int reconnectMaxTimes) {
            if (reconnectMaxTimes < 0){
                throw new IllegalArgumentException("Reconnect max times must > 0, now is "+reconnectMaxTimes);
            }
            this.reconnectMaxTimes = reconnectMaxTimes;
            return this;
        }
 
        /**
         * reconnect interval time(>=1000ms)
         * @param reconnectBaseSpaceTime default is 8 seconds
         * @return
         */
        public Builder setReconnectBaseSpaceTime(@IntRange(from = 1000) long reconnectBaseSpaceTime) {
            if (reconnectBaseSpaceTime < 1000){
                throw new IllegalArgumentException("reconnectBaseSpaceTime must >= 1000ms, now is "+reconnectBaseSpaceTime);
            }
            this.reconnectBaseSpaceTime = reconnectBaseSpaceTime;
            return this;
        }
 
        /**
         * time out of connect device(after this time will check bluetooth state, if bluetooth not available will close all connect)
         * @param connectTimeOutTimes default is 15 seconds(must > 1 seconds)
         * @return
         */
        public Builder setConnectTimeOutTimes(@IntRange(from = 1000) int connectTimeOutTimes){
            if (connectTimeOutTimes < 1000){
                throw new IllegalArgumentException("connectTimeOutTimes must >= 1000ms, now is "+connectTimeOutTimes);
            }
            this.connectTimeOutTimes = connectTimeOutTimes;
            return this;
        }
 
        /** Builds configured {@link BleParamsOptions} object */
        public BleParamsOptions build() {
            return new BleParamsOptions(this);
        }
    }
 
    public static BleParamsOptions createDefault() {
        return new Builder().build();
    }
 
}