Installation
Add carp_health_package to a CAMS app and configure Google Health Connect on Android and HealthKit on iOS.
Setting up the package takes four stages: add the packages, configure Google Health Connect on Android, configure HealthKit on iOS, and register the package.
Add the packages
The package runs on CARP Mobile Sensing (CAMS), so complete the CAMS setup in Installation first. Then add the packages:
carp_health_package exports the health plugin, so your app can use HealthDataType and the other plugin classes without a health dependency of its own.
Configure Android
On Android, the package reads from Health Connect. Add five settings to the CAMS setup:
Set minSdk to 26 or later
The
healthplugin and the Health Connect client need API level 26 (Android 8.0) or later, and Flutter projects default to 24. Inandroid/app/build.gradle.kts, replace the default:android/app/build.gradle.kts The Health Connect client also needs
compileSdk36 or later and Android Gradle plugin 8.9.1 or later.Extend FlutterFragmentActivity
The plugin opens the Health Connect permission screen with the AndroidX activity result API, which
FlutterActivitydoes not support. Change the base class ofMainActivity:android/app/src/main/kotlin/<package_path>/MainActivity.kt Declare the read permissions
Health Connect grants access per record type, and it grants only the permissions that the app declares. Above
<application>, declare the read permission of each data type that the protocol collects. For steps and heart rate:android/app/src/main/AndroidManifest.xml CAMS only reads, so
WRITE_STEPSand the other write permissions are not needed. For the permission of every data type, see Data types. To read data from more than 30 days before the participant granted access, see Request history access (Android).Warning
The health service collects nothing until every data type is granted, and Health Connect cannot grant a permission that the manifest does not declare. Declare the permission of every data type in the protocol.
Let the app find Health Connect
On Android 11 or later, an app sees another app only when it declares it. Add Health Connect to the
<queries>element that the Flutter template already contains:android/app/src/main/AndroidManifest.xml Link the Health Connect permission screen
The Health Connect permission screen links to your app's privacy policy, and it opens your app through an intent filter or an activity alias. Inside the
<activity>element of.MainActivity, add the intent filter:android/app/src/main/AndroidManifest.xml Inside
<application>, add the activity alias:android/app/src/main/AndroidManifest.xml Both open
MainActivity, so the app must show its privacy policy, or lead to it, when it opens this way. Google may require this to approve an app that asks for health permissions.
For the Android setup of the health plugin itself, such as the Health Connect install flow, see Android. That page also declares write permissions, which CAMS does not need.
Configure iOS
On iOS, the package reads from Apple Health through HealthKit. The health plugin needs iOS 15.0 or later, which the CAMS setup already sets. It works with both Swift Package Manager and CocoaPods.
HealthKit access does not go through permission_handler, so the package needs no Podfile macros. Add two settings to the CAMS setup:
Add the HealthKit capability
Open
ios/Runner.xcworkspacein Xcode, select Runner → Signing & Capabilities, click + Capability and add HealthKit. Xcode adds thecom.apple.developer.healthkitentitlement to the app.Add the usage descriptions
HealthKit shows the read description when it asks the participant for access. CAMS only reads, but the
healthplugin documents both keys, so add both to the top-level<dict>:ios/Runner/Info.plist
For the iOS setup of the health plugin itself, see iOS.
Register the sampling package
Register the package in main(), before you call configure() on SmartPhoneClientManager:
Call CarpMobileSensing.ensureInitialized() before register(): registering a sampling package before CAMS is initialized throws a null check error.
Registering the package:
- makes the
dk.cachet.carp.healthmeasure type known to CAMS; - adds the
HealthServicedevice and itsHealthServiceManager, which asks for access and holds thehealthplugin that reads the data; - lets the app run each
HealthAppTaskas a task that the participant starts; - registers the package's classes for JSON, so that CAMS can load protocols and data that contain them.
configure() restores the studies saved in earlier runs of the app, so the package must be registered before it.
Troubleshooting
Set minSdk to 26 or later in android/app/build.gradle.kts, as in Configure Android. The health plugin and the Health Connect client declare API level 26 as their minimum, so the manifest merge fails for a lower value.
Raise compileSdk to 36 or later in android/app/build.gradle.kts, and the com.android.application plugin to 8.9.1 or later in android/settings.gradle.kts. The Health Connect client requires both versions.
Ask the participant to install Health Connect from Google Play, for example with Health().installHealthConnect(), which opens its store page. On Android 13 and earlier, Health Connect is a separate app, and CAMS logs this warning when it configures the health service.
Next step
Quickstart
Build an app that reads steps and heart rate every hour.