Android Health Samples

repository·main·Indexed 43 days ago

https://github.com/android/health-samples

A collection of Android Studio reference implementations for health-related APIs on Android and Wear OS. Includes samples for Health Connect, the Samsung Health Platform using the HealthDataClient API, and Wear OS Health Services featuring ExerciseClient, MeasureClient, and PassiveMonitoringClient.

Tokens
7.4K
Snippets
20
Records
47
Agent score
56%

What's inside Android Health Samples

  1. Overview of Health Samples Repository

    main
    The Health Samples Repository provides a collection of individual Android Studio projects designed to help developers build health-related applications for Android and Wear OS. The repository is organized by the specific health API being demonstrated.
  2. Overview of Health Services Samples

    main

    This repository provides a collection of individual Android Studio projects designed to help developers implement Health Services on Wear OS.

    Important Note on API Stability: The Health Services API is currently in alpha release. This means APIs are subject to being added, removed, or changed. It is not recommended for production use at this time.

  3. Manage exercise experiences with ExerciseClient

    main

    The Exercise Sample demonstrates how to use the ExerciseClient API to manage an exercise session.

    Key features demonstrated include:

    • Configuring and starting a running exercise.
    • Monitoring real-time metrics such as exercise duration, heart rate (bpm), calories burned, distance traveled, and lap counts.
    • Handling app backgrounding: while an exercise is in progress, an ongoing notification is provided to allow the user to quickly return to the exercise screen.

    Note on testing: If you are testing on a device without active user movement, you may need to use a synthetic data provider to prevent Health Services from immediately pausing the exercise due to inactivity.

  4. Implement a Heart Rate complication

    main

    The sample demonstrates how to expose heart rate data to watch faces using a complication via HeartRateComplicationService.

    Implementation Details

    • Dynamic Expressions: The service uses PlatformHealthSources.heartRateBpm() wrapped in a DynamicComplicationText. On API 33+, the platform re-evaluates this expression roughly once per second in interactive mode, allowing for live updates without waking the complication service.
    • Fallback & Older Devices:
      • For devices below API 33, the complication shows the most recent measurement stored by PassiveDataService.
      • PassiveDataService calls ComplicationDataSourceUpdateRequester.requestUpdateAll() when new data arrives to ensure the fallback text and older device paths stay current.
    • Permissions: To use dynamic heart rate binding, the app must hold BODY_SENSORS (or READ_HEART_RATE on API 36+).
  5. Use PassiveMonitoringClient for background data and goals

    main

    The PassiveMonitoringClient is used to receive updates in the background without requiring the app to be in the foreground. Two specific sample implementations demonstrate this:

    • PassiveData: Demonstrates receiving background data updates.
    • PassiveGoals: Demonstrates receiving goal notifications in the background.
  6. Use Health Platform for health data (Legacy/Alternative)

    main

    The health-platform-v1/ directory contains samples for reading and writing health, fitness, and wellness data using the Health Platform.

    Warning: Health Connect is the preferred choice for new integrations over the Health Platform.

  7. Insert dummy session data into Health Platform

    main

    The sample includes functionality to generate and insert dummy fitness/health data into the Health Platform.

    When triggered, the app attempts to generate data and insert it. If necessary, the app will present permission grants to the user. Once inserted, this session data is visible to any other clients (such as Samsung Health) that have been granted the corresponding READ permission in the Health Platform.

  8. Run the Passive Goals Sample (Compose)

    main

    To run this sample, you must have a Wear OS device with Health Services installed.

    1. Open the sample project in Android Studio.
    2. Connect your Wear device.
    3. Launch the app.

    Upon startup, the app detects if steps and floors data are available. If available, you can use the UI switch to enable or disable goal subscriptions. The sample implements two specific goals:

    • Daily step goal: Sends a notification when 10,000 steps are achieved (resets daily).
    • Floors goal: Sends a notification every time 3 floors are climbed.
  9. Use Health Connect for health, fitness, and wellness data

    main

    Use the samples in the health-connect/ directory to learn how to read, write, and share health, fitness, and wellness data using the Health Connect API.

    Note: For new integrations, developers should use Health Connect instead of the Health Platform.