Discover bonded devices by default

A lof of devices will simply work anyway even if already paired in
Android bluetooth settings. Discover them by default, but warn the user
if the device is not known to pair correctly if already paired in Android
settings. Allows this warning to be disabled to known working devices.
This commit is contained in:
José Rebelo 2024-03-22 21:32:59 +00:00 committed by José Rebelo
parent f05043481c
commit 8085eb6ec3
11 changed files with 49 additions and 36 deletions

View File

@ -611,6 +611,22 @@ public class DiscoveryActivityV2 extends AbstractGBActivity implements AdapterVi
}
}
if (coordinator.suggestUnbindBeforePair() && deviceCandidate.isBonded()) {
new MaterialAlertDialogBuilder(getContext())
.setTitle(R.string.unbind_before_pair_title)
.setMessage(R.string.unbind_before_pair_message)
.setIcon(R.drawable.ic_warning_gray)
.setPositiveButton(R.string.ok, (dialog, whichButton) -> {
startPair(deviceCandidate, coordinator);
})
.setNegativeButton(android.R.string.cancel, null)
.show();
} else {
startPair(deviceCandidate, coordinator);
}
}
private void startPair(final GBDeviceCandidate deviceCandidate, final DeviceCoordinator coordinator) {
final Class<? extends Activity> pairingActivity = coordinator.getPairingActivity();
if (pairingActivity != null) {
final Intent intent = new Intent(this, pairingActivity);
@ -784,7 +800,6 @@ public class DiscoveryActivityV2 extends AbstractGBActivity implements AdapterVi
private void loadSettings() {
final Prefs prefs = GBApplication.getPrefs();
deviceFoundProcessor.setIgnoreBonded(prefs.getBoolean("ignore_bonded_devices", true));
deviceFoundProcessor.setDiscoverUnsupported(prefs.getBoolean("discover_unsupported_devices", false));
}

View File

@ -24,13 +24,11 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@ -55,14 +53,11 @@ public final class GBScanEventProcessor implements Runnable {
private static final ParcelUuid ZERO_UUID = ParcelUuid.fromString("00000000-0000-0000-0000-000000000000");
// Devices that can be ignored by just the address (eg. already bonded)
private final Set<String> devicesToIgnore = new HashSet<>();
private final Map<String, GBDeviceCandidate> candidatesByAddress = new LinkedHashMap<>();
private final BlockingQueue<String> eventsToProcessQueue = new LinkedBlockingQueue<>();
private final Map<String, List<GBScanEvent>> eventsToProcessMap = new HashMap<>();
private boolean ignoreBonded = true;
private boolean discoverUnsupported = false;
private volatile boolean running = false;
@ -121,16 +116,11 @@ public final class GBScanEventProcessor implements Runnable {
}
public void clear() {
devicesToIgnore.clear();
candidatesByAddress.clear();
eventsToProcessMap.clear();
eventsToProcessQueue.clear();
}
public void setIgnoreBonded(boolean ignoreBonded) {
this.ignoreBonded = ignoreBonded;
}
public void setDiscoverUnsupported(boolean discoverUnsupported) {
this.discoverUnsupported = discoverUnsupported;
}
@ -203,11 +193,6 @@ public final class GBScanEventProcessor implements Runnable {
return false;
}
if (devicesToIgnore.contains(address)) {
LOG.trace("Ignoring {} events for {}", events.size(), address);
return false;
}
LOG.debug("Processing {} events for {}", events.size(), address);
GBDeviceCandidate candidate = candidatesByAddress.get(address);
@ -228,12 +213,6 @@ public final class GBScanEventProcessor implements Runnable {
previousUuids = candidate.getServiceUuids();
}
if (candidate.isBonded() && ignoreBonded) {
LOG.trace("Ignoring already bonded device {}", address);
devicesToIgnore.add(address);
return false;
}
// Update the device with the remaining events
for (final GBScanEvent event : events) {
candidate.setRssi(event.getRssi());

View File

@ -17,7 +17,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.adapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
@ -30,7 +29,6 @@ import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
@ -76,9 +74,6 @@ public class DeviceCandidateAdapter extends ArrayAdapter<GBDeviceCandidate> {
final List<String> statusLines = new ArrayList<>();
if (device.isBonded()) {
statusLines.add(getContext().getString(R.string.device_is_currently_bonded));
if (!GBApplication.getPrefs().getBoolean("ignore_bonded_devices", true)) { // This could be passed to the constructor instead
deviceImageView.setImageResource(coordinator.getDisabledIconResource());
}
}
if (!deviceType.isSupported()) {

View File

@ -382,6 +382,11 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
return BONDING_STYLE_ASK;
}
@Override
public boolean suggestUnbindBeforePair() {
return true;
}
@Override
public boolean isExperimental() {
return false;

View File

@ -457,6 +457,12 @@ public interface DeviceCoordinator {
*/
int getBondingStyle();
/**
* Whether it is recommended to unbind the device before pairing due to compatibility issues. Returns false
* if the device is known to pair without issues even when already bound in Android bluetooth settings.
*/
boolean suggestUnbindBeforePair();
/**
* Returns true if this device is in an experimental state / not tested.
*/

View File

@ -148,6 +148,11 @@ public class BangleJSCoordinator extends AbstractBLEDeviceCoordinator {
return BuildConfig.INTERNET_ACCESS ? AppsManagementActivity.class : null;
}
@Override
public boolean suggestUnbindBeforePair() {
return false;
}
@Override
protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) {
}

View File

@ -87,6 +87,11 @@ public class CmfWatchProCoordinator extends AbstractBLEDeviceCoordinator {
return handler.isValid() ? handler : null;
}
@Override
public boolean suggestUnbindBeforePair() {
return false;
}
@Override
protected void deleteDevice(@NonNull final GBDevice gbDevice,
@NonNull final Device device,

View File

@ -78,6 +78,11 @@ public abstract class HuamiCoordinator extends AbstractBLEDeviceCoordinator {
return Collections.singletonList(filter);
}
@Override
public boolean suggestUnbindBeforePair() {
return false;
}
@Override
protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException {
Long deviceId = device.getId();

View File

@ -58,6 +58,11 @@ public abstract class SonyHeadphonesCoordinator extends AbstractBLClassicDeviceC
return new SonyHeadphonesSettingsCustomizer(device);
}
@Override
public boolean suggestUnbindBeforePair() {
return false;
}
@Override
protected void deleteDevice(@NonNull GBDevice gbDevice, @NonNull Device device, @NonNull DaoSession session) throws GBException {
}

View File

@ -1812,8 +1812,6 @@
<string name="error_exporting_device_preferences">Error exporting device specific preferences</string>
<string name="error_setting_alias">Error setting alias: </string>
<string name="error_retrieving_devices_database">Error retrieving devices from database</string>
<string name="ignore_bonded_devices">Ignore bonded devices</string>
<string name="ignore_bonded_devices_description">Enabling this option will ignore devices that have been bonded/paired already when scanning</string>
<string name="discover_unsupported_devices">Discover unsupported devices</string>
<string name="discover_unsupported_devices_description">Enabling this option will display all discovered bluetooth devices when scanning. Short tap will copy device name and mac address to clipboard. Long press will launch `Add test device` dialog. Can cause potential app freezing issues.</string>
<string name="error_location_enabled_mandatory">Location must be turned on to scan for devices</string>
@ -2709,4 +2707,6 @@
<string name="scan_scanning_single_device">Scanning 1 device</string>
<string name="scan_scanning_multiple_devices">Scanning %d devices</string>
<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>
</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="true"
android:key="ignore_bonded_devices"
android:layout="@layout/preference_checkbox"
android:summary="@string/ignore_bonded_devices_description"
android:title="@string/ignore_bonded_devices"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="enable_companiondevice_pairing"