Read your first health data in C++
Use FHealthBridgeService to initialize the active provider and read step
samples. All completion callbacks run on the game thread.
Before you begin
Section titled “Before you begin”- Install and verify HealthBridge.
- Enable read permission for
steps. - Add the
HealthBridgemodule to the consuming module’s dependencies.
Initialize and read
Section titled “Initialize and read”#include "HealthBridgeService.h"#include "HealthBridgeTypes.h"
void ReadRecentSteps(){ FHealthBridgeService::AutoInitialize([](bool bReady) { if (!bReady || !FHealthBridgeService::IsReady()) { UE_LOG(LogTemp, Warning, TEXT("HealthBridge is not ready")); return; }
const FHealthDataQuery Query(TEXT("steps"), 7, 100); FHealthBridgeService::ReadData( Query, [](const FHealthDataReadResult& Result) { if (!Result.bSuccess) { UE_LOG(LogTemp, Warning, TEXT("HealthBridge read failed: %s"), *Result.Error); return; }
for (const FHealthDataSample& Sample : Result.Samples) { const double Count = Sample.GetNumber(TEXT("count")); UE_LOG(LogTemp, Log, TEXT("Step sample: %.0f"), Count); } }); });}FHealthDataQuery clamps DaysBack to 1–90 and MaxRecords to at least 1.
Treat empty reads correctly
Section titled “Treat empty reads correctly”bSuccess=true with an empty Samples array is a valid outcome. 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 to the app.
Do not turn an empty read into a hard error or claim that the player has no health data.
Verify the result
Section titled “Verify the result”Run the project in the editor. The expected result is:
- Initialization succeeds against provider
Mock. - The read result succeeds.
- At least one generated step count appears in the Output Log.
Remove or sanitize sample logging before shipping. Health samples and record IDs must not enter general telemetry or support logs.
