Usage: Sync health data manually
Sync Health Connect data by yourself.
The automatic implementation is faster and easier to integrate and is what most developers need, try to use it before attempting to create a custom sync process.
Sync health data manually
There are 2 types of health data Summaries and Events.
| Health Data | Timezone | Oldest date of retriefinal | Latest date of retriefinal | Class |
|---|---|---|---|---|
| Summary | UTC | 29 days | Yesterday | HCRookSyncManager |
| Event | UTC | 29 days | Today | HCRookSyncManager |
Sync summaries
Use sync(bool enableLogs) to sync the last 29 days of SLEEP_SUMMARY, PHYSICAL_SUMMARY and BODY_SUMMARY (not
including today).
If the app goes to the background the synchronization will fail, use scheduleYesterdaySync. instead if you want the synchronization to continue until it finishes (scheduleYesterdaySync will not only sync summaries, but also events).
void syncSummariesHistoric() async {
try {
await HCRookSyncManager.sync(enableLogs: isDebug);
// Historic summaries sync started
} catch (error) {
// Handle error
}
}
Use sync(DateTime date) to sync SLEEP_SUMMARY, PHYSICAL_SUMMARY and BODY_SUMMARY for the provided date.
void syncSummaries() async {
try {
await HCRookSyncManager.sync(date: date);
// Summaries synced successfully
} catch (error) {
// Handle error
}
}
Use sync(DateTime date, HCSummarySyncType summary) to sync the summary of choice for the provided date.
void syncSingleSummary() async {
try {
await HCRookSyncManager.sync(date: date, summary: summarySyncType);
// Summary synced successfully
} catch (error) {
// Handle error
}
}
Sync events
Use syncEvents(DateTime date, HCEventSyncType event) to sync the event of choice for the provided date.
void syncSingleEvent() async {
try {
await HCRookSyncManager.syncEvents(date, eventSyncType);
// Event synced successfully
} catch (error) {
// Handle error
}
}
Sync current day events
Current day events allow to send small amounts of health data, but they also return the data that was sent, you can use this data to update your UI without waiting for the webhook event.
Steps events
Retrieve and upload current day steps count of Health Connect.
void syncStepsEvents() async {
try {
final syncStatusWithData = await HCRookSyncManager.getTodayStepsCount();
switch (syncStatusWithData) {
case Synced(data: final steps):
// Update the UI with steps count
break;
case RecordsNotFound():
// No steps events found
break;
}
} catch (error) {
// Handle error
}
}
Warning: This function contributes to the Health Connect rate limit, don't call it too frequently.
Calories events
Retrieve and upload current day calories count of Health Connect.
void syncCaloriesEvents() async {
try {
final syncStatusWithData = await HCRookSyncManager.getTodayCaloriesCount();
switch (syncStatusWithData) {
case Synced(data: final calories):
// Update the UI with calories count
break;
case RecordsNotFound():
// No calories events found
break;
}
} catch (error) {
// Handle error
}
}
Warning: This function contributes to the Health Connect rate limit, don't call it too frequently.
Sync and display
If you want to sync and get a copy of the summary/event that was uploaded you can call any of the following functions:
Sleep Summary
void example() async {
final date = DateTime.now();
try {
final sleepSummaries = await HCRookSyncManager.getSleepSummary(date);
if (sleepSummaries.isNotEmpty) {
// Update UI
} else {
// No records found
}
} catch (error) {
// Handle error
}
}
The returned SleepSummary has the following definition:
// Properties not supported by Health Connect or not processed by this SDK are commented out and will always be null.
// Commented properties are updated with the latest public version of the SDK.
class SleepSummary {
final DateTime dateTime;
final String sourceOfData;
final bool wasTheUserUnderPhysicalActivity;
final DateTime sleepStartDateTime;
final DateTime sleepEndDateTime;
final DateTime sleepDate;
final int? sleepDurationSeconds;
final int? timeInBedSeconds;
final int? lightSleepDurationSeconds;
final int? remSleepDurationSeconds;
final int? deepSleepDurationSeconds;
// final int? timeToFallAsleepSeconds;
final int? timeAwakeDuringSleepSeconds;
// final int? sleepQualityRating1To5Score;
// final int? sleepEfficiency1To100Score;
// final int? sleepGoalSeconds;
// final int? sleepContinuity1To5Score;
// final int? sleepContinuity1To5Rating;
final int? hrMaximumBpm;
final int? hrMinimumBpm;
final int? hrAvgBpm;
final int? hrRestingBpm;
// final int? hrBasalBpm;
final List<HrGranularData>? hrGranularData;
final double? hrvAvgRmssd;
// final double? hrvAvgSdnn;
// final List<HrvSdnnGranularData>? hrvSdnnGranularData;
final List<HrvRmssdGranularData>? hrvRmssdGranularData;
final TemperatureObject? temperatureMinimum;
final TemperatureObject? temperatureAvg;
final TemperatureObject? temperatureMaximum;
final List<TemperatureGranularData>? temperatureGranularData;
// final TemperatureObject? temperatureDelta;
final double? breathsMinimumPerMin;
final double? breathsAvgPerMin;
final double? breathsMaximumPerMin;
final List<BreathingGranularData>? breathingGranularData;
// final int? snoringEventsCount;
// final int? snoringDurationTotalSeconds;
// final List<SnoringGranularData>? snoringGranularData;
final double? saturationAvgPercentage;
final double? saturationMinimumPercentage;
final double? saturationMaximumPercentage;
final List<SaturationGranularData>? saturationGranularData;
}
Physical Summary
void example() async {
final date = DateTime.now();
try {
final physicalSummary = await HCRookSyncManager.getPhysicalSummary(date);
if (physicalSummary != null) {
// Update UI
} else {
// No records found
}
} catch (error) {
// Handle error (for apple health records not found can be returned as an error)
}
}
The returned PhysicalSummary has the following definition:
// Properties not supported by Health Connect or not processed by this SDK are commented out and will always be null.
// Commented properties are updated with the latest public version of the SDK.
class PhysicalSummary {
final DateTime dateTime;
final String sourceOfData;
final bool wasTheUserUnderPhysicalActivity;
// final int? activeSeconds;
// final int? restSeconds;
// final int? lowIntensitySeconds;
// final int? moderateIntensitySeconds;
// final int? vigorousIntensitySeconds;
// final int? inactivitySeconds;
// final List<ActivityLevelGranularData>? activityLevelGranularData;
// final int? continuousInactivePeriods;
// final double? caloriesNetIntakeKcal;
final double? caloriesExpenditureKcal;
final double? caloriesNetActiveKcal;
// final double? caloriesBasalMetabolicRateKcal;
final int? steps;
final List<StepsGranularData>? stepsGranularData;
// final int? activeSteps;
// final List<ActiveStepsGranularData>? activeStepsGranularData;
// final double? walkedDistanceMeters;
final double? traveledDistanceMeters;
final List<TraveledDistanceGranularData>? traveledDistanceGranularData;
final double? floorsClimbed;
final List<FloorsClimbedGranularData>? floorsClimbedGranularData;
final double? elevationAvgAltitudeMeters;
final double? elevationMinimumAltitudeMeters;
final double? elevationMaximumAltitudeMeters;
// final double? elevationLossActualAltitudeMeters;
final double? elevationGainActualAltitudeMeters;
// final double? elevationPlannedGainMeters;
final List<ElevationGranularData>? elevationGranularData;
// final int? swimmingNumStrokes;
// final int? swimmingNumLaps;
// final double? swimmingPoolLengthMeters;
// final double? swimmingTotalDistanceMeters;
// final List<SwimmingDistanceGranularData>? swimmingDistanceGranularData;
final int? hrMaximumBpm;
final int? hrMinimumBpm;
final int? hrAvgBpm;
final int? hrRestingBpm;
final List<HrGranularData>? hrGranularData;
final double? hrvAvgRmssd;
// final double? hrvAvgSdnn;
// final List<HrvSdnnGranularData>? hrvSdnnGranularData;
final List<HrvRmssdGranularData>? hrvRmssdGranularData;
final double? saturationAvgPercentage;
final List<SaturationGranularData>? saturationGranularData;
final double? vo2MaxMlPerMinPerKg;
final List<Vo2GranularData>? vo2GranularData;
// final int? stressAtRestDurationSeconds;
// final int? stressDurationSeconds;
// final int? lowStressDurationSeconds;
// final int? mediumStressDurationSeconds;
// final int? highStressDurationSeconds;
// final List<StressGranularData>? stressGranularData;
// final int? stressAvgLevel;
// final int? stressMaximumLevel;
}
Body Summary
void example() async {
final date = DateTime.now();
try {
final bodySummary = await HCRookSyncManager.getBodySummary(date);
if (bodySummary != null) {
// Update UI
} else {
// No records found
}
} catch (error) {
// Handle error (for apple health records not found can be returned as an error)
}
}
The returned BodySummary has the following definition:
// Properties not supported by Health Connect or not processed by this SDK are commented out and will always be null.
// Commented properties are updated with the latest public version of the SDK.
class BodySummary {
final DateTime dateTime;
final String sourceOfData;
final bool wasTheUserUnderPhysicalActivity;
// final double? waistCircumferenceCm;
// final double? hipCircumferenceCm;
// final double? chestCircumferenceCm;
// final double? boneCompositionPercentage;
// final double? muscleCompositionPercentage;
// final double? waterCompositionPercentage;
final double? weightKg;
final double? heightCm;
final double? bmi;
final double? bloodGlucoseAvgMgPerDl;
final List<BloodGlucoseGranularData>? bloodGlucoseGranularData;
final BloodPressureObject? bloodPressureAvg;
final List<BloodPressureGranularData>? bloodPressureGranularData;
final double? waterTotalConsumptionMl;
final List<HydrationAmountGranularData>? hydrationAmountGranularData;
// final List<HydrationLevelGranularData>? hydrationLevelGranularData;
final int? hrMaximumBpm;
final int? hrMinimumBpm;
final int? hrAvgBpm;
final int? hrRestingBpm;
final List<HrGranularData>? hrGranularData;
final double? hrvAvgRmssd;
// final double? hrvAvgSdnn;
// final List<HrvSdnnGranularData>? hrvSdnnGranularData;
final List<HrvRmssdGranularData>? hrvRmssdGranularData;
// final double? moodMinimumScale;
// final double? moodAvgScale;
// final double? moodMaximumScale;
// final List<MoodGranularData>? moodGranularData;
// final double? moodDeltaScale;
// final double? foodIntake;
// final double? caloriesIntakeKcal;
final double? proteinIntakeG;
final double? sugarIntakeG;
final double? fatIntakeG;
final double? transFatIntakeG;
final double? carbohydratesIntakeG;
final double? fiberIntakeG;
// final double? alcoholIntakeG;
final double? sodiumIntakeMg;
final double? cholesterolIntakeMg;
final double? saturationAvgPercentage;
final List<SaturationGranularData>? saturationGranularData;
final double? vo2MaxMlPerMinPerKg;
final List<Vo2GranularData>? vo2GranularData;
final TemperatureObject? temperatureMinimum;
final TemperatureObject? temperatureAvg;
final TemperatureObject? temperatureMaximum;
final List<TemperatureGranularData>? temperatureGranularData;
// final TemperatureObject? temperatureDelta;
final DateTime? lastUpdatedDatetime;
final DateTime? periodStartDate;
// final int? cycleDay;
final int? cycleLengthDays;
// final int? predictedCycleLengthDays;
// final String? currentPhase;
// final int? lengthOfCurrentPhaseDays;
// final int? daysUntilNextPhase;
// final bool? isAPredictedCycle;
// final List<MenstruationFlowGranularData>? menstruationFlowGranularData;
}
Activity event
void example() async {
final date = DateTime.now();
try {
final activityEvents = await HCRookSyncManager.getActivityEvents(date);
if (activityEvents.isNotEmpty) {
// Update UI
} else {
// No records found
}
} catch (error) {
// Handle error (for apple health records not found can be returned as an error)
}
}
The returned ActivityEvent has the following definition:
// Properties not supported by Health Connect or not processed by this SDK are commented out and will always be null.
// Commented properties are updated with the latest public version of the SDK.
class ActivityEvent {
final DateTime dateTime;
final String sourceOfData;
final bool wasTheUserUnderPhysicalActivity;
final DateTime activityStartDateTime;
final DateTime activityEndDateTime;
final int activityDurationSeconds;
final String activityTypeName;
// final int? activeSeconds;
// final int? restSeconds;
// final int? lowIntensitySeconds;
// final int? moderateIntensitySeconds;
// final int? vigorousIntensitySeconds;
// final int? inactivitySeconds;
// final List<ActivityLevelGranularData>? activityLevelGranularData;
// final int? continuousInactivePeriods;
// final int? activityStrainLevel;
// final double? activityWorkKilojoules;
// final double? activityEnergyKilojoules;
// final double? activityEnergyPlannedKilojoules;
// final double? caloriesNetIntakeKcal;
final double? caloriesExpenditureKcal;
final double? caloriesNetActiveKcal;
// final double? caloriesBasalMetabolicRateKcal;
// final double? fatPercentageOfCalories;
// final double? carbohydratePercentageOfCalories;
// final double? proteinPercentageOfCalories;
final int? steps;
final List<StepsGranularData>? stepsGranularData;
// final double? walkedDistanceMeters;
final double? traveledDistanceMeters;
final List<TraveledDistanceGranularData>? traveledDistanceGranularData;
final double? floorsClimbed;
final List<FloorsClimbedGranularData>? floorsClimbedGranularData;
final double? elevationAvgAltitudeMeters;
final double? elevationMinimumAltitudeMeters;
final double? elevationMaximumAltitudeMeters;
// final double? elevationLossActualAltitudeMeters;
final double? elevationGainActualAltitudeMeters;
// final double? elevationPlannedGainMeters;
final List<ElevationGranularData>? elevationGranularData;
// final int? swimmingNumStrokes;
final int? swimmingNumLaps;
// final double? swimmingPoolLengthMeters;
// final double? swimmingTotalDistanceMeters;
// final List<SwimmingDistanceGranularData>? swimmingDistanceGranularData;
final int? hrMaximumBpm;
final int? hrMinimumBpm;
final int? hrAvgBpm;
final int? hrRestingBpm;
final List<HrGranularData>? hrGranularData;
final double? hrvAvgRmssd;
// final double? hrvAvgSdnn;
// final List<HrvSdnnGranularData>? hrvSdnnGranularData;
final List<HrvRmssdGranularData>? hrvRmssdGranularData;
final double? speedNormalizedMetersPerSecond;
final double? speedAvgMetersPerSecond;
final double? speedMaximumMetersPerSecond;
final List<SpeedGranularData>? speedGranularData;
// final VelocityObject? velocityAvg;
// final VelocityObject? velocityMaximum;
// final double? paceAvgMinPerKm;
// final double? paceMaximumMinPerKm;
final double? cadenceAvgRpm;
final double? cadenceMaximumRpm;
final List<CadenceGranularData>? cadenceGranularData;
// final double? torqueAvgNewtonMeters;
// final double? torqueMaximumNewtonMeters;
// final List<TorqueGranularData>? torqueGranularData;
final List<LapGranularData>? lapGranularData;
final double? saturationAvgPercentage;
final List<SaturationGranularData>? saturationGranularData;
final double? vo2MaxMlPerMinPerKg;
final List<Vo2GranularData>? vo2GranularData;
// final PositionObject? positionStart;
// final PositionObject? positionCentroid;
// final PositionObject? positionEnd;
// final List<PositionGranularData>? positionGranularData;
// final String? positionPolylineMapDataSummary;
final double? powerAvgWatts;
final double? powerMaximumWatts;
final List<PowerGranularData>? powerGranularData;
// final int? stressAtRestDurationSeconds;
// final int? stressDurationSeconds;
// final int? lowStressDurationSeconds;
// final int? mediumStressDurationSeconds;
// final int? highStressDurationSeconds;
// final List<TssGranularData>? tssGranularData;
// final int? stressAvgLevel;
// final int? stressMaximumLevel;
}