wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*  Copyright (C) 2024 Daniel Dakhno, José Rebelo, Andreas Shimokawa
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
 
package nodomain.freeyourgadget.gadgetbridge.service.btle;
 
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
 
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.List;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
 
public class BLEScanService extends Service {
    public static final String COMMAND_SCAN_DEVICE = "nodomain.freeyourgadget.gadgetbridge.service.ble.scan.command.START_SCAN_FOR_DEVICE";
    public static final String COMMAND_START_SCAN_ALL = "nodomain.freeyourgadget.gadgetbridge.service.ble.scan.command.START_SCAN_ALL";
    public static final String COMMAND_STOP_SCAN_ALL = "nodomain.freeyourgadget.gadgetbridge.service.ble.scan.command.STOP_SCAN_ALL";
 
    public static final String EVENT_DEVICE_FOUND = "nodomain.freeyourgadget.gadgetbridge.service.ble.scan.event.DEVICE_FOUND";
 
    public static final String EXTRA_DEVICE = "EXTRA_DEVICE";
    public static final String EXTRA_DEVICE_ADDRESS = "EXTRA_DEVICE_ADDRESS";
    public static final String EXTRA_RSSI = "EXTRA_RSSI";
 
    // 5 minutes scan restart interval
    private final int DELAY_SCAN_RESTART = 5 * 60 * 1000;
 
    private LocalBroadcastManager localBroadcastManager;
    private NotificationManager notificationManager;
    private BluetoothManager bluetoothManager;
    private BluetoothLeScanner scanner;
 
    private Logger LOG = LoggerFactory.getLogger(getClass());
    // private final ArrayList<ScanFilter> currentFilters = new ArrayList<>();
 
    private enum ScanningState {
        NOT_SCANNING,
        SCANNING_WITHOUT_FILTERS,
        SCANNING_WITH_FILTERS;
 
        public boolean isDoingAnyScan() {
            return ordinal() > NOT_SCANNING.ordinal();
        }
 
        public boolean shouldDiscardAfterFirstMatch() {
            return this == SCANNING_WITH_FILTERS;
        }
    }
 
    private ScanningState currentState = ScanningState.NOT_SCANNING;
 
    private final ScanCallback scanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            BluetoothDevice device = result.getDevice();
 
            LOG.debug("onScanResult: " + result);
 
            Intent intent = new Intent(EVENT_DEVICE_FOUND);
            intent.putExtra(EXTRA_DEVICE_ADDRESS, device.getAddress());
            intent.putExtra(EXTRA_RSSI, result.getRssi());
            localBroadcastManager.sendBroadcast(intent);
 
            // device found, attempt connection
            // stop scanning for device for now
            // will restart when connection attempt fails
            if (currentState.shouldDiscardAfterFirstMatch()) {
                // stopScanningForDevice(device.getAddress());
            }
        }
 
        @Override
        public void onScanFailed(int errorCode) {
            super.onScanFailed(errorCode);
 
            LOG.error("onScanFailed: " + errorCode);
 
            updateNotification(getString(R.string.error_scan_failed, errorCode));
        }
    };
 
    @Override
    public void onCreate() {
        super.onCreate();
 
        bluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        scanner = bluetoothManager.getAdapter().getBluetoothLeScanner();
 
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
 
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 
        registerReceivers();
 
        this.startForeground();
 
        if (scanner == null) {
            updateNotification(getString(R.string.waiting_for_bluetooth));
        } else {
            restartScan(true);
        }
 
        // schedule after 5 seconds to fix weird timing of both services
        scheduleRestartScan(5000);
    }
 
    private void scheduleRestartScan() {
        scheduleRestartScan(DELAY_SCAN_RESTART);
    }
 
    private void scheduleRestartScan(long millis) {
        Handler handler = new Handler();
        handler.postDelayed(() -> {
            LOG.debug("restarting scan...");
            try {
                restartScan(true);
            } catch (Exception e) {
                LOG.error("error during scheduled scan restart", e);
            }
            scheduleRestartScan();
        }, millis);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceivers();
    }
 
    private void updateNotification(boolean isScanning, int scannedDeviceCount) {
        notificationManager.notify(
                GB.NOTIFICATION_ID_SCAN,
                createNotification(isScanning, scannedDeviceCount)
        );
    }
 
    private void updateNotification(String content) {
        notificationManager.notify(
                GB.NOTIFICATION_ID_SCAN,
                createNotification(content, R.drawable.ic_bluetooth)
        );
    }
 
    private Notification createNotification(boolean isScanning, int scannedDevicesCount) {
        int icon = R.drawable.ic_bluetooth;
        String content = getString(R.string.scan_not_scanning);
        if (isScanning) {
            icon = R.drawable.ic_bluetooth_searching;
            if (scannedDevicesCount == 1) {
                content = getString(R.string.scan_scanning_single_device);
            } else if (scannedDevicesCount > 1) {
                content = getString(R.string.scan_scanning_multiple_devices, scannedDevicesCount);
            } else {
                content = getString(R.string.scan_scanning_all_devices);
            }
        }
 
        return createNotification(content, icon);
    }
 
    private Notification createNotification(String content, int icon) {
 
        return new NotificationCompat
                .Builder(this, GB.NOTIFICATION_CHANNEL_ID_SCAN_SERVICE)
                .setContentTitle(getString(R.string.notification_channel_scan_service_name))
                .setContentText(content)
                .setSmallIcon(icon)
                .build();
    }
 
    private void startForeground() {
        Notification serviceNotification = createNotification(false, 0);
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            super.startForeground(GB.NOTIFICATION_ID_SCAN, serviceNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE);
        } else {
            super.startForeground(GB.NOTIFICATION_ID_SCAN, serviceNotification);
        }
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null) {
            return START_STICKY;
        }
        String action = intent.getAction();
        if (action == null) {
            return START_STICKY;
        }
        switch (action) {
            case COMMAND_SCAN_DEVICE:
                handleScanDevice(intent);
                break;
            case COMMAND_START_SCAN_ALL:
                handleScanAll(intent);
                break;
            case COMMAND_STOP_SCAN_ALL:
                handleStopScanAll(intent);
                break;
            default:
                return START_STICKY;
        }
        return START_STICKY;
    }
 
    private void handleStopScanAll(Intent intent) {
        restartScan(true);
    }
 
    private void handleScanAll(Intent intent) {
        if (currentState != ScanningState.SCANNING_WITHOUT_FILTERS) {
            restartScan(false);
        }
    }
 
    private void handleScanDevice(Intent intent) {
        restartScan(true);
    }
 
    BroadcastReceiver deviceStateUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
                GBDevice.DeviceUpdateSubject subject =
                        (GBDevice.DeviceUpdateSubject)
                                intent.getSerializableExtra(GBDevice.EXTRA_UPDATE_SUBJECT);
 
                if (subject != GBDevice.DeviceUpdateSubject.CONNECTION_STATE) {
                    return;
                }
                restartScan(true);
                return;
            }
            if (GBApplication.ACTION_QUIT.equals(intent.getAction())) {
                LOG.debug("stopping scan service...");
                if (currentState.isDoingAnyScan()) {
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                        // this should never happen
                        LOG.error("No BLUETOOTH_SCAN permission");
                        return;
                    }
 
                    scanner.stopScan(scanCallback);
                }
                stopSelf();
            }
        }
    };
 
    BroadcastReceiver bluetoothStateChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) {
                return;
            }
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            switch (state) {
                case BluetoothAdapter.STATE_OFF:
                case BluetoothAdapter.STATE_TURNING_OFF:
                    updateNotification(getString(R.string.waiting_for_bluetooth));
                    break;
                case BluetoothAdapter.STATE_ON:
                    restartScan(true);
                    break;
            }
        }
    };
 
    private void registerReceivers() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(GBDevice.ACTION_DEVICE_CHANGED);
        filter.addAction(GBApplication.ACTION_QUIT);
        localBroadcastManager.registerReceiver(
                deviceStateUpdateReceiver,
                filter
        );
 
        ContextCompat.registerReceiver(
                this,
                bluetoothStateChangedReceiver,
                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED),
                ContextCompat.RECEIVER_EXPORTED
        );
    }
 
    private void unregisterReceivers() {
        localBroadcastManager.unregisterReceiver(deviceStateUpdateReceiver);
 
        unregisterReceiver(bluetoothStateChangedReceiver);
    }
 
    private boolean hasBluetoothPermission() {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
            // workaround. Cannot give bluetooth permission on Android O
            LOG.warn("Running on android 11, skipping bluetooth permission check");
            return ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        }
        return ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED;
    }
 
    @SuppressLint("MissingPermission") // linter does not recognize the usage of hasBluetoothPermission
    private void restartScan(boolean applyFilters) {
        if (!hasBluetoothPermission()) {
            // this should never happen
            LOG.error("No BLUETOOTH_SCAN permission");
            updateNotification("Missing Bluetooth scan permissions");
            return;
        }
 
        if (scanner == null) {
            scanner = bluetoothManager.getAdapter().getBluetoothLeScanner();
        }
        if (scanner == null) {
            // at this point we should already be waiting for bluetooth to turn back on
            LOG.debug("cannot enable scan since bluetooth seems off (scanner == null)");
            return;
        }
        if (bluetoothManager.getAdapter().getState() != BluetoothAdapter.STATE_ON) {
            // again, we should be waiting for the adapter to turn on again
            LOG.debug("Bluetooth adapter state off");
            return;
        }
        if (currentState.isDoingAnyScan()) {
            if (hasBluetoothPermission()) {
                scanner.stopScan(scanCallback);
            }
        }
        ArrayList<ScanFilter> scanFilters = null;
 
        if (applyFilters) {
            List<GBDevice> devices = GBApplication.app().getDeviceManager().getDevices();
 
            scanFilters = new ArrayList<>(devices.size());
 
            for (GBDevice device : devices) {
                if (device.getState() == GBDevice.State.WAITING_FOR_SCAN) {
                    scanFilters.add(new ScanFilter.Builder()
                            .setDeviceAddress(device.getAddress())
                            .build()
                    );
                }
            }
 
            if (scanFilters.isEmpty()) {
                // no need to start scanning
                LOG.debug("restartScan: stopping BLE scan, no devices");
                currentState = ScanningState.NOT_SCANNING;
                updateNotification(false, 0);
                return;
            }
        }
 
        final ScanSettings.Builder scanSettingsBuilder = new ScanSettings.Builder()
                .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER); // enforced anyway in background
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            scanSettingsBuilder
                    .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
                    .setMatchMode(ScanSettings.MATCH_MODE_STICKY);
        }
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            scanSettingsBuilder.setLegacy(false);
        }
 
        scanner.startScan(scanFilters, scanSettingsBuilder.build(), scanCallback);
        if (applyFilters) {
            LOG.debug("restartScan: started scan for " + scanFilters.size() + " devices");
            updateNotification(true, scanFilters.size());
            currentState = ScanningState.SCANNING_WITH_FILTERS;
        } else {
            LOG.debug("restartScan: started scan for all devices");
            updateNotification(true, 0);
            currentState = ScanningState.SCANNING_WITHOUT_FILTERS;
        }
 
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}