Quickstart
Build an app that connects to a Polar sensor and prints its heart rate and ECG measurements.
The finished app connects to a Polar H10 by its Polar ID and prints each heart rate and ECG measurement as JSON.
Prerequisites
- The setup in Installation, including the CARP Mobile Sensing (CAMS) setup it builds on.
- A Polar H10 sensor and its Polar ID: the eight characters printed on the sensor, for example
B5FC172F. With an H9 or a Verity Sense, the example collects heart rate only. - A physical Android or iOS phone. Emulators and simulators cannot connect to a Bluetooth sensor.
Complete example
Replace the contents of lib/main.dart with this program, and set polarIdentifier to the Polar ID of your sensor:
How it works
| Stage | What it does |
|---|---|
| 1. Register | PolarSamplingPackage makes the Polar measure types, the PolarDevice configuration and its device manager known to CAMS. CarpMobileSensing.ensureInitialized() runs first, because registering needs an initialized CAMS. |
| 2. Protocol | The phone is the primary device, and a PolarDevice with the role Polar H10 is connected to it. The protocol does not identify the physical sensor. |
| 3. Task | A BackgroundTask on the Polar device collects HR and ECG. Both measures are event-based, so an ImmediateTrigger starts them once, and they stream while the sensor is connected. |
| 4. Bind | requestPermissions() asks for Bluetooth and, on Android, location. Setting polarIdentifier binds the Polar H10 role to your physical sensor. |
| 5. Deploy | During the deployment, CAMS configures the Polar device manager and connects to the sensor, because the manager has a Polar ID. The Polar measures start when the sensor has reported the data types it offers. The app adds the study only on the first run: on later runs, configure() restores the saved study. |
| 6. Print | client.measurements streams every measurement, and the example prints each one as JSON. |
The example binds the sensor before it deploys the study, so CAMS connects to it as part of the deployment. To bind a sensor later, for example from a settings screen, see Connecting a device.
Run the app
Put on the sensor
Wear the H10 on its chest strap, against the skin.
Start the app on your phone
Allow the Bluetooth permission. On Android, also allow access to the location: CAMS needs it to connect to Bluetooth devices.
Check the console
Once the phone is connected to the sensor, the console prints one JSON object per measurement. ECG measurements hold batches of voltage samples, and heart rate measurements look like this:
The console also shows the task measurements of CAMS itself, such as
dk.cachet.carp.triggeredtask.
To work with the values in code, check the type of the data:
For all the fields, see Measure types.
Troubleshooting
Print the status of the Polar device manager and act on it:
configured: the device manager has no Polar ID. SetpolarIdentifierbefore the study is deployed.disconnectedright after the start: the Bluetooth permissions are missing. Allow them, and on Android, check the manifest entries in Installation.connectingfor a long time: the phone has not found the sensor. Check that the Polar ID matches the ID printed on the sensor, and that the sensor is worn, charged and near the phone.
For the code that prints the status, see Monitor the connection.
Use a Polar H10: it is the only supported sensor with ECG. With an H10, CAMS may have started the tasks before the sensor reported the ECG data type. Once the sensor is connected, pause and resume sampling with client.pause() and client.resume().
In both cases, the log says that the Polar device does not support the data type PolarDataType.ecg. See Connect to the sensor.
The example keeps working, because it creates a new study at every start. The message comes from a known issue in version 2.1.0: CAMS cannot read back the stored registration of a Polar sensor. See Restore the binding after a restart.
Next steps
Connecting a device
Bind a Polar sensor, connect to it and follow its status.
Measure types
The seven Polar measure types, their sample rates and data fields.
Data managers
Store the measurements on the phone or upload them to a server.