Prevent companion device connection with bluetooth off

This commit is contained in:
José Rebelo
2025-12-29 18:43:30 +00:00
parent 45d8240b5c
commit 7ff237bdfd
@@ -19,6 +19,7 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents;
import static nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State.WAITING_FOR_RECONNECT;
import static nodomain.freeyourgadget.gadgetbridge.impl.GBDevice.State.WAITING_FOR_SCAN;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -63,7 +64,7 @@ public class BluetoothConnectReceiver extends BroadcastReceiver {
observedDevice(address);
}
public static void observedDevice(String address) {
public static void observedDevice(final String address) {
final DeviceManager manager = GBApplication.app().getDeviceManager();
final GBDevice gbDevice = manager.getDeviceByAddress(address);
if (gbDevice == null) {
@@ -71,6 +72,15 @@ public class BluetoothConnectReceiver extends BroadcastReceiver {
return;
}
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
LOG.warn("Bluetooth adapter not found - ignoring observed device");
return;
} else if (!adapter.isEnabled()) {
LOG.warn("Bluetooth adapter is disabled - ignoring observed device");
return;
}
final GBDevice.State state = gbDevice.getState();
if (state == WAITING_FOR_RECONNECT || state == WAITING_FOR_SCAN) {
LOG.debug("re-connecting to observed device due to state {}", state);