Quickstart
Build an app that reads steps and heart rate from Google Health Connect or Apple Health every hour.
Build an app that asks for access to steps and heart rate, reads them every hour, and prints each data point as JSON.
Prerequisites
- The setup from Installation, with the
READ_STEPSandREAD_HEART_RATEpermissions in the Android manifest. - A phone with steps or heart rate data from the last 30 days in Google Health Connect or Apple Health. The phone itself, a watch or a fitness app can record it.
- On Android 13 and earlier, the Health Connect app from Google Play. See Health Connect.
Complete example
Replace the contents of lib/main.dart with:
How it works
| Stage | What it does |
|---|---|
| 1. Register | Makes the health measure type, HealthService and its manager known to CARP Mobile Sensing (CAMS). CarpMobileSensing.ensureInitialized() runs first, because registering needs an initialized CAMS. |
| 2. Protocol | HealthService is a connected device that stands for Health Connect or Apple Health on the phone. A protocol with health measures always needs one, and tasks that target it run only while it is connected. |
| 3. Hourly read | getHealthMeasure() creates a dk.cachet.carp.health measure for the two types. Its first read goes 30 days back, and each later read starts where the previous one ended. The measure is one-time, so the PeriodicTrigger repeats it: once when the health service connects, then every hour while the app runs. |
| 4. Deploy | Deploying adds the measure's data types to the health service manager and tries to connect the service. On Android, the service connects only when every type is already granted; on iOS, it connects right away. The app adds the study only on the first run: on later runs, configure() restores the saved study. |
| 5. Print | Prints every measurement, including a dk.cachet.carp.triggeredtask measurement each time the trigger fires. |
| 6. Access | requestPermissions() shows the permission screen for the data types of all health measures. Connecting the service then resumes its task control, so the first read with the new access runs right away instead of an hour later. On iOS, the service is already connected, so the function disconnects it first. |
Run the app
Run the app on a phone and tap Allow access to health data:
Health Connect shows its permission screen with steps and heart rate. Allow both: the health service connects only when every data type is allowed, and then the first read starts.
HealthKit shows its access sheet with steps and heart rate. Allow both. iOS does not tell the app what the participant chose, so a type that stays off returns no data and no error.
Check the data
The console prints one line for each data point from the last 30 days, and every hour the data recorded since the previous read. A step count from Health Connect looks like this, formatted for readability:
sensorStartTime and sensorEndTime are the start and end of the sample in microseconds since 1970, not the time of the read. For every field, see Health data.
Troubleshooting
Allow both data types: tap the button again, or allow them in the Health Connect app and tap the button once more. The health service connects only when every type is allowed; until then, the log shows "has not the permissions required to connect". Also check that the manifest declares both permissions and that Health Connect has data from the last 30 days.
Check that the participant allowed steps and heart rate in the access sheet. HealthKit does not report a type that is off; the participant can turn it on later in the Apple Health app. HealthKit also refuses reads while the phone is locked, and the next read then covers the missed time.
Next steps
Collecting data
Collect with background measures or health app tasks, and choose triggers.
Permissions
Ask for access to health data and connect the health service.
Health data
The fields, value classes and units of health data measurements.