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
431
432
433
434
435
436
437
438
439
440
441
/*  Copyright (C) 2020-2024 Andreas Böhler, Arjan Schrijver, Daniel Dakhno,
    José Rebelo, Taavi Eomäe
 
    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.util;
 
import static androidx.core.app.ActivityCompat.startIntentSenderForResult;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.toast;
 
import android.app.Activity;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.companion.AssociationRequest;
import android.companion.BluetoothDeviceFilter;
import android.companion.CompanionDeviceManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
 
import androidx.annotation.RequiresApi;
 
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.Locale;
 
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
 
public class BondingUtil {
    public static final String STATE_DEVICE_CANDIDATE = "stateDeviceCandidate";
 
    private static final int REQUEST_CODE = 1;
    private static final Logger LOG = LoggerFactory.getLogger(BondingUtil.class);
    private static final long DELAY_AFTER_BONDING = 1000; // 1s
 
    /**
     * Returns a BroadcastReceiver that handles Gadgetbridge's device changed broadcasts
     */
    public static BroadcastReceiver getPairingReceiver(final BondingInterface activity) {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
                    GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
                    LOG.debug("Pairing receiver: device changed: " + device);
                    if (activity.getMacAddress().equals(device.getAddress())) {
                        if (device.isInitialized()) {
                            LOG.info("Device is initialized, finish things up");
                            activity.onBondingComplete(true);
                        } else if (device.isConnecting() || device.isInitializing()) {
                            LOG.info("Still connecting/initializing device...");
                        }
                    }
                }
            }
        };
    }
 
    /**
     * Returns a BroadcastReceiver that handles Bluetooth chance broadcasts
     */
    public static BroadcastReceiver getBondingReceiver(final BondingInterface bondingInterface) {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    String bondingMacAddress = bondingInterface.getMacAddress();
 
                    LOG.info("Bond state changed: " + device + ", state: " + device.getBondState() + ", expected address: " + bondingMacAddress);
                    if (bondingMacAddress != null && bondingMacAddress.equals(device.getAddress())) {
                        int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
                        switch (bondState) {
                            case BluetoothDevice.BOND_BONDED: {
                                LOG.info("Bonded with " + device.getAddress());
                                //noinspection StatementWithEmptyBody
                                if (isLePebble(device) || isPebble2(device) || !bondingInterface.getAttemptToConnect()) {
                                    // Do not initiate connection to LE Pebble and some others!
                                } else {
                                    attemptToFirstConnect(bondingInterface.getCurrentTarget().getDevice());
                                }
                                return;
                            }
                            case BluetoothDevice.BOND_NONE: {
                                LOG.info("Not bonded with " + device.getAddress() + ", attempting to connect anyway.");
                                if(bondingInterface.getAttemptToConnect())
                                    attemptToFirstConnect(bondingInterface.getCurrentTarget().getDevice());
                                return;
                            }
                            case BluetoothDevice.BOND_BONDING: {
                                LOG.info("Bonding in progress with " + device.getAddress());
                                return;
                            }
                            default: {
                                LOG.warn("Unknown bond state for device " + device.getAddress() + ": " + bondState);
                                bondingInterface.onBondingComplete(false);
                            }
                        }
                    }
                }
            }
        };
    }
 
    /**
     * Connect to candidate after a certain delay
     *
     * @param candidate the device to connect to
     */
    public static void attemptToFirstConnect(final BluetoothDevice candidate) {
        Looper mainLooper = Looper.getMainLooper();
        new Handler(mainLooper).postDelayed(new Runnable() {
            @Override
            public void run() {
                GBApplication.deviceService().disconnect();
                GBDevice device = DeviceHelper.getInstance().toSupportedDevice(candidate);
                connectToGBDevice(device);
            }
        }, DELAY_AFTER_BONDING);
    }
 
    /**
     * Just calls DeviceService connect with the "first time" flag
     */
    private static void connectToGBDevice(GBDevice device) {
        if (device != null) {
            GBApplication.deviceService(device).connect(true);
        } else {
            GB.toast("Unable to connect, can't recognize the device type", Toast.LENGTH_LONG, GB.ERROR);
        }
    }
 
    /**
     * Connects to the device and calls callback
     */
    public static void connectThenComplete(BondingInterface bondingInterface, GBDeviceCandidate deviceCandidate) {
        GBDevice device = DeviceHelper.getInstance().toSupportedDevice(deviceCandidate);
        connectThenComplete(bondingInterface, device);
    }
 
    /**
     * Connects to the device and calls callback
     */
    public static void connectThenComplete(BondingInterface bondingInterface, GBDevice device) {
        toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.discovery_trying_to_connect_to, device.getName()), Toast.LENGTH_SHORT, GB.INFO);
        // Disconnect when LE Pebble so that the user can manually initiate a connection
        GBApplication.deviceService(device).disconnect();
        GBApplication.deviceService(device).connect(true);
        bondingInterface.onBondingComplete(true);
    }
 
    /**
     * Checks the type of bonding needed for the device and continues accordingly
     */
    public static void initiateCorrectBonding(final BondingInterface bondingInterface, final GBDeviceCandidate deviceCandidate, DeviceCoordinator coordinator) {
        int bondingStyle = coordinator.getBondingStyle();
        if (bondingStyle == DeviceCoordinator.BONDING_STYLE_NONE ||
            bondingStyle == DeviceCoordinator.BONDING_STYLE_LAZY ) {
            // Do nothing
            return;
        } else if (bondingStyle == DeviceCoordinator.BONDING_STYLE_ASK) {
            new MaterialAlertDialogBuilder(bondingInterface.getContext())
                    .setCancelable(true)
                    .setTitle(bondingInterface.getContext().getString(R.string.discovery_pair_title, deviceCandidate.getName()))
                    .setMessage(bondingInterface.getContext().getString(R.string.discovery_pair_question))
                    .setPositiveButton(bondingInterface.getContext().getString(R.string.discovery_yes_pair), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            BondingUtil.tryBondThenComplete(bondingInterface, deviceCandidate.getDevice(), deviceCandidate.getMacAddress());
                        }
                    })
                    .setNegativeButton(R.string.discovery_dont_pair, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            BondingUtil.connectThenComplete(bondingInterface, deviceCandidate);
                        }
                    })
                    .show();
        } else {
            BondingUtil.tryBondThenComplete(bondingInterface, deviceCandidate.getDevice(), deviceCandidate.getMacAddress());
        }
        LOG.debug("Bonding initiated");
    }
 
    /**
     * Tries to create a BluetoothDevice bond
     * Do not call directly, use createBond(Activity, GBDeviceCandidate) instead!
     */
    private static void bluetoothBond(BondingInterface context, BluetoothDevice device) {
        if (device.createBond()) {
            // Async, results will be delivered via a broadcast
            LOG.info("Bonding in progress...");
        } else {
            LOG.error(String.format(Locale.getDefault(),
                    "Bonding failed immediately! %1$s (%2$s) %3$d",
                    device.getName(),
                    device.getAddress(),
                    device.getType())
            );
 
            BluetoothClass bluetoothClass = device.getBluetoothClass();
            if (bluetoothClass != null) {
                LOG.error(String.format(Locale.getDefault(),
                        "BluetoothClass: %1$s",
                        bluetoothClass.toString()));
            }
 
            // Theoretically we shouldn't be doing this
            // because this function shouldn't've been called
            // with an already bonded device
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                LOG.warn("For some reason the device is already bonded, but let's try first connect");
                attemptToFirstConnect(context.getCurrentTarget().getDevice());
            } else if (device.getBondState() == BluetoothDevice.BOND_BONDING) {
                LOG.warn("Device is still bonding after an error");
                // TODO: Not sure we can handle this better, it's weird already.
            } else {
                LOG.warn("Bonding failed immediately and no bond was made");
                toast(context.getContext(), context.getContext().getString(R.string.discovery_bonding_failed_immediately, device.getName()), Toast.LENGTH_SHORT, GB.ERROR);
            }
        }
    }
 
    /**
     * Handles the activity result and checks if there's anything CompanionDeviceManager-related going on
     */
    public static void handleActivityResult(BondingInterface bondingInterface, int requestCode, int resultCode, Intent data) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
                requestCode == BondingUtil.REQUEST_CODE &&
                resultCode == Activity.RESULT_OK) {
 
            BluetoothDevice deviceToPair =
                    data.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE);
 
            if (deviceToPair != null) {
                if (bondingInterface.getCurrentTarget().getDevice().getAddress().equals(deviceToPair.getAddress())) {
                    if (deviceToPair.getBondState() != BluetoothDevice.BOND_BONDED) {
                        BondingUtil.bluetoothBond(bondingInterface, bondingInterface.getCurrentTarget().getDevice());
                    } else {
                        bondingInterface.onBondingComplete(true);
                    }
                } else {
                    bondingInterface.onBondingComplete(false);
                }
            }
        }
    }
 
    /**
     * Checks if device is LE Pebble
     */
    public static boolean isLePebble(BluetoothDevice device) {
        return (device.getType() == BluetoothDevice.DEVICE_TYPE_DUAL || device.getType() == BluetoothDevice.DEVICE_TYPE_LE) &&
                (device.getName().startsWith("Pebble-LE ") || device.getName().startsWith("Pebble Time LE "));
    }
 
    /**
     * Checks if device is Pebble 2
     */
    public static boolean isPebble2(BluetoothDevice device) {
        return device.getType() == BluetoothDevice.DEVICE_TYPE_LE &&
                device.getName().startsWith("Pebble ") &&
                !device.getName().startsWith("Pebble Time LE ");
    }
 
    /**
     * Uses the CompanionDeviceManager bonding method
     */
    @RequiresApi(Build.VERSION_CODES.O)
    private static void companionDeviceManagerBond(BondingInterface bondingInterface,
                                                   BluetoothDevice device,
                                                   String macAddress) {
        BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder()
                .setAddress(macAddress)
                .build();
 
        AssociationRequest pairingRequest = new AssociationRequest.Builder()
                .addDeviceFilter(deviceFilter)
                .setSingleDevice(true)
                .build();
 
        CompanionDeviceManager manager = (CompanionDeviceManager) bondingInterface.getContext().getSystemService(Context.COMPANION_DEVICE_SERVICE);
        LOG.debug(String.format("Searching for %s associations", macAddress));
        for (String association : manager.getAssociations()) {
            LOG.debug(String.format("Already associated with: %s", association));
            if (association.equals(macAddress)) {
                LOG.info("The device has already been bonded through CompanionDeviceManager, using regular");
                // If it's already "associated", we should immediately pair
                // because the callback is never called (AFAIK?)
                BondingUtil.bluetoothBond(bondingInterface, device);
                return;
            }
        }
 
        LOG.debug("Starting association request");
        manager.associate(pairingRequest,
                getCompanionDeviceManagerCallback(bondingInterface),
                null);
    }
 
    /**
     * This is a bit hacky, but it does stop a bonding that might be otherwise stuck,
     * use with some caution
     */
    public static void stopBluetoothBonding(BluetoothDevice device) {
        try {
            //noinspection JavaReflectionMemberAccess
            device.getClass().getMethod("cancelBondProcess").invoke(device);
        } catch (Throwable ignore) {
        }
    }
 
    /**
     * Finalizes bonded device
     */
    public static void handleDeviceBonded(BondingInterface bondingInterface, GBDeviceCandidate deviceCandidate) {
        if (deviceCandidate == null) {
            LOG.error("deviceCandidate was null! Can't handle bonded device!");
            return;
        }
 
        toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.discovery_successfully_bonded, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.INFO);
        connectThenComplete(bondingInterface, deviceCandidate);
    }
 
    /**
     * Use this function to initiate bonding to a GBDeviceCandidate
     */
    public static void tryBondThenComplete(final BondingInterface bondingInterface, final BluetoothDevice device, final String macAddress) {
        bondingInterface.registerBroadcastReceivers();
 
        final int bondState = device.getBondState();
 
        if (bondState == BluetoothDevice.BOND_BONDING) {
            GB.toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.pairing_in_progress, device.getName(), device.getAddress()), Toast.LENGTH_LONG, GB.INFO);
            return;
        }
 
        // FIXME: We can only attempt companion pairing if the context of the bondingInterface is an activity
        // however, when called from the BondAction it will always be the global Application context
        // See https://codeberg.org/Freeyourgadget/Gadgetbridge/issues/3784#issuecomment-2121792
        final boolean contextIsActivity = bondingInterface.getContext() instanceof Activity;
 
        if (bondState == BluetoothDevice.BOND_BONDED) {
            GB.toast(bondingInterface.getContext().getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isPebble2(device) && contextIsActivity) {
                // If CompanionDeviceManager is available, skip connection and go bond
                // TODO: It would theoretically be nice to check if it's already been granted,
                //  but re-bond works
                askCompanionPairing(bondingInterface, device, macAddress);
            } else {
                attemptToFirstConnect(bondingInterface.getCurrentTarget().getDevice());
            }
            return;
        }
 
        GB.toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.pairing_creating_bond_with, device.getName(), device.getAddress()), Toast.LENGTH_LONG, GB.INFO);
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isPebble2(device) && contextIsActivity) {
            askCompanionPairing(bondingInterface, device, macAddress);
        } else if (isPebble2(device)) {
            // TODO: start companionDevicePairing after connecting to Pebble 2 but before writing to pairing trigger
            attemptToFirstConnect(device);
        } else {
            bluetoothBond(bondingInterface, device);
        }
    }
 
    @RequiresApi(Build.VERSION_CODES.O)
    private static void askCompanionPairing(BondingInterface bondingInterface, BluetoothDevice device, String macAddress) {
        new MaterialAlertDialogBuilder(bondingInterface.getContext())
                .setTitle(R.string.companion_pairing_request_title)
                .setMessage(R.string.companion_pairing_request_description)
                .setPositiveButton(R.string.yes, (dialog, whichButton) -> {
                    companionDeviceManagerBond(bondingInterface, device, macAddress);
                })
                .setNegativeButton(R.string.no, (dialog, whichButton) -> {
                    bluetoothBond(bondingInterface, device);
                })
                .show();
    }
 
    /**
     * Returns a callback for CompanionDeviceManager
     *
     * @param bondingInterface the activity that started the CDM bonding process
     * @return CompanionDeviceManager.Callback that handles the CompanionDeviceManager bonding process results
     */
    @RequiresApi(Build.VERSION_CODES.O)
    private static CompanionDeviceManager.Callback getCompanionDeviceManagerCallback(final BondingInterface bondingInterface) {
        return new CompanionDeviceManager.Callback() {
            @Override
            public void onFailure(CharSequence error) {
                LOG.error("Bonding failed immediately: {}", error);
                toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.discovery_bonding_failed_immediately), Toast.LENGTH_SHORT, GB.ERROR);
            }
 
            @Override
            public void onDeviceFound(IntentSender chooserLauncher) {
                try {
                    startIntentSenderForResult((Activity) bondingInterface.getContext(),
                            chooserLauncher,
                            REQUEST_CODE,
                            null,
                            0,
                            0,
                            0,
                            null);
                } catch (IntentSender.SendIntentException e) {
                    LOG.error(e.toString());
                }
            }
        };
    }
}