ble: BluetoothDevice.getAlias replaced getAliasName in Android 11

This commit is contained in:
Thomas Kuehne
2025-05-17 14:52:14 +00:00
committed by José Rebelo
parent 9814ee74ac
commit a83026b180
2 changed files with 20 additions and 9 deletions
@@ -426,6 +426,10 @@ public class GBApplication extends Application {
return VERSION.SDK_INT >= Build.VERSION_CODES.Q;
}
public static boolean isRedVelvetCakeOrLater() {
return VERSION.SDK_INT >= Build.VERSION_CODES.R;
}
public static boolean isRunningTwelveOrLater() {
return VERSION.SDK_INT >= 31; // Build.VERSION_CODES.S, but our target SDK is lower
}
@@ -35,9 +35,7 @@ import java.util.UUID;
import androidx.annotation.NonNull;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
/**
* A device candidate is a Bluetooth device that is not yet managed by
@@ -176,13 +174,22 @@ public class GBDeviceCandidate implements Parcelable, Cloneable {
return;
}
try {
final Method method = device.getClass().getMethod("getAliasName");
deviceName = (String) method.invoke(device);
} catch (final NoSuchMethodException ignore) {
// ignored
} catch (final IllegalAccessException | InvocationTargetException ignore) {
LOG.warn("Could not get device alias for {}", device.getAddress());
if (GBApplication.isRedVelvetCakeOrLater()) {
try {
deviceName = device.getAlias();
} catch (final SecurityException e) {
// Should never happen
LOG.error("SecurityException on device.getAlias", e);
}
} else {
try {
final Method method = device.getClass().getMethod("getAliasName");
deviceName = (String) method.invoke(device);
} catch (final NoSuchMethodException ignore) {
// ignored
} catch (final IllegalAccessException | InvocationTargetException ignore) {
LOG.warn("Could not get device alias for {}", device.getAddress());
}
}
if (deviceName == null || deviceName.isEmpty()) {
try {