carp_context_package 0.40.0 icon indicating copy to clipboard operation
carp_context_package: ^0.40.0 copied to clipboard

CARP context sampling package. Samples location, mobility, activity, weather, air-quality, and geofence.

CARP Context Sampling Package #

This library contains a sampling package for collection of contextual data to work with the carp_mobile_sensing framework. This packages supports sampling of the following Measure types:

  • dk.cachet.carp.activity
  • dk.cachet.carp.location
  • dk.cachet.carp.geolocation
  • dk.cachet.carp.geofence
  • dk.cachet.carp.mobility
  • dk.cachet.carp.air_quality

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.

Installing #

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_mobile_sensing: ^latest
  carp_context_package: ^latest

Android Integration #

Add the following to your app's manifest.xml file located in android/app/src/main:

<manifest xmlns:android=""

    <!-- The following permissions are used in the Context Package -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <!-- For Android 9 (API 28 and earlier), use: -->
    <uses-permission android:name="" />
    <!-- for Android 10 (API 29 and later), use: -->
    <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />


NOTE: For Android 10 (API 29 and later) use the following permission:

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />

See Privacy changes in Android 10.

iOS Integration #

Add the following permissions in the Info.plist file located in ios/Runner (use your own text for explanation in the <string> tags):

<string>Uses the location API to record location.</string>
<string>Uses the location API to record location.</string>
<string>Uses the location API to record location.</string>
<string>Detects activity.</string>

In app settings enable Background Modes and check Location Updates.

iOS Setup

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_context_package/context.dart';

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


In order to use the context measures to a study protocol, this context package uses different "services" to collect data.

The dk.cachet.carp.activity measure uses the phone itself and can be added like this:

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

  // Define the smartphone as the master device.
  Smartphone phone = Smartphone();

  // Add a background task that collects activity data from the phone
        ..addMeasure(Measure(type: ContextSamplingPackage.ACTIVITY)),

All of the location-based measures;

  • dk.cachet.carp.location
  • dk.cachet.carp.geolocation
  • dk.cachet.carp.geofence
  • dk.cachet.carp.mobility

uses the LocationService service as a 'connected device' to collect data and can be added to a protocol like this:

  // Define the online location service and add it as a 'device'
  LocationService locationService = LocationService(
      accuracy: GeolocationAccuracy.high,
      distance: 10,
      interval: const Duration(minutes: 1));

  // Add a background task that collects location on a regular basis
      IntervalTrigger(period: Duration(minutes: 5)),
        ..addMeasure(Measure(type: ContextSamplingPackage.LOCATION)),

Note that you would often need to balance the configuration of the LocationService with the measure you are collecting. For example, if only using the mobility measure, a lower accuracy, distance, and sampling interval could be used.

The and dk.cachet.carp.air_quality measures uses the online Open Weather API and Air Quality Open Data Platform, respectively. In order to use these service, you need to obtain an API key from each of them. Once you have this, these services can be configured and added to a protocol like this:

  // Define the online weather service and add it as a 'device'
  WeatherService weatherService =
      WeatherService(apiKey: 'OW_API_key_goes_here');

  // Add a background task that collects weather every 30 miutes.
      IntervalTrigger(period: Duration(minutes: 30)),
        ..addMeasure(Measure(type: ContextSamplingPackage.WEATHER)),

  // Define the online air quality service and add it as a 'device'
  AirQualityService airQualityService =
      AirQualityService(apiKey: 'WAQI_API_key_goes_here');

  // Add a background task that air quality every 30 miutes.
      IntervalTrigger(period: Duration(minutes: 30)),
        ..addMeasure(Measure(type: ContextSamplingPackage.AIR_QUALITY)),

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