Skip to content

Read your first health data

Complete this lesson with editor mock data before moving to a mobile device.

using HealthBridge;
using HealthBridge.Data;
using UnityEngine;
public sealed class StepReader : MonoBehaviour
{
public async void ReadRecentSteps()
{
if (!HealthBridgeService.IsReady)
{
Debug.LogWarning("HealthBridge is not ready.");
return;
}
var query = new HealthDataQuery(
"steps",
daysBack: 7,
maxRecords: 100);
HealthDataReadResult result =
await HealthBridgeService.ReadDataAsync(query);
if (!result.Success)
{
Debug.LogWarning($"Step read failed: {result.Error}");
return;
}
foreach (HealthDataSample sample in result.Samples)
{
if (sample is StepsSample steps)
Debug.Log($"{steps.Count} steps at {steps.StartDateTime}");
}
}
}

Always check Success before reading Samples. HealthDataSample has no single Value property; cast to a typed sample or use GetValue<T>(key).

Success=true with zero samples is valid. It can mean:

  • The selected period contains no records.
  • The device has not synchronized yet.
  • On iOS, the player declined read access without exposing that decision.

Do not report an empty read as proof that the player has no data or granted access.

Enter Play Mode and run the read after initialization. The expected result is a successful query with one or more typed mock StepsSample records.

Remove or sanitize health-value logging before release.