From 3855dbc4238c2e8f7d5f37f036a8b853fa14a63e Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 7 Aug 2026 16:24:18 +0200 Subject: [PATCH] Import blood oxygen from the health platforms --- android/app/src/main/AndroidManifest.xml | 1 + ios/Runner/Info.plist | 2 +- lib/features/health/models/health_metric.dart | 16 ++++++++++++++++ .../models/measurement_category.dart | 9 ++++++++- lib/l10n/app_de.arb | 1 + lib/l10n/app_en.arb | 4 ++++ lib/l10n/app_es.arb | 1 + lib/l10n/app_fr.arb | 1 + test/features/health/health_manifest_test.dart | 1 + .../health/models/health_metric_test.dart | 17 ++++++++++++++++- 10 files changed, 50 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index edf2e886d..d273b9da2 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -19,6 +19,7 @@ + diff --git a/ios/Runner/Info.plist b/ios/Runner/Info.plist index 0e29c7cc2..b0855683b 100644 --- a/ios/Runner/Info.plist +++ b/ios/Runner/Info.plist @@ -88,7 +88,7 @@ NSCameraUsageDescription Workout photos NSHealthShareUsageDescription - wger reads your health data to automatically import body measurements such as weight, body fat, blood pressure and heart rate + wger reads your health data to automatically import body measurements such as weight, body fat, blood pressure, heart rate and blood oxygen UIApplicationSceneManifest UIApplicationSupportsMultipleScenes diff --git a/lib/features/health/models/health_metric.dart b/lib/features/health/models/health_metric.dart index 5e53793f6..b7669247e 100644 --- a/lib/features/health/models/health_metric.dart +++ b/lib/features/health/models/health_metric.dart @@ -158,6 +158,11 @@ double _bodyFatToPercent(double raw) => raw <= 1.5 ? raw * 100 : raw; /// value that already arrived in centimeters. double _heightToCm(double raw) => raw < 3 ? raw * 100 : raw; +/// Blood oxygen has the same fraction-vs-percent split as body fat, with even +/// more room between the two: a saturation worth recording is above 70, a +/// fraction never reaches 1.01. +double _saturationToPercent(double raw) => raw <= 1.5 ? raw * 100 : raw; + double _identity(double raw) => raw; /// Both platforms report a distance in meters, the category stores kilometers. @@ -219,6 +224,17 @@ const List healthMetrics = [ toCategoryValue: _identity, enabled: true, ), + HealthMetric( + // A single saturation says little on its own, and a wearable measures it + // through the night, so the day's mean is what is stored. The window is + // the small one until the log says how densely it actually arrives. + metricType: MetricType.bloodOxygen, + dataType: HealthDataType.BLOOD_OXYGEN, + toCategoryValue: _saturationToPercent, + dailyAggregation: DailyAggregation.average, + enabled: true, + readWindow: highVolumeReadWindow, + ), HealthMetric.group( // SLEEP_ASLEEP is not the whole night, it is the coarsest stage: the // plugin filters it down to HealthKit's asleepUnspecified and to Health diff --git a/lib/features/measurements/models/measurement_category.dart b/lib/features/measurements/models/measurement_category.dart index bd59139c1..1886e0d0f 100644 --- a/lib/features/measurements/models/measurement_category.dart +++ b/lib/features/measurements/models/measurement_category.dart @@ -185,6 +185,7 @@ enum MetricType { bloodPressureDiastolic('blood_pressure_diastolic'), heartRate('heart_rate'), restingHeartRate('resting_heart_rate'), + bloodOxygen('blood_oxygen'), steps('steps'), distance('distance'), energy('energy'), @@ -311,6 +312,7 @@ enum MetricType { MetricType.bloodPressureDiastolic => 'Diastolic', MetricType.heartRate => 'Heart rate', MetricType.restingHeartRate => 'Resting heart rate', + MetricType.bloodOxygen => 'Blood oxygen', MetricType.steps => 'Steps', MetricType.distance => 'Distance', MetricType.energy => 'Energy', @@ -331,7 +333,7 @@ enum MetricType { String get defaultUnit => switch (this) { MetricType.custom => '', MetricType.bodyWeight => 'kg', - MetricType.bodyFat => '%', + MetricType.bodyFat || MetricType.bloodOxygen => '%', MetricType.height => 'cm', MetricType.bloodPressure || MetricType.bloodPressureSystolic || @@ -383,6 +385,9 @@ enum MetricType { MetricType.bloodPressureDiastolic => const MetricLimits(30, 150, 50, 110), MetricType.heartRate => const MetricLimits(30, 250, 40, 200), MetricType.restingHeartRate => const MetricLimits(30, 120, 40, 100), + // A saturation cannot exceed 100 %, and the floor is deliberately far + // below what a pulse oximeter still displays + MetricType.bloodOxygen => const MetricLimits(50, 100, 90, 100), // The cumulative types hold a whole day, and a rest day really is 0 steps MetricType.steps => const MetricLimits(0, 100000, 0, 30000), MetricType.distance => const MetricLimits(0, 500, 0, 30), @@ -419,6 +424,7 @@ enum MetricType { MetricType.bloodPressureSystolic || MetricType.bloodPressureDiastolic => 5, MetricType.heartRate => 2, MetricType.restingHeartRate => 1, + MetricType.bloodOxygen => 1, MetricType.steps => 1000, MetricType.distance => 1, MetricType.energy => 100, @@ -445,6 +451,7 @@ extension MeasurementMetricTypeL10n on MetricType { MetricType.bloodPressureDiastolic => l10n.metricBloodPressureDiastolic, MetricType.heartRate => l10n.metricHeartRate, MetricType.restingHeartRate => l10n.metricRestingHeartRate, + MetricType.bloodOxygen => l10n.metricBloodOxygen, MetricType.steps => l10n.metricSteps, MetricType.distance => l10n.metricDistance, MetricType.energy => l10n.metricEnergy, diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index 2aec3502a..844d600a3 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -1143,6 +1143,7 @@ "metricBloodPressureDiastolic": "Diastolisch", "metricHeartRate": "Herzfrequenz", "metricRestingHeartRate": "Ruhepuls", + "metricBloodOxygen": "Sauerstoffsättigung", "metricSteps": "Schritte", "metricDistance": "Distanz", "metricEnergy": "Energie", diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 5ba9923a5..d6003ad2e 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -758,6 +758,10 @@ "@metricRestingHeartRate": { "description": "Label for resting heart rate measurement" }, + "metricBloodOxygen": "Blood oxygen", + "@metricBloodOxygen": { + "description": "Label for blood oxygen saturation measurement" + }, "metricSteps": "Steps", "@metricSteps": { "description": "Label for step count measurement" diff --git a/lib/l10n/app_es.arb b/lib/l10n/app_es.arb index 7b73a5004..046cf3a8e 100644 --- a/lib/l10n/app_es.arb +++ b/lib/l10n/app_es.arb @@ -1182,6 +1182,7 @@ "metricBloodPressureDiastolic": "Diastólica", "metricHeartRate": "Frecuencia cardíaca", "metricRestingHeartRate": "Frecuencia cardíaca en reposo", + "metricBloodOxygen": "Saturación de oxígeno", "metricSteps": "Pasos", "metricDistance": "Distancia", "metricEnergy": "Energía", diff --git a/lib/l10n/app_fr.arb b/lib/l10n/app_fr.arb index 40fd7db6f..c0cb4dee7 100644 --- a/lib/l10n/app_fr.arb +++ b/lib/l10n/app_fr.arb @@ -1153,6 +1153,7 @@ "metricBloodPressureDiastolic": "Diastolique", "metricHeartRate": "Fréquence cardiaque", "metricRestingHeartRate": "Fréquence cardiaque au repos", + "metricBloodOxygen": "Saturation en oxygène", "metricSteps": "Pas", "metricDistance": "Distance", "metricEnergy": "Énergie", diff --git a/test/features/health/health_manifest_test.dart b/test/features/health/health_manifest_test.dart index 74760c6f8..ec4d85aae 100644 --- a/test/features/health/health_manifest_test.dart +++ b/test/features/health/health_manifest_test.dart @@ -34,6 +34,7 @@ const _readPermissions = { HealthDataType.BLOOD_PRESSURE_DIASTOLIC: 'android.permission.health.READ_BLOOD_PRESSURE', HealthDataType.HEART_RATE: 'android.permission.health.READ_HEART_RATE', HealthDataType.RESTING_HEART_RATE: 'android.permission.health.READ_RESTING_HEART_RATE', + HealthDataType.BLOOD_OXYGEN: 'android.permission.health.READ_OXYGEN_SATURATION', // Every sleep type is a SleepSessionRecord, so they share one permission HealthDataType.SLEEP_ASLEEP: 'android.permission.health.READ_SLEEP', HealthDataType.SLEEP_LIGHT: 'android.permission.health.READ_SLEEP', diff --git a/test/features/health/models/health_metric_test.dart b/test/features/health/models/health_metric_test.dart index 6afe86872..417577fe8 100644 --- a/test/features/health/models/health_metric_test.dart +++ b/test/features/health/models/health_metric_test.dart @@ -35,17 +35,20 @@ void main() { MetricType.bloodPressure, MetricType.heartRate, MetricType.restingHeartRate, + MetricType.bloodOxygen, MetricType.sleep, MetricType.steps, MetricType.distance, MetricType.energy, ]), ); - expect(enabledHealthMetrics.length, 10); + expect(enabledHealthMetrics.length, 11); }); test('daily aggregation is set per metric', () { expect(_metric(MetricType.heartRate).dailyAggregation, DailyAggregation.average); + // Measured through the night, so a single saturation says little + expect(_metric(MetricType.bloodOxygen).dailyAggregation, DailyAggregation.average); // A night arrives as segments from possibly several sources, so the // time they cover is counted once rather than added up expect(_metric(MetricType.sleep).dailyAggregation, DailyAggregation.mergedDuration); @@ -113,6 +116,18 @@ void main() { }); }); + group('Blood oxygen conversion', () { + final bloodOxygen = _metric(MetricType.bloodOxygen); + + test('iOS fraction is scaled to a percentage', () { + expect(bloodOxygen.toCategoryValue(0.95), closeTo(95, 0.001)); + }); + + test('Health Connect percentage is kept as-is', () { + expect(bloodOxygen.toCategoryValue(95), closeTo(95, 0.001)); + }); + }); + group('Height conversion', () { final height = _metric(MetricType.height);