Reduce toast spam when bluetooth is disabled (#1093)

It's likely that it still triggers in the background under some
situations, but at least this reduces the number of toasts from 2 to 1.
This commit is contained in:
José Rebelo
2025-12-24 09:22:22 +00:00
parent 53c9355e98
commit 83118c078e
3 changed files with 29 additions and 29 deletions
@@ -27,6 +27,7 @@ import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -751,7 +752,15 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
throw e;
}
} else {
GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR);
// no device found, check transport availability and warn
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
GB.toast(this, getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN);
} else if (!adapter.isEnabled()) {
GB.toast(this, getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN);
} else {
GB.toast(this, getString(R.string.cannot_connect, "Can't create device support"), Toast.LENGTH_SHORT, GB.ERROR);
}
}
} catch (Exception e) {
LOG.warn("exception in connectToDevice for {}", deviceAddress, e);
@@ -27,7 +27,6 @@ package nodomain.freeyourgadget.gadgetbridge.service;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.widget.Toast;
import androidx.annotation.Nullable;
@@ -39,7 +38,6 @@ import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleSupport;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import java.lang.reflect.Constructor;
import java.util.EnumSet;
@@ -72,13 +70,7 @@ public class DeviceSupportFactory {
deviceSupport = createClassNameDeviceSupport(device);
}
if (deviceSupport != null) {
return deviceSupport;
}
// no device found, check transport availability and warn
checkBtAvailability();
return null;
return deviceSupport;
}
@Nullable
@@ -92,20 +84,13 @@ public class DeviceSupportFactory {
support.setContext(device, null, mContext);
return support;
} catch (ClassNotFoundException e) {
LOG.warn("Unable to find DeviceSupport class {} for {}", className, device.getAddress());
return null; // not a class, or not known at least
} catch (Exception e) {
throw new GBException("Error creating DeviceSupport instance for " + className, e);
}
}
private void checkBtAvailability() {
if (mBtAdapter == null) {
GB.toast(mContext.getString(R.string.bluetooth_is_not_supported_), Toast.LENGTH_SHORT, GB.WARN);
} else if (!mBtAdapter.isEnabled()) {
GB.toast(mContext.getString(R.string.bluetooth_is_disabled_), Toast.LENGTH_SHORT, GB.WARN);
}
}
private ServiceDeviceSupport createServiceDeviceSupport(GBDevice device) throws GBException {
final DeviceCoordinator coordinator = device.getDeviceCoordinator();
final Class<?> supportClass = coordinator.getDeviceSupportClass(device);
@@ -114,23 +99,29 @@ public class DeviceSupportFactory {
final DeviceSupport supportInstance = (DeviceSupport) supportClass.newInstance();
return new ServiceDeviceSupport(supportInstance, coordinator.getInitialFlags());
} catch (ReflectiveOperationException e) {
LOG.error("error calling DeviceSupport constructor with zero arguments");
LOG.error("error calling DeviceSupport constructor for {} with zero arguments", device.getAddress());
throw new GBException(e);
}
}
@Nullable
private DeviceSupport createBTDeviceSupport(final GBDevice gbDevice) throws GBException {
if (mBtAdapter != null && mBtAdapter.isEnabled()) {
try {
final DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice);
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
return deviceSupport;
} catch (final Exception e) {
throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), e);
}
if (mBtAdapter == null) {
LOG.warn("Unable to create bt device support for {} - no bt adapter", gbDevice.getAddress());
return null;
}
if (!mBtAdapter.isEnabled()) {
LOG.warn("Unable to create bt device support for {} - bt is disabled", gbDevice.getAddress());
return null;
}
try {
final DeviceSupport deviceSupport = createServiceDeviceSupport(gbDevice);
deviceSupport.setContext(gbDevice, mBtAdapter, mContext);
return deviceSupport;
} catch (final Exception e) {
throw new GBException(mContext.getString(R.string.cannot_connect_bt_address_invalid_), e);
}
return null;
}
private DeviceSupport createTCPDeviceSupport(GBDevice gbDevice) throws GBException {
@@ -111,7 +111,7 @@ public class AutoConnectIntervalReceiver extends BroadcastReceiver {
am.setAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
Calendar.getInstance().
getTimeInMillis() + delay * 1000,
getTimeInMillis() + delay * 1000L,
pendingIntent
);
mScheduled = true;