carp_health_package 2.7.0 carp_health_package: ^2.7.0 copied to clipboard
CARP health sampling package. Samples health data from Apple Health or Google Fit.
CARP Health Sampling Package #
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
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
dependencies: flutter: sdk: flutter carp_core: ^latest carp_mobile_sensing: ^latest carp_health_package: ^latest ...
Then, follow the setup guides in the health plugin.
Android Integration #
iOS Integration #
See the setup guide for iOS in the health package.
Add this permission in the
Info.plist file located in
<key>NSHealthShareUsageDescription</key> <string>We will sync your data with the Apple Health app to give you better insights</string> <key>NSHealthUpdateUsageDescription</key> <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
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: 'email@example.com', name: 'Health Sensing Example', ); // Define which devices are used for data collection. // First add this smartphone. final phone = Smartphone(); protocol.addPrimaryDevice(phone); // Define which health types to collect. var healthDataTypes = [ HealthDataType.WEIGHT, HealthDataType.EXERCISE_TIME, HealthDataType.STEPS, HealthDataType.SLEEP_ASLEEP, ]; // 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. protocol.addTaskControl( PeriodicTrigger(period: Duration(minutes: 60)), BackgroundTask() ..addMeasure(Measure(type: HealthSamplingPackage.HEALTH) ..overrideSamplingConfiguration = HealthSamplingConfiguration(healthDataTypes: healthDataTypes)), healthService);
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. protocol.addTaskControl( PeriodicTrigger(period: Duration(minutes: 60)), BackgroundTask() ..addMeasure(Measure(type: HealthSamplingPackage.HEALTH) ..overrideSamplingConfiguration = HealthSamplingConfiguration(healthDataTypes: [ HealthDataType.BLOOD_GLUCOSE, HealthDataType.BLOOD_PRESSURE_DIASTOLIC, HealthDataType.BLOOD_PRESSURE_SYSTOLIC, HealthDataType.BLOOD_PRESSURE_DIASTOLIC, HealthDataType.HEART_RATE, HealthDataType.STEPS, ])), healthService);
HealthSamplingConfiguration can be configured to collect a specific set of
HealthDataType documentation for a complete list.
HealthSamplingConfiguration is a
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 protocol.addTaskControl( PeriodicTrigger(period: Duration(hours: 24)), AppTask( type: 'health', title: "Press here to collect your physical health data", description: "This will collect your weight, exercise time, steps, and sleep time from Apple Health.", measures: [ Measure(type: HealthSamplingPackage.HEALTH) ..overrideSamplingConfiguration = HealthSamplingConfiguration(healthDataTypes: healthDataTypes) ]), healthService);
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.
example.dart file for a full example of how to set up a CAMS study protocol for this sampling package.