diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java index fa1cc7db5..a14788adf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java @@ -122,7 +122,6 @@ import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes; import nodomain.freeyourgadget.gadgetbridge.model.Weather; import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec; import nodomain.freeyourgadget.gadgetbridge.service.serial.GBDeviceProtocol; -import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BondingUtil.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BondingUtil.java index 73bed52b3..f7fe21638 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BondingUtil.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BondingUtil.java @@ -365,35 +365,35 @@ public class BondingUtil { /** * Use this function to initiate bonding to a GBDeviceCandidate */ - public static void tryBondThenComplete(BondingInterface bondingInterface, BluetoothDevice device, String macAddress) { + public static void tryBondThenComplete(final BondingInterface bondingInterface, final BluetoothDevice device, final String macAddress) { bondingInterface.registerBroadcastReceivers(); - int bondState = device.getBondState(); - if (bondState == BluetoothDevice.BOND_BONDED) { - GB.toast(bondingInterface.getContext().getString(R.string.pairing_already_bonded, device.getName(), device.getAddress()), Toast.LENGTH_SHORT, GB.INFO); - //noinspection StatementWithEmptyBody - if (GBApplication.getPrefs().getBoolean("enable_companiondevice_pairing", true) && - Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // If CompanionDeviceManager is enabled, skip connection and go bond - // TODO: It would theoretically be nice to check if it's already been granted, - // but re-bond works - } else { - attemptToFirstConnect(bondingInterface.getCurrentTarget().getDevice()); - return; - } - } else if (bondState == BluetoothDevice.BOND_BONDING) { + 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; } + final boolean companionPairingAvailable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; + + 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 (companionPairingAvailable && !isPebble2(device)) { + // If CompanionDeviceManager is enabled, 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); - toast(bondingInterface.getContext(), bondingInterface.getContext().getString(R.string.discovery_attempting_to_pair, macAddress), Toast.LENGTH_SHORT, GB.INFO); - boolean companionPairingEnabled = GBApplication.getPrefs().getBoolean("enable_companiondevice_pairing", true) && - Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; - - if (companionPairingEnabled && !isPebble2(device)) { - companionDeviceManagerBond(bondingInterface, device, macAddress); + if (companionPairingAvailable && !isPebble2(device)) { + askCompanionPairing(bondingInterface, device, macAddress); } else if (isPebble2(device)) { // TODO: start companionDevicePairing after connecting to Pebble 2 but before writing to pairing trigger attemptToFirstConnect(device); @@ -402,6 +402,20 @@ public class BondingUtil { } } + @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 * diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a497058f..e501c5bbe 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1797,7 +1797,7 @@ CAUTION: Error when checking version information! You should not continue! Saw version name \"%s\" Location must be enabled CompanionDevice Pairing - Enables the new CompanionDevice API support (only has an effect on Android 8 or above) which will increase reliability if the service needs to be restarted in the background, requires re-pairing using Gadgetbridge to have an effect + Failed to start background service Starting the background service failed becauseā€¦ ALREADY BONDED @@ -2709,4 +2709,6 @@ Scan service Already bound This device is already bound in Android settings, which can make pairing fail for some devices.\n\nIf adding the device fails, please remove it in Android settings and try again. + Companion device + Pair this device as companion?.\n\nThis is recommended for some functions such as find device, and provides a better connection. diff --git a/app/src/main/res/xml/discovery_pairing_preferences.xml b/app/src/main/res/xml/discovery_pairing_preferences.xml index 59aea4a59..12a00e932 100644 --- a/app/src/main/res/xml/discovery_pairing_preferences.xml +++ b/app/src/main/res/xml/discovery_pairing_preferences.xml @@ -2,13 +2,6 @@ -