All entries

    React Native Health Connect: Avoid Double-Counting Steps

    Use Health Connect's aggregate step count in a React Native Android app, then test overlapping sources, permission changes, and day boundaries.

    Pixel-art Mumbai waterfront runners with a watch and phone whose step streams converge
    AI-generated pixel-art editorial illustration.

    For a daily step total in a React Native Android app, use Health Connect's aggregation for StepsRecord rather than adding every raw record from a phone and a wearable. Raw records are useful for diagnosis. They can contain overlapping intervals, so adding them can treat two measurements of one walk as two walks. The app also needs a separate state for unavailable data.

    A step-count fixture with overlapping records

    Imagine a phone and a watch tracking the same morning walk. Later, a different non-overlapping activity is recorded only by the phone. The record counts below are fictional. You can use them to test that your interface calls the aggregate path and displays whatever result Health Connect reports instead of substituting a JavaScript sum.

    The raw sum is 2,730. It is the sum of three stored records, rather than evidence for 2,730 distinct steps. Do not hardcode a universal expected aggregate from this table. The effective result can depend on what the device actually stores and how Health Connect handles data origins and priorities. In an adapter unit test, explicitly choose a fake aggregate and assert that the UI displays that value, not the raw sum.

    Scroll the table horizontally to see all columns.

    A step-count fixture with overlapping records
    SourceLocal intervalRaw count
    Phone08:00–08:201,200
    Watch08:00–08:201,180
    Phone12:00–12:10350
    EXPLAINED / SYSTEM FLOW

    Use the aggregate result for the daily headline

    Raw step records and the aggregate query answer separate questions about the same fictional time window.

    Device inputs

    1. Phone records

      The phone records the morning walk and a later activity.

    2. Watch records

      The watch also measures the morning interval.

    App boundary

    1. Health Connect aggregation

      Ask for StepsRecord.COUNT_TOTAL for the selected day.

    2. Headline display

      Render the returned total and retain distinct permission and unavailable states.

    THE KEY DISTINCTION

    A record sum is a diagnostic, while the native aggregate supplies the product total.

    Illustrative flow with synthetic inputs. It does not predict a device-specific aggregate value.

    Choose aggregation for the headline number

    Android's steps documentation exposes StepsRecord.COUNT_TOTAL for aggregation over a range. In a React Native integration, use an adapter that calls the native Health Connect aggregation path for the metric and time range. Keep diagnostic raw records behind a separate adapter method. That boundary makes it harder for a later UI change to use the wrong source and gives tests a clear dependency to stub.

    Start with one explicitly named range, such as local calendar day, and serialize both start and end consistently. The day starts at local midnight and ends at the next local midnight. Avoid assuming that adding a fixed 24 hours handles every timezone and daylight-saving transition. Document what changing timezone means for previously displayed days.

    Keep permission and no-data states distinct

    Check authorization again on return from the permission flow and when a previously granted permission might have changed. For older dates, review the Android documentation for current history permissions and supported platform versions. An empty query for an out-of-range date should not silently look like a zero-activity day. If the reason is unknown, keep the interface neutral.

    Scroll the table horizontally to see all columns.

    Keep permission and no-data states distinct
    StateReader-facing behavior
    Health Connect unavailableExplain that step data cannot be read on this setup
    Permission missing or revokedExplain how to reconnect, without displaying a made-up zero
    Permission granted with no aggregate resultShow that no data is available for this window
    Aggregate returns zeroDisplay zero for that particular window
    Latest read failedMark a previously cached total as stale, if you keep one

    Test the adapter and an Android device separately

    Use only test accounts and synthetic data for the last case. The adapter test verifies your app contract. A device test checks platform behavior under a specific setup. Record the Android version, data origins, dates and permission state with any observation you publish. This example explains an integration pattern and does not describe UltraFit360's internal implementation.

    • Unit-test that the adapter receives the intended day start and end.
    • Stub an aggregate distinct from the raw sum and check the displayed number.
    • Revoke permission after a successful read and confirm the screen explains the changed state.
    • Cross midnight, change timezone in a controlled test, and check how day labels update.
    • Use a supported Android test setup with two data sources to observe actual aggregation and compare it with the same time window.

    Compare the same sources and the same window

    When two screens disagree, first write down the metric, the exact time range, the timezone and which origins were included. Check if one screen was refreshed more recently. A local cache can be useful while the native query runs, but its label should show when it was updated. Separate caching failures from permission and aggregation failures so a fix for one issue does not mask another.

    Write a contract test with a fake aggregate

    The number 1,520 is deliberately arbitrary. Its purpose is to be distinguishable from the sum of the records, rather than to describe the platform's expected data-origin behavior. In this example the adapter supplies the aggregate and the view displays it. A different fake result, such as zero, lets you verify that a measured zero does not accidentally fall through to the missing-data presentation.

    Add a test for a rejected read. The first successful load can be cached with an explicit timestamp, then a second load can return a permission error. Decide whether your product clears the cached number or labels it stale. The important property is that a cached value does not make a permission failure look like a fresh total. If you keep the cache, minimize what is retained and avoid logging raw record content.

    Pseudocode for the app boundary
    fakeStore.rawSteps = [1200, 1180, 350]
    fakeStore.aggregateSteps = 1520
    result = loadDailySteps(fakeStore, calendarDay)
    assert(result.kind === "measured")
    assert(result.count === 1520)

    Pin down what today means

    A dashboard and a history view can choose different windows without either being broken. A dashboard may mean the user's current local day. A weekly graph may group using the timezone recorded when an activity occurred. Pick a definition and use it consistently across native queries, app cache keys and labels. The cache should not reuse the same entry for a different query range merely because both pages say today.

    For a useful test, open the app before midnight, hold it in the background and return after midnight. Check which day label appears, whether the new query actually ran, and whether the old total is still on screen while loading. On devices where timezone changes are relevant, repeat with a controlled change in system timezone. Keep the date-range observations separate from measurements about how any particular device counts steps.

    Frequently asked questions

    Should I sum raw Health Connect step records? Use the native aggregation path for a user-facing total. Keep raw records for diagnostics when you need to inspect source intervals.

    Does no data mean I walked zero steps? Keep a missing result, a missing permission, and a successfully measured zero as separate application states.

    Why do my app and my watch show different totals? Compare the selected time range, timezone, data origins and last refresh first. Different inputs can produce different totals.

    FURTHER READING

    Sources

    Technical references checked .

    END OF ENTRY / 98
    Eeshan Singh with Big Ben in London
    BEHIND THE JOURNAL

    Eeshan Singh

    I’m Eeshan Singh Pokharia, an AI Full Stack Developer and CoFounder of UltraFit360. I work across AI features, full-stack product development, and web and mobile experiences—from Figma and frontend interactions to the systems behind them.

    Fitness app screens often make one number look simple. The hard part is deciding what the number means across devices, permissions and date boundaries. These synthetic fixtures make that decision visible without using any user's data.

    Meet the author
    THE CONVERSATION

    Your side of the story.