Quickstart
Build a Flutter app that collects step, battery and app lifecycle data with CARP Mobile Sensing.
Build a Flutter app that collects step events, battery state and app lifecycle events (the app being opened or closed), and lists each measurement as it arrives. The data stays on the phone; to store it elsewhere, see Storing data.
Prerequisites
- Flutter, installed with Google's Install Flutter guide for your system. Run
flutter doctorand confirm that it shows green checkmarks for Android or iOS. - A phone set up for development:
- Android: turn on USB debugging.
- iOS: set up Xcode and your device. iOS apps can only be built on a Mac.
Use a physical device
Simulators have no step counter or real battery, so they produce very little data.
Build the app
Create a Flutter app
Add the CARP packages
This downloads the packages from pub.dev and adds them to
pubspec.yaml:Configure Android and iOS
CARP uses platform features that need configuration on each platform.
Android iOS To allow the app to count steps, add the activity recognition permission to
android/app/src/main/AndroidManifest.xml, directly above<application:android/app/src/main/AndroidManifest.xml Then enable core library desugaring in
android/app/build.gradle.ktsby adding the marked lines:android/app/build.gradle.kts CARP requires iOS 15 or newer. Open
ios/Runner.xcworkspacein Xcode, select Runner → General, and set Minimum Deployments to 15.0.To allow the app to count steps, add a motion usage description to
ios/Runner/Info.plist, inside the<dict>:ios/Runner/Info.plist Replace the app code
Replace the contents of
lib/main.dartwith:lib/main.dart Run the app
Connect your phone with a cable and run:
The app requests permissions for notifications and physical activity. Allow them. Walk around, switch to another app and back, and
stepevent,applifecycleeventandbatterystateappear in the list.
How it works
The four numbered comments in lib/main.dart map to the core CARP workflow:
| Step in the code | What it does |
|---|---|
| 1. Protocol | Defines what to collect. SmartphoneStudyProtocol.local(...) is a shortcut for a protocol with only this phone, one participant, and data saved on the phone. |
| 2. Client | SmartPhoneClientManager is the CAMS runtime. configure() starts it with default settings. |
| 3. Add, deploy, resume | addStudyFromProtocol() hands the protocol to the client, tryDeployment() prepares the sensors, and resume() starts collecting. |
| 4. Measurements | client.measurements is a live stream of everything collected. You can display, store or forward each measurement. |
Every measurement is also saved in a database on the phone, and no data leaves the device. To store data elsewhere, see Storing data.
Add more data types
To collect other kinds of data, add a sampling package. For example, carp_context_package adds location and activity (walking, cycling and so on):
Package permissions
Each sampling package may need its own permissions, for example for location. The package's page on pub.dev lists the configuration to add for Android and iOS.
Measure types lists every available measure, and the API reference lists all packages.
Next steps
Choose where data goes
Keep data on the phone, upload it to CAWS, or send it to your own server.
Collect more kinds of data
All measures, and the sampling packages that add them.
Schedule data collection
Triggers: every hour, at 8:00, when the participant is walking…
Ask the participant questions
Surveys and tasks the user is notified about.
Keep collecting in the background
Battery, background mode and notifications on Android and iOS.
Apple Watch
Collect sensor data from an Apple Watch. Packages for Polar, Movesense and other wearables are listed in the API reference.
How CARP fits together
The components and the study lifecycle, with diagrams.
Get help
- Ask questions on Discord.
- Report problems on GitHub.
- Email support@carp.dk.