1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-02 03:16:07 +02:00

Automatically connect to all devices

Automatically connect to all devices instead of connecting only to the
last connected device.
This commit is contained in:
Davis Mosenkovs 2023-10-01 23:54:45 +03:00 committed by José Rebelo
parent 705361ae3a
commit 904a7807a8
2 changed files with 61 additions and 51 deletions

View File

@ -476,66 +476,76 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
break;
case ACTION_CONNECT:
start(); // ensure started
GBDevice gbDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
String btDeviceAddress = null;
if (gbDevice == null) {
if (prefs != null) { // may be null in test cases
btDeviceAddress = prefs.getString("last_device_address", null);
if (btDeviceAddress != null) {
gbDevice = DeviceHelper.getInstance().findAvailableDevice(btDeviceAddress, this);
}
}
List<GBDevice> gbDevs = null;
boolean fromExtra = false;
GBDevice extraDevice = intent.getParcelableExtra(GBDevice.EXTRA_DEVICE);
if (extraDevice != null) {
gbDevs = new ArrayList<>();
gbDevs.add(extraDevice);
fromExtra = true;
} else {
btDeviceAddress = gbDevice.getAddress();
gbDevs = GBApplication.app().getDeviceManager().getDevices();
}
if(gbDevice == null){
if(gbDevs == null || gbDevs.size() == 0) {
return START_NOT_STICKY;
}
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
if (prefs != null && prefs.getPreferences() != null) {
prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
autoReconnect = getGBPrefs().getAutoReconnect(gbDevice);
}
for(GBDevice gbDevice : gbDevs) {
String btDeviceAddress = gbDevice.getAddress();
DeviceStruct registeredStruct = getDeviceStructOrNull(gbDevice);
if(registeredStruct != null){
boolean deviceAlreadyConnected = isDeviceConnecting(registeredStruct.getDevice()) || isDeviceConnected(registeredStruct.getDevice());
if(deviceAlreadyConnected){
break;
}
try {
removeDeviceSupport(gbDevice);
} catch (DeviceNotFoundException e) {
e.printStackTrace();
}
}else{
registeredStruct = new DeviceStruct();
registeredStruct.setDevice(gbDevice);
registeredStruct.setCoordinator(gbDevice.getDeviceCoordinator());
deviceStructs.add(registeredStruct);
}
try {
DeviceSupport deviceSupport = mFactory.createDeviceSupport(gbDevice);
if (deviceSupport != null) {
setDeviceSupport(gbDevice, deviceSupport);
if (firstTime) {
deviceSupport.connectFirstTime();
} else {
deviceSupport.setAutoReconnect(autoReconnect);
deviceSupport.connect();
boolean autoReconnect = GBPrefs.AUTO_RECONNECT_DEFAULT;
if (prefs != null && prefs.getPreferences() != null) {
autoReconnect = getGBPrefs().getAutoReconnect(gbDevice);
if(!fromExtra && !autoReconnect) {
continue;
}
} else {
GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR);
prefs.getPreferences().edit().putString("last_device_address", btDeviceAddress).apply();
}
} catch (Exception e) {
GB.toast(this, getString(R.string.cannot_connect, e.getMessage()), Toast.LENGTH_SHORT, GB.ERROR, e);
}
for(DeviceStruct struct2 : deviceStructs){
struct2.getDevice().sendDeviceUpdateIntent(this);
if(!fromExtra && !autoReconnect) {
continue;
}
DeviceStruct registeredStruct = getDeviceStructOrNull(gbDevice);
if(registeredStruct != null){
boolean deviceAlreadyConnected = isDeviceConnecting(registeredStruct.getDevice()) || isDeviceConnected(registeredStruct.getDevice());
if(deviceAlreadyConnected){
break;
}
try {
removeDeviceSupport(gbDevice);
} catch (DeviceNotFoundException e) {
e.printStackTrace();
}
}else{
registeredStruct = new DeviceStruct();
registeredStruct.setDevice(gbDevice);
registeredStruct.setCoordinator(gbDevice.getDeviceCoordinator());
deviceStructs.add(registeredStruct);
}
try {
DeviceSupport deviceSupport = mFactory.createDeviceSupport(gbDevice);
if (deviceSupport != null) {
setDeviceSupport(gbDevice, deviceSupport);
if (firstTime) {
deviceSupport.connectFirstTime();
} else {
deviceSupport.setAutoReconnect(autoReconnect);
deviceSupport.connect();
}
} else {
GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR);
}
} catch (Exception e) {
GB.toast(this, getString(R.string.cannot_connect, e.getMessage()), Toast.LENGTH_SHORT, GB.ERROR, e);
}
for(DeviceStruct struct2 : deviceStructs){
struct2.getDevice().sendDeviceUpdateIntent(this);
}
}
break;
default:

View File

@ -203,7 +203,7 @@
<string name="pref_header_time">Time</string>
<string name="pref_header_workout">Workout</string>
<string name="pref_header_equalizer">Equalizer</string>
<string name="pref_title_general_autoconnectonbluetooth">Connect to Gadgetbridge device when Bluetooth is turned on</string>
<string name="pref_title_general_autoconnectonbluetooth">Connect to Gadgetbridge device(s) when Bluetooth is turned on</string>
<string name="pref_title_general_autostartonboot">Start automatically</string>
<string name="pref_title_general_autoreconnect">Reconnect automatically</string>
<string name="pref_title_mb_intents">Broadcast Media Button Intents Directly</string>