1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-22 06:41:06 +02:00

Fix Bonding for the Casio GB5600/6900/STB-1000 series

This commit is contained in:
Andreas Böhler 2023-07-23 21:59:04 +02:00 committed by José Rebelo
parent 8bd7e103d0
commit 6ef6c9be43
11 changed files with 150 additions and 23 deletions

View File

@ -752,6 +752,16 @@ public class DiscoveryActivityV2 extends AbstractGBActivity implements AdapterVi
return this.deviceTarget;
}
@Override
public String getMacAddress() {
return deviceTarget.getDevice().getAddress();
}
@Override
public boolean getAttemptToConnect() {
return true;
}
@Override
public void registerBroadcastReceivers() {
final IntentFilter bluetoothIntents = new IntentFilter();

View File

@ -54,7 +54,7 @@ public class CasioGB6900DeviceCoordinator extends CasioDeviceCoordinator {
@Override
public int getBondingStyle(){
return BONDING_STYLE_BOND;
return BONDING_STYLE_LAZY;
}
@Override

View File

@ -110,6 +110,16 @@ public class LenovoWatchPairingActivity extends AbstractGBActivity implements Bo
return this.deviceCandidate;
}
@Override
public String getMacAddress() {
return deviceCandidate.getDevice().getAddress();
}
@Override
public boolean getAttemptToConnect() {
return true;
}
@Override
protected void onResume() {
registerBroadcastReceivers();

View File

@ -143,7 +143,7 @@ public class MiBandPairingActivity extends AbstractGBActivity implements Bonding
return;
}
BondingUtil.tryBondThenComplete(this, deviceCandidate);
BondingUtil.tryBondThenComplete(this, deviceCandidate.getDevice(), deviceCandidate.getMacAddress());
}
@ -189,6 +189,16 @@ public class MiBandPairingActivity extends AbstractGBActivity implements Bonding
super.onResume();
}
@Override
public String getMacAddress() {
return deviceCandidate.getDevice().getAddress();
}
@Override
public boolean getAttemptToConnect() {
return true;
}
@Override
protected void onStart() {
registerBroadcastReceivers();

View File

@ -125,7 +125,7 @@ public class PebblePairingActivity extends AbstractGBActivity implements Bonding
btDevice.getBondState() == BluetoothDevice.BOND_BONDING) {
BondingUtil.connectThenComplete(this, deviceCandidate);
} else {
BondingUtil.tryBondThenComplete(this, deviceCandidate);
BondingUtil.tryBondThenComplete(this, deviceCandidate.getDevice(), deviceCandidate.getDevice().getAddress());
}
}
@ -187,6 +187,16 @@ public class PebblePairingActivity extends AbstractGBActivity implements Bonding
return this.deviceCandidate;
}
@Override
public String getMacAddress() {
return deviceCandidate.getDevice().getAddress();
}
@Override
public boolean getAttemptToConnect() {
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

View File

@ -101,6 +101,16 @@ public class Watch9PairingActivity extends AbstractGBActivity implements Bonding
return this.deviceCandidate;
}
@Override
public String getMacAddress() {
return deviceCandidate.getDevice().getAddress();
}
@Override
public boolean getAttemptToConnect() {
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

View File

@ -28,6 +28,7 @@ import androidx.annotation.RequiresApi;
import java.util.Arrays;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.BondAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.NotifyAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.ReadAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.RequestConnectionPriorityAction;
@ -87,6 +88,11 @@ public class TransactionBuilder {
);
}
public TransactionBuilder bond() {
BondAction action = new BondAction();
return add(action);
}
public TransactionBuilder notify(BluetoothGattCharacteristic characteristic, boolean enable) {
if (characteristic == null) {
LOG.warn("Unable to notify characteristic: null");

View File

@ -0,0 +1,66 @@
package nodomain.freeyourgadget.gadgetbridge.service.btle.actions;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.BondingInterface;
import nodomain.freeyourgadget.gadgetbridge.util.BondingUtil;
public class BondAction extends PlainAction implements BondingInterface {
private String mMacAddress;
private final BroadcastReceiver pairingReceiver = BondingUtil.getPairingReceiver(this);
private final BroadcastReceiver bondingReceiver = BondingUtil.getBondingReceiver(this);
@Override
public void onBondingComplete(boolean success) {
unregisterBroadcastReceivers();
}
@Override
public GBDeviceCandidate getCurrentTarget() {
return null;
}
@Override
public String getMacAddress() {
return mMacAddress;
}
@Override
public boolean getAttemptToConnect() {
return false;
}
@Override
public void unregisterBroadcastReceivers() {
AndroidUtils.safeUnregisterBroadcastReceiver(LocalBroadcastManager.getInstance(GBApplication.getContext()), pairingReceiver);
AndroidUtils.safeUnregisterBroadcastReceiver(GBApplication.getContext(), bondingReceiver);
}
@Override
public void registerBroadcastReceivers() {
LocalBroadcastManager.getInstance(GBApplication.getContext()).registerReceiver(pairingReceiver, new IntentFilter(GBDevice.ACTION_DEVICE_CHANGED));
getContext().registerReceiver(bondingReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
}
@Override
public Context getContext() {
return GBApplication.getContext();
}
@Override
public boolean run(BluetoothGatt gatt) {
mMacAddress = gatt.getDevice().getAddress();
BondingUtil.tryBondThenComplete(this, gatt.getDevice(), gatt.getDevice().getAddress());
return true;
}
}

View File

@ -60,6 +60,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.casio.CasioSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.casio.gb6900.CasioGB6900HandlerThread;
import nodomain.freeyourgadget.gadgetbridge.service.devices.casio.gb6900.InitOperation;
import nodomain.freeyourgadget.gadgetbridge.service.devices.casio.gb6900.SetAlarmOperation;
import nodomain.freeyourgadget.gadgetbridge.util.BondingUtil;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
@ -151,6 +152,7 @@ public class CasioGB6900DeviceSupport extends CasioSupport {
setInitialized();
getDevice().setFirmwareVersion("N/A");
getDevice().setFirmwareVersion2("N/A");
builder.bond();
return builder;
}

View File

@ -54,6 +54,9 @@ public interface BondingInterface {
**/
void unregisterBroadcastReceivers();
String getMacAddress();
boolean getAttemptToConnect();
/**
* This forces bonding activities to handle the addition
* of all broadcast receivers in the same place

View File

@ -68,7 +68,7 @@ public class BondingUtil {
if (GBDevice.ACTION_DEVICE_CHANGED.equals(intent.getAction())) {
GBDevice device = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
LOG.debug("Pairing receiver: device changed: " + device);
if (activity.getCurrentTarget().getDevice().getAddress().equals(device.getAddress())) {
if (activity.getMacAddress().equals(device.getAddress())) {
if (device.isInitialized()) {
LOG.info("Device is initialized, finish things up");
activity.onBondingComplete(true);
@ -90,7 +90,7 @@ public class BondingUtil {
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.getCurrentTarget().getDevice().getAddress();
String bondingMacAddress = bondingInterface.getMacAddress();
LOG.info("Bond state changed: " + device + ", state: " + device.getBondState() + ", expected address: " + bondingMacAddress);
if (bondingMacAddress != null && bondingMacAddress.equals(device.getAddress())) {
@ -99,8 +99,8 @@ public class BondingUtil {
case BluetoothDevice.BOND_BONDED: {
LOG.info("Bonded with " + device.getAddress());
//noinspection StatementWithEmptyBody
if (isLePebble(device)) {
// Do not initiate connection to LE Pebble!
if (isLePebble(device) || !bondingInterface.getAttemptToConnect()) {
// Do not initiate connection to LE Pebble and some others!
} else {
attemptToFirstConnect(bondingInterface.getCurrentTarget().getDevice());
}
@ -108,7 +108,8 @@ public class BondingUtil {
}
case BluetoothDevice.BOND_NONE: {
LOG.info("Not bonded with " + device.getAddress() + ", attempting to connect anyway.");
attemptToFirstConnect(bondingInterface.getCurrentTarget().getDevice());
if(bondingInterface.getAttemptToConnect())
attemptToFirstConnect(bondingInterface.getCurrentTarget().getDevice());
return;
}
case BluetoothDevice.BOND_BONDING: {
@ -203,7 +204,7 @@ public class BondingUtil {
.setPositiveButton(bondingInterface.getContext().getString(R.string.discovery_yes_pair), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
BondingUtil.tryBondThenComplete(bondingInterface, deviceCandidate);
BondingUtil.tryBondThenComplete(bondingInterface, deviceCandidate.getDevice(), deviceCandidate.getMacAddress());
}
})
.setNegativeButton(R.string.discovery_dont_pair, new DialogInterface.OnClickListener() {
@ -214,7 +215,7 @@ public class BondingUtil {
})
.show();
} else {
BondingUtil.tryBondThenComplete(bondingInterface, deviceCandidate);
BondingUtil.tryBondThenComplete(bondingInterface, deviceCandidate.getDevice(), deviceCandidate.getMacAddress());
}
LOG.debug("Bonding initiated");
}
@ -223,8 +224,7 @@ public class BondingUtil {
* Tries to create a BluetoothDevice bond
* Do not call directly, use createBond(Activity, GBDeviceCandidate) instead!
*/
private static void bluetoothBond(BondingInterface context, GBDeviceCandidate candidate) {
BluetoothDevice device = candidate.getDevice();
private static void bluetoothBond(BondingInterface context, BluetoothDevice device) {
if (device.createBond()) {
// Async, results will be delivered via a broadcast
LOG.info("Bonding in progress...");
@ -273,7 +273,7 @@ public class BondingUtil {
if (deviceToPair != null) {
if (bondingInterface.getCurrentTarget().getDevice().getAddress().equals(deviceToPair.getAddress())) {
if (deviceToPair.getBondState() != BluetoothDevice.BOND_BONDED) {
BondingUtil.bluetoothBond(bondingInterface, bondingInterface.getCurrentTarget());
BondingUtil.bluetoothBond(bondingInterface, bondingInterface.getCurrentTarget().getDevice());
} else {
bondingInterface.onBondingComplete(true);
}
@ -297,9 +297,10 @@ public class BondingUtil {
*/
@RequiresApi(Build.VERSION_CODES.O)
private static void companionDeviceManagerBond(BondingInterface bondingInterface,
final GBDeviceCandidate deviceCandidate) {
BluetoothDevice device,
String macAddress) {
BluetoothDeviceFilter deviceFilter = new BluetoothDeviceFilter.Builder()
.setAddress(deviceCandidate.getMacAddress())
.setAddress(macAddress)
.build();
AssociationRequest pairingRequest = new AssociationRequest.Builder()
@ -308,14 +309,14 @@ public class BondingUtil {
.build();
CompanionDeviceManager manager = (CompanionDeviceManager) bondingInterface.getContext().getSystemService(Context.COMPANION_DEVICE_SERVICE);
LOG.debug(String.format("Searching for %s associations", deviceCandidate.getMacAddress()));
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(deviceCandidate.getMacAddress())) {
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, deviceCandidate);
BondingUtil.bluetoothBond(bondingInterface, device);
return;
}
}
@ -354,9 +355,8 @@ public class BondingUtil {
/**
* Use this function to initiate bonding to a GBDeviceCandidate
*/
public static void tryBondThenComplete(BondingInterface bondingInterface, GBDeviceCandidate deviceCandidate) {
public static void tryBondThenComplete(BondingInterface bondingInterface, BluetoothDevice device, String macAddress) {
bondingInterface.registerBroadcastReceivers();
BluetoothDevice device = deviceCandidate.getDevice();
int bondState = device.getBondState();
if (bondState == BluetoothDevice.BOND_BONDED) {
@ -377,12 +377,12 @@ public class BondingUtil {
}
GB.toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.pairing_creating_bond_with, device.getName(), device.getAddress()), Toast.LENGTH_LONG, GB.INFO);
toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.discovery_attempting_to_pair, deviceCandidate.getName()), Toast.LENGTH_SHORT, GB.INFO);
toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.discovery_attempting_to_pair, macAddress), Toast.LENGTH_SHORT, GB.INFO);
if (GBApplication.getPrefs().getBoolean("enable_companiondevice_pairing", true) &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
companionDeviceManagerBond(bondingInterface, deviceCandidate);
companionDeviceManagerBond(bondingInterface, device, macAddress);
} else {
bluetoothBond(bondingInterface, deviceCandidate);
bluetoothBond(bondingInterface, device);
}
}