Quickstart
Build an app that connects to an eSense earbud and prints its button and motion events.
The finished app connects to an eSense earbud by its Bluetooth name and prints each button and motion measurement as JSON.
Prerequisites
- The setup in Installation, including the CARP Mobile Sensing (CAMS) setup it builds on.
- An eSense pair and the Bluetooth name of its left earbud, such as
eSense-0332. A Bluetooth scanner app, such as nRF Connect, shows the name. - A physical Android or iOS phone. Emulators and simulators cannot connect to a Bluetooth device.
Complete example
Replace the contents of lib/main.dart with this program, and set bleName to the name of your left earbud:
How it works
| Stage | What it does |
|---|---|
| 1. Register | ESenseSamplingPackage makes the eSense measure types, the ESenseDevice 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 an ESenseDevice with the role eSense is connected to it. samplingRate sets the motion data to 20 Hz. The protocol does not identify the physical earbud. |
| 3. Task | A BackgroundTask on the eSense device collects button and motion events. Both measures are event-based, so an ImmediateTrigger starts them once, and they stream while the earbud is connected. |
| 4. Bind | requestPermissions() asks for Bluetooth and, on Android, location. Setting bleName binds the eSense role to your earbud. |
| 5. Deploy | During the deployment, CAMS configures the eSense device manager and connects to the earbud, because the manager has a name. The eSense measures start when the earbud is connected. 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 earbud before it deploys the study, so CAMS connects to it as part of the deployment. To bind an earbud later, for example from a settings screen, see Connecting a device.
Run the app
Turn on the left earbud
Press and hold the button under the eSense logo until the LED turns blue, then release it. Keep the earbud near the phone.
Start the app on your phone
Allow the Bluetooth permission. On Android, also allow location access, with precise location on Android 12 or later: CAMS needs it to connect to Bluetooth devices. On iOS, connecting to the earbud may take several seconds.
Check the console
Once the earbud is connected, the console prints one motion measurement per sensor packet, at about the 20 Hz of
samplingRate:Each press and each release of the button adds a button measurement:
To work with the values in code, check the type of the data:
The motion values are raw sensor values. To convert them to g and degrees per second, see Unit conversion.
Troubleshooting
Print the status of the eSense device manager and act on it:
disconnectedright after the start: the Bluetooth permissions are missing. Allow them, and on Android 12 or later, choose precise location.disconnectedafter about 5 seconds: on Android, the plugin scans for the earbud for 5 seconds, and CAMS does not try again. Check thatbleNamematches the name of the earbud exactly and that the left earbud is on and near the phone. Also check that no other phone or app is connected to it, and then restart the app.connectingfor a long time: on iOS, the phone has not found the earbud. Check the same points.
For the code that prints the status, see Monitor the connection.
Set samplingRate to a value from 1 to 100. On Android, a rate outside this range starts no sampling in release builds.
Next steps
Connecting a device
Bind an eSense earbud, connect to it and follow its status.
Measure types
The two eSense measure types, their raw values and timestamps.
Data managers
Store the measurements on the phone or upload them to a server.