Fix discovery of connected devices

Devices might be connected at bluetooth-level, but not yet paired in
Gadgetbridge. These devices will not fire discovery events, so
Gadgetbridge would not be able to discover them.
This commit is contained in:
José Rebelo 2024-09-10 22:23:54 +01:00
parent cde9dfb6a5
commit 721b582b87

View File

@ -70,10 +70,12 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
@ -283,6 +285,26 @@ public class DiscoveryActivityV2 extends AbstractGBActivity implements AdapterVi
refreshDeviceList(false); refreshDeviceList(false);
// Pre-add currently connected devices, as those will not trigger discovery events
// Paired devices that are not connected do not need to be added, as those will be discovered
try {
final Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
for (final BluetoothDevice device : pairedDevices) {
try {
final Method isConnectedMethod = device.getClass().getMethod("isConnected");
final Boolean isConnected = (Boolean) isConnectedMethod.invoke(device);
if (isConnected!= null && isConnected) {
LOG.debug("Pre-adding already bonded device {}", device.getAddress());
deviceFoundProcessor.scheduleProcessing(new GBScanEvent(device, (short) -1, null));
}
} catch (final Exception e) {
LOG.error("Failed to check whether {} is connected", device.getAddress());
}
}
} catch (final SecurityException e) {
LOG.error("Failed to pre-add paired devices", e);
}
try { try {
if (!ensureBluetoothReady()) { if (!ensureBluetoothReady()) {
toast(DiscoveryActivityV2.this, getString(R.string.discovery_enable_bluetooth), Toast.LENGTH_SHORT, GB.ERROR); toast(DiscoveryActivityV2.this, getString(R.string.discovery_enable_bluetooth), Toast.LENGTH_SHORT, GB.ERROR);