Sensor: add temperature.

Fixes #2241.
This commit is contained in:
Dennis Guse
2026-01-04 09:56:44 +01:00
parent 738201c1c2
commit 54844b9a9a
36 changed files with 435 additions and 44 deletions
@@ -12,10 +12,10 @@ import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.sensors.sensorData.SensorHandlerInterface;
public class BluetoothHandlerBarometricPressure implements SensorHandlerInterface {
private static final UUID ENVIRONMENTAL_SENSING_SERVICE = new UUID(0x181A00001000L, 0x800000805f9b34fbL);
static final UUID ENVIRONMENTAL_SENSING_SERVICE = new UUID(0x181A00001000L, 0x800000805f9b34fbL);
public static final ServiceMeasurementUUID BAROMETRIC_PRESSURE = new ServiceMeasurementUUID(
ENVIRONMENTAL_SENSING_SERVICE,
new UUID(0x2A6D00001000L, 0x800000805f9b34fbL)
new UUID(0x2A6D00001000L, 0x800000805f9b34fbL) //TODO extract leastSigBits
);
@Override
@@ -37,9 +37,7 @@ public class BluetoothHandlerBarometricPressure implements SensorHandlerInterfac
}
/**
* Decoding:
* org.bluetooth.service.environmental_sensing.xml
* org.bluetooth.characteristic.pressure.xml
* Decoding: org.bluetooth.characteristic.pressure.xml
*/
public static AtmosphericPressure parseEnvironmentalSensing(BluetoothGattCharacteristic characteristic) {
byte[] raw = characteristic.getValue();
@@ -0,0 +1,53 @@
package de.dennisguse.opentracks.sensors;
import static de.dennisguse.opentracks.sensors.BluetoothHandlerBarometricPressure.ENVIRONMENTAL_SENSING_SERVICE;
import android.bluetooth.BluetoothGattCharacteristic;
import java.util.List;
import java.util.UUID;
import de.dennisguse.opentracks.data.models.Temperature;
import de.dennisguse.opentracks.sensors.sensorData.Aggregator;
import de.dennisguse.opentracks.sensors.sensorData.AggregatorTemperature;
import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.sensors.sensorData.SensorHandlerInterface;
public class BluetoothHandlerTemperature implements SensorHandlerInterface {
public static final ServiceMeasurementUUID TEMPERATURE = new ServiceMeasurementUUID(
ENVIRONMENTAL_SENSING_SERVICE,
new UUID(0x2A6E00001000L, 0x800000805f9b34fbL)
);
@Override
public List<ServiceMeasurementUUID> getServices() {
return List.of(TEMPERATURE);
}
@Override
public Aggregator<?, ?> createEmptySensorData(String address, String name) {
return new AggregatorTemperature(address, name);
}
@Override
public void handlePayload(SensorManager.SensorDataChangedObserver observer, ServiceMeasurementUUID serviceMeasurementUUID, String sensorName, String address, BluetoothGattCharacteristic characteristic) {
Temperature value = parseEnvironmentalSensing(characteristic);
if (value == null) return;
observer.onChange(new Raw<>(observer.getNow(), value));
}
/**
* Decoding: org.bluetooth.characteristic.temperature.xml
*/
public static Temperature parseEnvironmentalSensing(BluetoothGattCharacteristic characteristic) {
byte[] raw = characteristic.getValue();
if (raw.length < 2) {
return null;
}
Integer pressure = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, 0);
return Temperature.ofCentiCelsius(pressure);
}
}
@@ -24,8 +24,6 @@ import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.time.Duration;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.settings.PreferencesUtils;
@@ -40,13 +38,12 @@ public class BluetoothRemoteSensorManager implements SensorConnector, SharedPref
private static final String TAG = BluetoothRemoteSensorManager.class.getSimpleName();
public static final Duration MAX_SENSOR_DATE_SET_AGE = Duration.ofSeconds(5);
private final Context context;
private final Handler handler;
private boolean started = false;
private final BluetoothConnectionManager heartRate;
private final BluetoothConnectionManager temperature;
private final BluetoothConnectionManager cyclingCadence;
private final BluetoothConnectionManager cyclingSpeed;
private final BluetoothConnectionManager cyclingPower;
@@ -58,6 +55,7 @@ public class BluetoothRemoteSensorManager implements SensorConnector, SharedPref
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(context);
this.heartRate = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerHeartRate());
this.temperature = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerTemperature());
this.cyclingCadence = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerCyclingCadence());
this.cyclingSpeed = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerCyclingDistanceSpeed());
this.cyclingPower = new BluetoothConnectionManager(bluetoothAdapter, observer, new BluetoothHandlerCyclingPower());
@@ -75,6 +73,7 @@ public class BluetoothRemoteSensorManager implements SensorConnector, SharedPref
@Override
public synchronized void stop(Context context) {
heartRate.disconnect();
temperature.disconnect();
cyclingCadence.disconnect();
cyclingSpeed.disconnect();
cyclingPower.disconnect();
@@ -96,6 +95,11 @@ public class BluetoothRemoteSensorManager implements SensorConnector, SharedPref
connect(heartRate, address);
}
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_temperature_key, key)) {
String address = PreferencesUtils.getBluetoothTemperatureSensorAddress();
connect(temperature, address);
}
if (PreferencesUtils.isKey(R.string.settings_sensor_bluetooth_cycling_cadence_key, key)) {
String address = PreferencesUtils.getBluetoothCyclingCadenceSensorAddress();
connect(cyclingCadence, address);
@@ -2,12 +2,13 @@ package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.NonNull;
import java.time.Duration;
import java.time.Instant;
import de.dennisguse.opentracks.sensors.BluetoothRemoteSensorManager;
public abstract class Aggregator<Input, Output> {
private static final Duration MAX_SENSOR_DATE_SET_AGE = Duration.ofSeconds(5);
protected Raw<Input> previous;
protected Output aggregatedValue;
@@ -43,6 +44,7 @@ public abstract class Aggregator<Input, Output> {
return aggregatedValue != null;
}
@Deprecated //TODO Remove: for sensor there is no default value. Make getAggregatedValue() @Nullable
@NonNull
protected abstract Output getNoneValue();
@@ -84,10 +86,14 @@ public abstract class Aggregator<Input, Output> {
return now
.isAfter(
previous.time()
.plus(BluetoothRemoteSensorManager.MAX_SENSOR_DATE_SET_AGE)
.plus(getMaxSensorAge())
);
}
protected Duration getMaxSensorAge() {
return MAX_SENSOR_DATE_SET_AGE;
}
@NonNull
@Override
public String toString() {
@@ -0,0 +1,34 @@
package de.dennisguse.opentracks.sensors.sensorData;
import androidx.annotation.NonNull;
import java.time.Duration;
import de.dennisguse.opentracks.data.models.Temperature;
public class AggregatorTemperature extends Aggregator<Temperature, Temperature> {
public AggregatorTemperature(String sensorAddress, String sensorName) {
super(sensorAddress, sensorName);
}
protected void computeValue(Raw<Temperature> current) {
this.aggregatedValue = current.value();
}
@NonNull
protected Temperature getNoneValue() {
return Temperature.ofCentiCelsius(0);
}
protected void resetImmediate() {
aggregatedValue = getNoneValue();
}
public void resetAggregated() {};
@Override
protected Duration getMaxSensorAge() {
return Duration.ofMinutes(1);
}
}
@@ -14,6 +14,7 @@ import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.HeartRate;
import de.dennisguse.opentracks.data.models.Position;
import de.dennisguse.opentracks.data.models.Speed;
import de.dennisguse.opentracks.data.models.Temperature;
import de.dennisguse.opentracks.sensors.BluetoothHandlerCyclingCadence;
import de.dennisguse.opentracks.sensors.BluetoothHandlerCyclingDistanceSpeed;
import de.dennisguse.opentracks.sensors.BluetoothHandlerCyclingPower;
@@ -27,6 +28,9 @@ public class SensorDataAggregator {
@VisibleForTesting
public AggregatorHeartRate heartRate;
@VisibleForTesting
public AggregatorTemperature temperature;
@VisibleForTesting
public AggregatorCyclingCadence cyclingCadence;
@@ -97,6 +101,7 @@ public class SensorDataAggregator {
set(data, data);
}
//TODO Refactor generic "fun" here: go explicit.
public void update(@NonNull Raw<?> data) {
Object value = data.value();
@@ -105,6 +110,11 @@ public class SensorDataAggregator {
return;
}
if (value instanceof Temperature) {
this.temperature.add((Raw<Temperature>) data);
return;
}
if (value instanceof BluetoothHandlerCyclingCadence.CrankData) {
this.cyclingCadence.add((Raw<BluetoothHandlerCyclingCadence.CrankData>) data);
return;
@@ -142,6 +152,7 @@ public class SensorDataAggregator {
public void clear() {
Log.i(TAG, "Removing all aggregators");
this.heartRate = null;
this.temperature = null;
this.cyclingCadence = null;
this.cyclingDistanceSpeed = null;
this.cyclingPower = null;
@@ -156,6 +167,7 @@ public class SensorDataAggregator {
getSpeed(now),
getDistance(now),
getHeartRate(now),
temperature != null && temperature.hasReceivedData() ? temperature.getAggregatedValueWithSensorName(now) : null,
getCadence(now),
cyclingPower != null && cyclingPower.hasReceivedData() ? cyclingPower.getAggregatedValueWithSensorName(now) : null,
barometer != null && barometer.hasReceivedData() ? barometer.getAggregatedValueWithSensorName(now) : null
@@ -166,6 +178,7 @@ public class SensorDataAggregator {
Log.i(TAG, "Resetting data");
if (heartRate != null) heartRate.resetAggregated();
if (temperature != null) temperature.resetAggregated();
if (cyclingCadence != null) cyclingCadence.resetAggregated();
if (cyclingDistanceSpeed != null) cyclingDistanceSpeed.resetAggregated();
if (cyclingPower != null) cyclingPower.resetAggregated();
@@ -182,6 +195,10 @@ public class SensorDataAggregator {
heartRate = (AggregatorHeartRate) sensorData;
return;
}
if (type instanceof AggregatorTemperature) {
temperature = (AggregatorTemperature) sensorData;
return;
}
if (type instanceof AggregatorCyclingCadence) {
cyclingCadence = (AggregatorCyclingCadence) sensorData;
return;
@@ -10,6 +10,7 @@ import de.dennisguse.opentracks.data.models.HeartRate;
import de.dennisguse.opentracks.data.models.Position;
import de.dennisguse.opentracks.data.models.Power;
import de.dennisguse.opentracks.data.models.Speed;
import de.dennisguse.opentracks.data.models.Temperature;
public record SensorDataSet(
@NonNull
@@ -21,6 +22,7 @@ public record SensorDataSet(
@Nullable
SensorData<HeartRate> heartRate,
@Nullable
SensorData<Temperature> temperature,
SensorData<Cadence> cadence,
@Nullable
SensorData<Power> power,