Skip to content

Write and delete health data

Writes change the player’s health store. Require an intentional user action, direction-specific permissions, and a deletion path.

A registry entry or read grant does not imply that the active provider can write it. Query its runtime capability before displaying an editor or write button:

HealthDataTypeCapability capability =
HealthBridgeService.GetDataTypeCapability(selectedId);
if (!capability.CanWrite)
{
// Keep the row disabled and show capability.Reason.
return;
}
// Build the form from the current-platform schema.
var fields = capability.Type.GetFields(capability.Platform);

Use HealthBridgeService.GetDataTypeCapabilities() for an all-types picker. Exclude PermissionOnly entries. Never offer a write for read-only, deprecated, or unavailable rows.

For schema-backed quantity and category records, use the field definitions for units, bounds, required values, and legal category choices. A correlation such as blood pressure requires every named field; do not replace it with one generic number. Specialized records such as routes, ECG, heartbeat series, characteristics, activity summaries, and medical records can require dedicated provider flows and must respect the capability reason.

long end = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var sample = new StepsSample
{
Count = 1200,
StartTime = end - 60 * 60 * 1000,
EndTime = end
};
sample.Values["count"] = sample.Count;
HealthDataWriteResult result = await HealthBridgeService.WriteDataAsync(
new HealthDataWriteQuery("steps", sample));
if (!result.Success)
{
// Preserve unsaved UI state and offer a safe retry.
}
var workout = new WorkoutWriteSample
{
ExerciseType = "Running",
Title = "Training Run",
StartTime = end - 30 * 60 * 1000,
EndTime = end,
DistanceMeters = 5000,
Location = WorkoutLocationType.Outdoor
};
HealthDataWriteResult result =
await HealthBridgeService.WriteWorkoutAsync(workout);

Times are UTC Unix milliseconds. Duration is derived. Preflight exercise names with ExerciseTypeMapper.TryToIOSRawValue or TryToAndroidExerciseType. Unknown or platform-unsupported names can fail.

Workout writes require exercise write permission. Optional calories, distance, steps, and heart-rate samples require their matching write permissions.

if (HealthBridgeService.Provider.Supports(HealthCapability.DeleteData))
{
var query = HealthDataDeleteQuery.FromSample(appCreatedSample);
HealthDataDeleteResult result =
await HealthBridgeService.DeleteDataAsync(query);
if (result.Success)
{
// Remove the record from UI only now.
}
}

HealthKit deletes only objects written by the current app. Health Connect uses record IDs. HealthBridge does not expose broad time-range deletion.

  1. Explain the write or deletion.
  2. Prevent duplicate submission while pending.
  3. Confirm Success before changing saved UI state.
  4. Preserve a record until deletion succeeds.
  5. Sanitize errors and private payloads.
  6. Remove synthetic test records after device validation.