Step 4: Collect
What you’ll do: start data collection and show each measurement as it arrives.
-
Replace
lib/main.dartwith this. It runs Step 3 and then starts collecting:lib/main.dart import 'package:flutter/material.dart';import 'package:carp_mobile_sensing/carp_mobile_sensing.dart';import 'protocol.dart';void main() => runApp(const MaterialApp(home: StepsPage()));class StepsPage extends StatefulWidget {const StepsPage({super.key});@overrideState<StepsPage> createState() => _StepsPageState();}class _StepsPageState extends State<StepsPage> {final List<String> _log = [];@overridevoid initState() {super.initState();_start();}Future<void> _start() async {// Step 3: deployfinal client = SmartPhoneClientManager();await client.configure();final study = await client.addStudyFromProtocol(createProtocol());await client.tryDeployment(study.studyDeploymentId, study.deviceRoleName);// Step 4: collectclient.resume();client.measurements.listen((m) => setState(() => _log.insert(0, m.data.dataType.name)));}@overrideWidget build(BuildContext context) => Scaffold(appBar: AppBar(title: const Text('My first CARP app')),body: ListView(children: [for (final line in _log) ListTile(title: Text(line))]),);}resume()presses play.measurementsis a live stream of everything collected. -
Run it.
Terminal window flutter runTap Allow when the app asks for permissions (notifications, physical activity).
Check it works
Section titled “Check it works”Walk around. stepevent and batterystate appear in the list. Every measurement is also saved in the database
on the phone.
Collect more kinds of data
Section titled “Collect more kinds of data”Add a sampling package or a wearable. For example, location and activity:
flutter pub add carp_context_packageSamplingPackageRegistry().register(ContextSamplingPackage()); // before creating the protocolMeasure(type: ContextSamplingPackage.LOCATION), // in the protocolAll available measures: Measure types. To keep collecting when the app is closed, see Run in production.
Next: Step 5: Get the data