Quickstart
Build a Flutter app that samples the noise level and records a short audio clip with CARP Mobile Sensing.
The finished app samples the noise level every 5 minutes, records 30 seconds of audio shortly after it starts and prints each measurement.
Prerequisites
- CARP Mobile Sensing (CAMS) set up as described in Installation.
- The package added, with the microphone permission and the iOS settings, as described in Installation.
- A physical Android or iOS phone.
Complete example
Replace the contents of lib/main.dart with:
How it works
| Stage | What it does |
|---|---|
| 1. Register | Makes the four measure types and their data classes known to CAMS. Register before configure(). CarpMobileSensing.ensureInitialized() runs first, because registering needs an initialized CAMS. |
| 2. Protocol | Creates a protocol with the phone as its only device. SQLiteDataEndPoint stores the measurements in a SQLite database on the phone, and the recordings stay on the phone as files. |
| 3. Noise | NOISE is event-based, so an ImmediateTrigger starts it once. It then samples a 10-second window every 5 minutes, the default, and adds one measurement per window. |
| 4. Audio | AUDIO records until its task stops, so the task's duration ends the recording after 30 seconds. DelayedTrigger fires 30 seconds after each start of sampling. |
| 5. Deploy | CAMS asks for microphone access and then starts the triggers. The app adds the study only on the first run: on later runs, configure() restores the saved study. |
| 6. Print | client.measurements delivers each measurement as it is added. |
To record at regular times or when the participant starts a task, see Audio recordings.
Run the app
Connect the phone and run:
Allow microphone access when the app asks. About a minute after the start, the recording stops, and the console prints an audio measurement that refers to the file. Formatted for readability, it looks like this:
The first noise window starts 5 minutes after the start, and its measurement follows 10 seconds later:
CAMS also adds a dk.cachet.carp.triggeredtask measurement each time a trigger fires.
Troubleshooting
Keep the app running for at least one interval plus one window: the first window starts one interval after the task starts. A window with fewer than two finite readings, such as a window of digital silence, adds no measurement.
Wait until the recording stops: the measurement is added when the task pauses the recording, not when it starts. On iOS, check that permission_handler has microphone access enabled, as described in Installation.
Next steps
Noise
Sampling windows, decibel values and the noise data.
Audio recordings
Start and stop recordings, and find and upload the files.
Video and images
Add videos and images that your app captures to the study data.