Bluetooth: upgrade minSDK=26 for simplification (race condition prevention).

Part of #1424.
This commit is contained in:
Dennis Guse
2023-11-05 13:02:48 +01:00
parent 062457f1aa
commit 17487961ad
2 changed files with 6 additions and 14 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ android {
buildConfigField "String", "VERSION_NAME_FULL", "\"${getVersionName()}\"" buildConfigField "String", "VERSION_NAME_FULL", "\"${getVersionName()}\""
minSdk 24 minSdk 26
targetSdk 34 targetSdk 34
testInstrumentationRunner "de.dennisguse.opentracks.TestRunner" testInstrumentationRunner "de.dennisguse.opentracks.TestRunner"
@@ -16,6 +16,7 @@
package de.dennisguse.opentracks.sensors; package de.dennisguse.opentracks.sensors;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCallback;
@@ -24,7 +25,6 @@ import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
@@ -40,6 +40,7 @@ import de.dennisguse.opentracks.sensors.sensorData.SensorData;
* Manages connection to a Bluetooth LE sensor and subscribes for onChange-notifications. * Manages connection to a Bluetooth LE sensor and subscribes for onChange-notifications.
* Also parses the transferred data into {@link SensorDataObserver}. * Also parses the transferred data into {@link SensorDataObserver}.
*/ */
@SuppressLint("MissingPermission")
public abstract class AbstractBluetoothConnectionManager<DataType> { public abstract class AbstractBluetoothConnectionManager<DataType> {
private static final String TAG = AbstractBluetoothConnectionManager.class.getSimpleName(); private static final String TAG = AbstractBluetoothConnectionManager.class.getSimpleName();
@@ -122,12 +123,7 @@ public abstract class AbstractBluetoothConnectionManager<DataType> {
SensorData<DataType> sensorData = parsePayload(serviceMeasurementUUID.get(), gatt.getDevice().getName(), gatt.getDevice().getAddress(), characteristic); SensorData<DataType> sensorData = parsePayload(serviceMeasurementUUID.get(), gatt.getDevice().getName(), gatt.getDevice().getAddress(), characteristic);
if (sensorData != null) { if (sensorData != null) {
Log.d(TAG, "Decoded data from " + gatt.getDevice().getAddress() + ": " + sensorData); Log.d(TAG, "Decoded data from " + gatt.getDevice().getAddress() + ": " + sensorData);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { observer.onChanged(sensorData);
observer.onChanged(sensorData);
} else {
//TODO This might lead to NPEs in case of race conditions due to shutdown.
observer.getHandler().post(() -> observer.onChanged(sensorData));
}
} }
} }
}; };
@@ -150,11 +146,8 @@ public abstract class AbstractBluetoothConnectionManager<DataType> {
Log.d(TAG, "Connecting to: " + device); Log.d(TAG, "Connecting to: " + device);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { bluetoothGatt = device.connectGatt(context, false, connectCallback, BluetoothDevice.TRANSPORT_AUTO, 0, this.observer.getHandler());
bluetoothGatt = device.connectGatt(context, false, connectCallback, BluetoothDevice.TRANSPORT_AUTO, 0, this.observer.getHandler());
} else {
bluetoothGatt = device.connectGatt(context, false, connectCallback);
}
SensorData<?> sensorData = createEmptySensorData(bluetoothGatt.getDevice().getAddress()); SensorData<?> sensorData = createEmptySensorData(bluetoothGatt.getDevice().getAddress());
observer.onChanged(sensorData); observer.onChanged(sensorData);
} }
@@ -163,7 +156,6 @@ public abstract class AbstractBluetoothConnectionManager<DataType> {
observer.onDisconnecting(createEmptySensorData(bluetoothGatt.getDevice().getAddress())); observer.onDisconnecting(createEmptySensorData(bluetoothGatt.getDevice().getAddress()));
} }
synchronized void disconnect() { synchronized void disconnect() {
if (bluetoothGatt == null) { if (bluetoothGatt == null) {
Log.w(TAG, "Cannot disconnect if not connected."); Log.w(TAG, "Cannot disconnect if not connected.");