multi-device: added reaction to ACL_CONNECTED

This commit is contained in:
Daniel Dakhno 2021-12-26 17:13:14 +01:00
parent d09dbe2fa7
commit b4fa3bf21b

View File

@ -20,6 +20,7 @@ import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -27,6 +28,7 @@ import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
public class BluetoothConnectReceiver extends BroadcastReceiver {
@ -46,16 +48,20 @@ public class BluetoothConnectReceiver extends BroadcastReceiver {
}
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
LOG.info("connection attempt detected from or to " + device.getAddress() + "(" + device.getName() + ")");
LOG.info("connection attempt detected from " + device.getAddress() + "(" + device.getName() + ")");
try {
GBDevice gbDevice = service.getDeviceByAddress(device.getAddress());
if (gbDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT) {
LOG.info("Will re-connect to " + gbDevice.getAddress() + "(" + gbDevice.getName() + ")");
GBApplication.deviceService().connect(gbDevice);
}
} catch (DeviceCommunicationService.DeviceNotFoundException e) {
// ACL_CONNECTED from wrong device
GBDevice gbDevice = service.getDeviceByAddressOrNull(device.getAddress());
if(gbDevice == null){
LOG.info("connected device {} unknown", device.getAddress());
return;
}
SharedPreferences deviceSpecificPreferences = GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress());
boolean reactToConnection = deviceSpecificPreferences.getBoolean(GBPrefs.DEVICE_CONNECT_BACK, false);
reactToConnection |= gbDevice.getState() == GBDevice.State.WAITING_FOR_RECONNECT;
if(!reactToConnection){
return;
}
LOG.info("Will re-connect to " + gbDevice.getAddress() + "(" + gbDevice.getName() + ")");
GBApplication.deviceService().connect(gbDevice);
}
}