log more failure details in WriteAction when running on Tiramisu or higher

This commit is contained in:
Thomas Kuehne
2025-05-01 13:35:39 +00:00
committed by José Rebelo
parent 284bbe129d
commit 742d5d96ab
3 changed files with 29 additions and 3 deletions
@@ -430,6 +430,10 @@ public class GBApplication extends Application {
return VERSION.SDK_INT >= 31; // Build.VERSION_CODES.S, but our target SDK is lower
}
public static boolean isRunningTiramisuOrLater() {
return VERSION.SDK_INT >= 33; // Build.VERSION_CODES.TIRAMISU
}
public static boolean isRunningPieOrLater() {
return VERSION.SDK_INT >= Build.VERSION_CODES.P;
}
@@ -26,6 +26,7 @@ import android.os.Build;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction;
import static nodomain.freeyourgadget.gadgetbridge.service.btle.GattDescriptor.UUID_DESCRIPTOR_GATT_CLIENT_CHARACTERISTIC_CONFIGURATION;
@@ -62,7 +63,7 @@ public class NotifyAction extends BtLEAction {
final String charUuid = descriptor.getCharacteristic().getUuid().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (GBApplication.isRunningTiramisuOrLater()) {
// use API introduced in SDK level 33 to catch exceptions and more specific errors
try {
final int result = gatt.writeDescriptor(descriptor, value);
@@ -1,5 +1,5 @@
/* Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, Daniele
Gobbetti, José Rebelo, Uwe Hermann
Gobbetti, José Rebelo, Uwe Hermann, Thomas Kuehne
This file is part of Gadgetbridge.
@@ -17,13 +17,16 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.service.btle.actions;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothStatusCodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.Logging;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction;
@@ -50,13 +53,31 @@ public class WriteAction extends BtLEAction {
if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0 || ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0)) {
return writeValue(gatt, characteristic, value);
}
LOG.error("WriteAction for non-writeable characteristic {}", characteristic.getUuid());
return false;
}
@SuppressLint("MissingPermission")
protected boolean writeValue(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, byte[] value) {
if (LOG.isDebugEnabled()) {
LOG.debug("writing to characteristic: " + characteristic.getUuid() + ": " + Logging.formatBytes(value));
LOG.debug("writing to characteristic: {} : {}", characteristic.getUuid(), Logging.formatBytes(value));
}
if (GBApplication.isRunningTiramisuOrLater()) {
// use API introduced in SDK level 33 to catch exceptions and more specific errors
try {
final int status = gatt.writeCharacteristic(characteristic, value, characteristic.getWriteType());
if (status == BluetoothStatusCodes.SUCCESS) {
return true;
}
LOG.error("writing to characteristic {} failed: BluetoothStatusCode={}", characteristic.getUuid(), status);
} catch (Exception e) {
LOG.error("writing to characteristic {} failed with ", characteristic.getUuid(), e);
}
return false;
}
if (characteristic.setValue(value)) {
return gatt.writeCharacteristic(characteristic);
}