Import blood oxygen from the health platforms

This commit is contained in:
Roland Geider
2026-08-07 16:24:18 +02:00
parent 3dc9789500
commit 3855dbc423
10 changed files with 50 additions and 3 deletions

View File

@@ -19,6 +19,7 @@
<uses-permission android:name="android.permission.health.READ_BLOOD_PRESSURE"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_RESTING_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_OXYGEN_SATURATION"/>
<uses-permission android:name="android.permission.health.READ_SLEEP"/>
<uses-permission android:name="android.permission.health.READ_STEPS"/>
<uses-permission android:name="android.permission.health.READ_DISTANCE"/>

View File

@@ -88,7 +88,7 @@
<key>NSCameraUsageDescription</key>
<string>Workout photos</string>
<key>NSHealthShareUsageDescription</key>
<string>wger reads your health data to automatically import body measurements such as weight, body fat, blood pressure and heart rate</string>
<string>wger reads your health data to automatically import body measurements such as weight, body fat, blood pressure, heart rate and blood oxygen</string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>

View File

@@ -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<HealthMetric> 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

View File

@@ -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,

View File

@@ -1143,6 +1143,7 @@
"metricBloodPressureDiastolic": "Diastolisch",
"metricHeartRate": "Herzfrequenz",
"metricRestingHeartRate": "Ruhepuls",
"metricBloodOxygen": "Sauerstoffsättigung",
"metricSteps": "Schritte",
"metricDistance": "Distanz",
"metricEnergy": "Energie",

View File

@@ -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"

View File

@@ -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",

View File

@@ -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",

View File

@@ -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',

View File

@@ -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);