1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +01:00

Ask for companion device during pairing

This commit is contained in:
José Rebelo 2024-03-22 22:13:23 +00:00 committed by José Rebelo
parent 8085eb6ec3
commit 852893408f
4 changed files with 38 additions and 30 deletions

View File

@ -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;

View File

@ -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
*

View File

@ -1797,7 +1797,7 @@
<string name="error_version_check_extreme_caution">CAUTION: Error when checking version information! You should not continue! Saw version name \"%s\"</string>
<string name="require_location_provider">Location must be enabled</string>
<string name="companiondevice_pairing">CompanionDevice Pairing</string>
<string name="companiondevice_pairing_details">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</string>
<string name="error_background_service">Failed to start background service</string>
<string name="error_background_service_reason_truncated">Starting the background service failed because…</string>
<string name="device_is_currently_bonded">ALREADY BONDED</string>
@ -2709,4 +2709,6 @@
<string name="notification_channel_scan_service_name">Scan service</string>
<string name="unbind_before_pair_title">Already bound</string>
<string name="unbind_before_pair_message">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.</string>
<string name="companion_pairing_request_title">Companion device</string>
<string name="companion_pairing_request_description">Pair this device as companion?.\n\nThis is recommended for some functions such as find device, and provides a better connection.</string>
</resources>

View File

@ -2,13 +2,6 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="enable_companiondevice_pairing"
android:layout="@layout/preference_checkbox"
android:summary="@string/companiondevice_pairing_details"
android:title="@string/companiondevice_pairing"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="discover_unsupported_devices"