CARP Health Sampling Package

pub package pub points github stars MIT License arXiv

This library contains a sampling package for sampling health data from Apple Health and Google Fit or Health Connect to work with the carp_mobile_sensing framework. It uses the health plugin for this. This packages supports sampling of the following Measure types:


See the wiki for further documentation, particularly on available measure types. See the CARP Mobile Sensing App for an example of how to build a mobile sensing app in Flutter.

For Flutter plugins for other CARP products, see CARP Mobile Sensing in Flutter.

If you're interested in writing you own sampling packages for CARP, see the description on how to extend CARP on the wiki.


To use this package, add the following to you pubspc.yaml file. Note that this package only works together with carp_mobile_sensing.

    sdk: flutter
  carp_core: ^latest
  carp_mobile_sensing: ^latest
  carp_health_package: ^latest

Then, follow the setup guides in the health plugin.

Android Integration

The health plugin supports both Google Fit and Health Connect.

Google Fit can be tricky to set up and it requires a separate app to be installed. Please follow this guide to set up Google Fit. Check out the documentation of the health package.

Health Connect seems more simple to setup - see the documentation on the health package and on the Android Developer page.

iOS Integration

See the setup guide for iOS in the health package.

Add this permission in the Info.plist file located in ios/Runner:

<string>We will sync your data with the Apple Health app to give you better insights</string>
<string>We will sync your data with the Apple Health app to give you better insights</string>

Then open your Flutter project in XCode by right clicking on the ios folder and selecting "Open in XCode". Enable "HealthKit" by adding a capability inside the "Signing & Capabilities" tab of the Runner target's settings.

Using it

To use this package, import it into your app together with the carp_mobile_sensing package:

import 'package:carp_core/carp_core.dart';
import 'package:carp_mobile_sensing/carp_mobile_sensing.dart';
import 'package:carp_health_package/health.dart';

Before creating a study and running it, register this package in the SamplingPackageRegistry.


When defining a study protocol with a health device, it would look like this:

  // Create a study protocol
  StudyProtocol protocol = StudyProtocol(
    ownerId: '',
    name: 'Health Sensing Example',

  // Define which devices are used for data collection.

  // First add this smartphone.
  final phone = Smartphone();

  // Define which health types to collect.
  var healthDataTypes = [

  // Create and add a health service (device)
  final healthService = HealthService(types: healthDataTypes);
  protocol.addConnectedDevice(healthService, phone);

Note that a list of HealthDataType types is specified for the service. This is later used by the service to request the right permission to access this type of data.

Data sampling can now be configured by a measure in the protocol:

  // Automatically collect the set of health data every hour.
  // Note that the [HealthSamplingConfiguration] is a [HistoricSamplingConfiguration]
  // which samples data back in time until last time, data was sampled.
      PeriodicTrigger(period: Duration(minutes: 60)),
        ..addMeasure(Measure(type: HealthSamplingPackage.HEALTH)
          ..overrideSamplingConfiguration =
              HealthSamplingConfiguration(healthDataTypes: healthDataTypes)),

This would collect health data every hour using the same data types, as configured for the service. Configuration of what data to collect is done via the HealthSamplingConfiguration which is used to override the default configuration (default is to collect nothing). Another set of data to collect can be specified, as shown below. However, the user might not have granted access to collect this data.

  // Automatically collect another set of health data every hour
  // Note, however, that the service defined above DOES NOT have this list of
  // health data specified, and has therefore not asked for permission to access
  // this new set of health data.
  // However, on Apple Health, for example, the user has an option to 
  // "Turn All Categories On". If the use has done this, the all the data listed 
  // below is accessible. 
      PeriodicTrigger(period: Duration(minutes: 60)),
        ..addMeasure(Measure(type: HealthSamplingPackage.HEALTH)
          ..overrideSamplingConfiguration =
              HealthSamplingConfiguration(healthDataTypes: [

The HealthSamplingConfiguration can be configured to collect a specific set of HealthDataType, like:

  • STEPS,
  • ...

See the HealthDataType documentation for a complete list.

A HealthSamplingConfiguration is a HistoricSamplingConfiguration. This means that when triggered, the task and measure will try to collect data back to the last time data was collected. Hence, this probe is suited for configuration using some trigger that collects data on a regular basis, like the PeriodicTrigger used above. However, it can also be configured using as an AppTask that asks the user to collect the data.

  // Create an app task for the user to collect his own health data once pr. day
      PeriodicTrigger(period: Duration(hours: 24)),
          type: 'health',
          title: "Press here to collect your physical health data",
              "This will collect your weight, exercise time, steps, and sleep time from Apple Health.",
          measures: [
            Measure(type: HealthSamplingPackage.HEALTH)
              ..overrideSamplingConfiguration =
                  HealthSamplingConfiguration(healthDataTypes: healthDataTypes)

NOTE - Health data can only be collected when the app is in the foreground and the phone is unlocked. This applies both for Android and iOS.

See the example.dart file for a full example of how to set up a CAMS study protocol for this sampling package.

Collected Data

The data collected is contained in a HealthData object, which wraps the data collected from a HealthDataPoint. For example, workout collected from Apple Health serialized to JSON may looks like this:

 "sensorStartTime": 1700415799841973,
 "data": {
  "__type": "",
  "uuid": "4321",
  "value": {
   "workoutActivityType": "AEROBICS",
   "totalEnergyBurned": 8,
   "totalEnergyBurnedUnit": "KILOCALORIE",
   "totalDistance": 1000,
   "totalDistanceUnit": "METER"
  "unit": "NO_UNIT",
  "date_from": "2023-11-19T09:43:19.841907Z",
  "date_to": "2023-11-19T17:43:19.841907Z",
  "data_type": "WORKOUT",
  "platform": "IOS",
  "device_id": "1234",
  "source_id": "4321",
  "source_name": "4321"

The type of the collected health data is and in general, the collected health data has the type of<health_type>, where health_type is the lower-case version of HealthDataType.


A CAMS sampling package for collecting health information from Apple Health or Google Fit. Is using the health plugin. Can be configured to collect the different HealthDataType.