Building sampling packages
Build, test and publish a sampling package that adds your own measure types to CARP Mobile Sensing.
A sampling package adds measure types to CARP Mobile Sensing (CAMS). For each type, it defines the data class that holds the values of a measurement and the probe that collects them. For a wearable or an online service, it also connects the phone to the device.
Build one when no existing package collects the data your study needs. To find an existing one, see Measure types.
A sampling package is a Flutter package of its own. You can use it in your app, share it with other CAMS apps or publish it on pub.dev.
Written for CAMS 2.5.1
The pages and the example package target carp_mobile_sensing 2.5.1, carp_core 2.2.2 and carp_serializable 3.0.0. Where CAMS 2.5.1 has a defect that affects package authors, the pages state the rule and the workaround.
How it works
CAMS finds your package through the measure type in a study protocol:
flowchart LR
Protocol["<b>Study protocol</b><br/>a measure with<br/>your measure type"] --> Phone
subgraph Phone["CAMS on the phone"]
direction TB
Package["<b>Sampling package</b><br/>found by the registry<br/>for the measure type"] -->|creates| Probe["<b>Probe</b><br/>reads the sensor"]
Probe --> Measurement["<b>Measurement</b><br/>your data class<br/>and its timestamps"]
end
Phone --> Manager[("<b>Data manager</b><br/>stores or uploads<br/>the measurements")]
- A task in the study protocol holds a measure whose type is one of your package's measure types, such as
com.example.airpressure. - When the phone gets the deployment, CAMS asks the
SamplingPackageRegistryfor the registered package that declares the type. That package'screate()method returns a new probe for the measure. - When the task's trigger fires, CAMS resumes the probe. The probe reads the sensor and adds measurements, each holding an object of your data class and its timestamps.
- CAMS applies the privacy schema and data format that the protocol selects. The data manager then stores or uploads each measurement.
Parts of a sampling package
| Part | Based on | What it does | Page |
|---|---|---|---|
| Data class | Data or SensorData | Holds the values of one measurement and serializes them to JSON | Data classes |
| Probe | A Probe subclass, such as StreamProbe | Collects the data while CAMS has it resumed | Probes |
| Sampling configuration | SamplingConfiguration or a subclass | Holds the settings of a measure, such as how often to sample; CAMS has classes you can reuse | Sampling configurations |
| Package class | SmartphoneSamplingPackage, or SamplingPackage for another device | Declares the measure types, creates the probes and registers the JSON classes | Sampling package class |
| Device manager | A DeviceManager subclass | Connects to a wearable or an online service; a package for the phone needs none | Devices and services |
| Privacy function | A DataTransformer function | Coarsens or hashes data before it is stored, in studies that ask for it | Privacy and data formats |
Example package
The guide builds one example package, barometer_sampling_package, which reads the phone's barometer with the sensors_plus plugin. It adds two measure types:
| Measure type | Data class | Probe | Default sampling |
|---|---|---|---|
com.example.airpressure | AirPressure: one reading in hectopascals (hPa) | AirPressureProbe, a StreamProbe | Every reading, requested once per second |
com.example.airpressuresummary | AirPressureSummary: the count, mean, minimum and maximum of the readings in hPa | AirPressureSummaryProbe, a BufferingPeriodicStreamProbe | A 10-second window every 5 minutes |
Together, the pages show every source file of the package, so you can rebuild it, and explain the rule behind each part. The package also rounds both measures to whole hectopascals in studies that ask for privacy protection.
The package is an example, not a published CARP package. Its measure types use the namespace com.example, the reverse of example.com, a domain reserved for documentation. Use a namespace that you control in your own package.
Prerequisites
- A Flutter app that runs a CAMS study and reads its measurements, as described in Using CAMS.
- Knowledge of study protocols, triggers, tasks and measures, as described in Domain model.
- A phone with the sensor that your package reads. The iOS Simulator, for example, has no barometer.
Guide pages
Package setup
Create the package, add its dependencies and generate its JSON code.
Data classes
Define the classes that hold the values of each measurement.
Probes
Choose a probe base class and collect the data from the sensor.
Sampling configurations
Give each measure default settings that a protocol can override.
Sampling package class
Declare the measure types, create the probes and register the package.
Devices and services
Add a device manager for a wearable or an online service.
Privacy and data formats
Protect sensitive values and convert data to other formats.
Testing and publishing
Test the package, run it in an app and publish it.
Common pitfalls
The rules that keep a sampling package from losing data.