Write and delete health data
Writes change the player’s health store. Require an intentional user action, direction-specific permissions, and a deletion path.
Check a type before offering a write
Section titled “Check a type before offering a write”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.
Write a typed sample
Section titled “Write a typed sample”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.}Write a workout
Section titled “Write a workout”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.
Delete by record ID
Section titled “Delete by record ID”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.
Safe interaction pattern
Section titled “Safe interaction pattern”- Explain the write or deletion.
- Prevent duplicate submission while pending.
- Confirm
Successbefore changing saved UI state. - Preserve a record until deletion succeeds.
- Sanitize errors and private payloads.
- Remove synthetic test records after device validation.
