> ## Documentation Index
> Fetch the complete documentation index at: https://docs.edgeimpulse.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Android data collector

> Collect phone, camera, Wear OS, BLE, and USB OTG sensor data on Android and upload it to your Edge Impulse project.

The Edge Impulse Data Collector Android app uploads data from four kinds of source to your Edge Impulse project: phone accelerometer and PPG sensors and the camera, a companion Zephyr device over BLE, a paired Wear OS watch, and Arduino boards over USB OTG. Use it to build a dataset, then train a model and deploy it with the other tutorials in this series.

![Android Data Collector Collect tab with sensor selection, project connection fields, and logging controls](https://mintcdn.com/edgeimpulse/cbBSz84gGrhqq2EJ/.assets/images/android/data-collector-collect.png?fit=max\&auto=format\&n=cbBSz84gGrhqq2EJ\&q=85\&s=ac2677fe75ec7280b18b684106255ece)

![Zephyr BLE tab listing an EI-Monitor device and controls for labeled IMU capture](https://mintcdn.com/edgeimpulse/cbBSz84gGrhqq2EJ/.assets/images/android/data-collector-zephyr-ble.png?fit=max\&auto=format\&n=cbBSz84gGrhqq2EJ\&q=85\&s=73878e863c525ae84f0d49174974e78b)

![WearOS tab combining watch, phone, and Nesso N1 streams with voice control status](https://mintcdn.com/edgeimpulse/cbBSz84gGrhqq2EJ/.assets/images/android/data-collector-wearos.png?fit=max\&auto=format\&n=cbBSz84gGrhqq2EJ\&q=85\&s=995da2832e399c68f109b27723957322)

<video controls width="100%">
  <source src="https://github.com/user-attachments/assets/db2ade2c-ef3d-49ef-b849-49de9445de4d" />
</video>

The video shows accelerometer data collected on the phone and uploaded as a labeled sample in the studio.

> [card] [Download pre-built APK](https://github.com/edgeimpulse/example-android-inferencing/blob/main/android-data-collector/sample-apk/edge-impulse-data-collector.apk)
>
> Sideload the app onto a device running Android 9.0 (API 28) or later, without building it. To build from source, follow the steps below.

## What you'll build

An Android app with four tabs:

| Tab | What it does |
| - | - |
| **Collect** | Records phone accelerometer or PPG (heart rate) data and camera images, uploads them with a label, and logs to CSV when you're offline |
| **Zephyr BLE** | Connects to a Nordic Thingy:53 or Nesso N1 running the [`ei-zephyr-ble-gatt-client`](https://docs.edgeimpulse.com/tutorials/topics/zephyr/zephyr-ble-gatt-client) firmware; receives raw IMU windows and inference results over BLE GATT and uploads them to your project |
| **WearOS** | Relays heart rate, accelerometer, and GPS from a paired Wear OS watch via the Wearable Data Layer API |
| **USB OTG** | Reads IMU data, and camera frames on supported boards, from an Arduino connected over USB OTG, with no BLE or Wi-Fi |

## Prerequisites

* An [Edge Impulse account](https://edgeimpulse.com/signup)
* An Edge Impulse project and API key
* Android Studio (Ladybug 2024.2.2 or later)
* An Android device running Android 9.0 (API 28) or later. Voice commands use on-device speech recognition on API 31 and later
* For the Zephyr BLE tab: a device flashed with [`ei-zephyr-ble-gatt-client`](https://docs.edgeimpulse.com/tutorials/topics/zephyr/zephyr-ble-gatt-client)
* For the WearOS tab: a paired Wear OS watch with the companion module installed
* For the USB OTG tab: a [supported Arduino board](#supported-boards-and-sketches) flashed with the matching sketch from `sample-arduino/`, and a USB OTG adapter for your phone

## 1. Clone the repository

```bash theme={"system"}
git clone https://github.com/edgeimpulse/example-android-inferencing.git
cd example-android-inferencing/android-data-collector
```

## 2. Get your API key

1. Open your project in [Edge Impulse Studio](https://studio.edgeimpulse.com).
2. Go to **Dashboard** -> **Keys**.
3. Click **Add new API key** and copy it (`ei_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`).

## 3. Configure the API key

Add your key to `~/.gradle/gradle.properties`, which keeps it out of source control:

```properties theme={"system"}
EI_API_KEY=ei_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

Alternatively, add it to the local `gradle.properties` in the project root, but do **not** commit that file.

> [!WARNING]
>
> Never hard-code API keys in source code or commit them to version control.

## 4. Build and install

```bash theme={"system"}
./gradlew :app:assembleDebug
adb install app/build/outputs/apk/debug/app-debug.apk
```

You can also open the project in Android Studio and run it on a connected device.

### Install the Wear OS companion (optional)

To use the **WearOS** tab, install the `wearosdatalogger` module on your watch over Wi-Fi with `adb`:

1. On the watch, go to **Settings** -> **Developer options** and turn on **Wireless debugging**.

2. Tap **Pair new device** and note the **IP:port** and the **6-digit pairing code** displayed.

3. On your computer, connect to the same Wi-Fi network as the watch (not a guest or isolated network), turn off any VPN, and run:

   ```bash theme={"system"}
   adb pair <WATCH_IP>:<PAIRING_PORT> <CODE>
   adb connect <WATCH_IP>:5555
   adb devices -l
   ```

4. Build and install the watch APK, targeting the watch by serial:

   ```bash theme={"system"}
   ./gradlew :wearosdatalogger:assembleDebug
   WATCH=$(adb devices | awk '/device$/ && !/emulator/' | sed -n '2p' | cut -f1)
   adb -s "$WATCH" install -r wearosdatalogger/build/outputs/apk/debug/wearosdatalogger-debug.apk
   ```

   Or build and install in one command: `ANDROID_SERIAL=$WATCH ./gradlew :wearosdatalogger:installDebug`.

> [!WARNING]
>
> The pairing port and code work once and expire after about 60 seconds. Pass both on the `adb pair` command line rather than waiting for the interactive prompt, and use `adb` version 34 or later (`brew install --cask android-platform-tools` on macOS). If pairing fails, see [Troubleshooting](#troubleshooting).

> [!TIP]
>
> If Wi-Fi pairing keeps failing, connect through the phone instead. Turn on **Debug over Bluetooth** on the watch and in the Wear OS companion app on the phone, then run `adb forward tcp:4444 localabstract:/adb-hub && adb connect 127.0.0.1:4444`.

Launch **WearOS Data Logger** on the watch and open the **WearOS** tab in the phone app. The watch appears and starts streaming heart rate, accelerometer, and GPS data.

## 5. Collect sensor data

1. Open the app and go to the **Collect** tab.
2. Enter an **Edge Impulse label**, for example `walking`, `idle`, or `running`. The sample gets this label in your project.
3. Select a **Sensor**: `Accelerometer` or `PPG (Heart Rate)`.
4. Tap **Start**. The live sensor values appear on screen.
5. Tap **Stop**. The sample uploads to your project.
6. Repeat with different labels to build a balanced dataset.

> [!NOTE]
>
> Aim for at least 2 to 3 minutes of data per class. The studio splits recordings into windows when you create your impulse.

## 6. Capture images

On the same **Collect** tab:

1. Enter a label, for example `crack` or `no_crack`.
2. Tap **Capture & upload image**. The rear camera takes a photo and uploads it with that label.
3. Repeat to build an image dataset for classification or object detection.

## 7. Offline logging

If you have no data connection, log samples on the device and upload them later:

1. Enable **Log samples to CSV on device**.
2. Tap **Start** and **Stop** as usual. The samples are saved to a local CSV file.
3. When you're back online, tap **Upload stored CSV to Edge Impulse** to upload them all.

## 8. Multi-modal capture and auto source priority

The **Multi-modal capture** card on the **Collect** tab records from connected sources for a fixed duration and uploads each stream with the same label. By default, the app uses the highest-priority source that's connected:

| Priority | Source | Active when |
| - | - | - |
| 1 | Zephyr BLE (Nesso N1 / Nano 33 BLE / Thingy:53) | Connected on the **Zephyr BLE** tab |
| 2 | Wear OS watch IMU + heart rate | Companion app installed and node visible on the **WearOS** tab |
| 3 | Phone accelerometer / PPG | Always available as a fallback |

Turn on **Camera snapshot** to also take one photo at the start of each capture. The app uploads the photo from memory without writing it to disk.

## 9. Voice control ("Hey Android")

You can start and label captures by voice, for example when the phone is strapped to a wrist, helmet, or dashboard. The app combines a `hey_android` wake word model trained in Edge Impulse with Android's `SpeechRecognizer`.

1. Tap the microphone icon in the top app bar and grant the `RECORD_AUDIO` permission. The icon turns blue and a status banner appears under the tab bar.
2. Say "Hey Android". Wake word detection pauses and the speech recognizer listens for a command.
3. Say a command in the form "capture N seconds with label LABEL", for example:

   * *"capture 5 seconds with label idle"*
   * *"record 10 seconds with label updown"*
   * *"start 20 seconds with label circle"*

   The verb can be `capture`, `record`, or `start`. The duration can be digits or a spelled-out number up to sixty. The parser recognizes the labels `idle`, `updown` (also "up down"), and `circle`. To add labels, edit `voice/VoiceCommandParser.kt`.
4. The app records from the highest-priority connected source and uploads the sample with the label you said. Wake word detection resumes about 300 ms after the capture ends.

> [!NOTE]
>
> The wake word model runs on the device with the Edge Impulse C++ SDK and needs no network. Speech recognition uses Android's on-device recognizer on API 31 and later, and falls back to the network recognizer on devices without one.

## 10. Connect a Zephyr device (optional)

The **Zephyr BLE** tab scans for a device that advertises as `EI-Monitor` and runs the [`ei-zephyr-ble-gatt-client`](https://docs.edgeimpulse.com/tutorials/topics/zephyr/zephyr-ble-gatt-client) firmware.

What the firmware sends depends on the board. Each board's `.conf` file sets `CONFIG_EI_SENSOR_LOCAL`:

| Board | Build target | Mode | What it sends |
| - | - | - | - |
| Arduino Nesso N1 | `arduino_nesso_n1` | Local sensor (ESP32-C6 + BMI270) | Raw IMU + optional on-device inference |
| Arduino Nano 33 BLE Sense | `arduino_nano_33_ble` | Local sensor (nRF52840 + LSM9DS1) | Raw IMU + optional on-device inference |
| Nordic Thingy:53 | `thingy53_nrf5340_cpuapp` | Relay | Inference + raw IMU relayed from an `EI-Golioth` peripheral |

1. Flash the firmware to your board. See the [BLE GATT client tutorial](https://docs.edgeimpulse.com/tutorials/topics/zephyr/zephyr-ble-gatt-client.md).
2. Open the **Zephyr BLE** tab.
3. Tap **Scan for EI-Monitor**. The device appears in the list.
4. Tap the device to connect. The banner turns green.

![Zephyr BLE tab connected to EI-Monitor with inference results displayed](https://mintcdn.com/edgeimpulse/cbBSz84gGrhqq2EJ/.assets/images/android/data-collector-project-connected.png?fit=max\&auto=format\&n=cbBSz84gGrhqq2EJ\&q=85\&s=00ed356318440e9ec1dc935b4f11b375)

Connected to EI-Monitor with inference results streaming in

5. The app uploads raw IMU windows and labeled inference results to your project as they arrive.

> [!NOTE]
>
> The app uploads an inference result only after it receives at least one raw sensor window on the same connection. If a result appears on screen but doesn't upload, check that the firmware exposes the sensor characteristic. The app subscribes to it when it connects.

The app decodes the 52-byte `inference_result_t` binary notification from the firmware:

| Field | Type | Offset |
| - | - | - |
| `label` | `char[32]` | 0 |
| `confidence` | `float` | 32 |
| `dsp_time_ms` | `uint32_t` | 36 |
| `classification_time_ms` | `uint32_t` | 40 |
| `timestamp` | `uint64_t` | 44 |

## 11. Connect a USB OTG Arduino device

The **USB OTG** tab makes the phone a USB host that reads CDC-ACM serial data from any Arduino that implements the wire protocol below. It needs no BLE, Wi-Fi, or network connection.

### Supported boards and sketches

All sketches are in the `sample-arduino/` directory of the repository.

| Board | Sketch | Sensors | Camera |
| - | - | - | - |
| Arduino Nano 33 BLE Sense Rev1 or Rev2 + TinyML Kit | `nano33ble_ai_kit_otg/nano33ble_ai_kit_otg.ino` | LSM9DS1 (Rev1) / BMI270+BMM150 (Rev2) | OV7675 QQVGA |
| Espressif ESP32-S3-EYE / ESP32-EYE / ESP32-CAM | `esp32_eye_otg/esp32_eye_otg.ino` | MPU-6050 (S3-EYE built-in) | OV2640 |
| Arduino UNO R4 WiFi / R4 Minima / UNO WiFi Rev2 / UNO classic | `uno_r4_otg/uno_r4_otg.ino` | LSM6DSOX / LSM6DS3 / MPU-6050 | None |
| Arduino Nano 33 BLE / Nano 33 IoT / generic Arduino + MPU-6050 | `nesso_usb_serial_imu/nesso_usb_serial_imu.ino` | LSM9DS1 / LSM6DS3 / MPU-6050 | None |

### Wire protocol

All sketches send the same text protocol at 115200 baud:

```text theme={"system"}
!ax,ay,az,gx,gy,gz          # column header, sent once on boot
0.12,-0.34,9.81,...         # CSV data row
# comment                   # ignored by the app
IMG:160,120,GRAYSCALE,19200 # camera frame header (camera boards only)
<raw bytes>                 # frame payload
```

The app reads the sensor axes from the `!` header and switches to image capture when it sees an `IMG:` header.

### Flash and connect

1. Open the sketch for your board in the Arduino IDE.
2. Set the `#define` at the top of the file to match your board variant.
3. Install the libraries listed in the sketch header from **Tools** -> **Manage Libraries**.
4. Upload the sketch.
5. Connect the board to the phone through a USB OTG adapter. For boards without native USB, such as the UNO classic and ESP32-CAM, use an FTDI, CP2102, or CH340 USB-serial adapter.
6. Open the **USB OTG** tab in the app. The app detects the board and starts streaming IMU data.

### Nano 33 BLE Sense + TinyML Kit (IMU + camera)

The `nano33ble_ai_kit_otg` sketch adds camera capture from the OV7675 on the Arduino TinyML Kit shield. IMU streaming starts on boot. Control the camera with these serial commands, sent from the **USB OTG** tab or any serial monitor at 115200 baud:

| Command | Action |
| - | - |
| `s` | Start / resume IMU streaming |
| `p` | Pause IMU streaming |
| `c` | Capture one QQVGA frame and send it over USB |
| `g` | Toggle pixel format between GRAYSCALE and RGB565 |
| `r` | Resume IMU streaming after a camera capture |
| `?` | Print IMU and camera status |

Frame sizes: 19 200 bytes (GRAYSCALE 160 × 120) or 38 400 bytes (RGB565 160 × 120).

> [!NOTE]
>
> The Nano 33 BLE connects as a CDC-ACM device, so it doesn't need a USB-serial adapter. Use `BOARD_NANO33BLE` for Rev1 (LSM9DS1) or uncomment `BOARD_NANO33BLE_REV2` for Rev2 (BMI270 + BMM150). To set the pixel format, edit the `CAM_FORMAT` define or send `g` at runtime.

## 12. Verify your data in Edge Impulse Studio

1. Open your project in [studio.edgeimpulse.com](https://studio.edgeimpulse.com).
2. Go to **Data acquisition**. Your samples appear with their labels, timestamps, and sensor axes.
3. Check the split between training and test data.
4. Go to **Create impulse** to design and train your model.

## How it works

![Architecture diagram connecting phone sensors, BLE, Wear OS, USB OTG, local logging, and Edge Impulse APIs](https://github.com/user-attachments/assets/b3c6455c-9d41-4432-8a0f-219157ebbd22)

Data collector system architecture

The app collects data from every connected source, buffers it in CSV files, and uploads it with the ingestion API. All sources go through one `DataRepository`, so every upload takes the same path.

```mermaid theme={"system"}
flowchart TD
    subgraph UI["MainActivity (Compose)"]
        direction LR
        C[Collect]
        Z[ZephyrBLE]
        W[WearOS]
        U[USB OTG]
    end
    UI --> VM[SensorViewModel]
    VM --> SC["SensorCollector\nphone IMU / PPG"]
    VM --> ZB["ZephyrBLEClient\nBLE: Thingy:53 / Nesso N1"]
    VM --> CH["CameraHelper\nCameraX JPEG"]
    VM --> EM["EdgeImpulseManager\nremote-mgmt WebSocket"]
    VM --> UO["UsbOtgClient\nUSB OTG: Arduino CDC-ACM"]
    SC --> DR["DataRepository\nCSV log · HTTPS ingestion · image upload"]
    ZB --> DR
    CH --> DR
    UO --> DR
    EM --> EI
    DR --> EI["Edge Impulse APIs\ningestion + remote-mgmt"]
```

### Data flows

| Source | Format | Edge Impulse endpoint |
| - | - | - |
| Phone accelerometer / PPG | `IngestionPayload` JSON | `POST /api/training/data` |
| Camera JPEG | binary `image/jpeg` | `POST /api/training/data` |
| Zephyr inference result | JSON, `x-label` = inferred class | `POST /api/training/data` |
| Zephyr raw IMU | Buffered CSV, flushed on each inference | `POST /api/training/data` |
| USB OTG IMU stream | Buffered CSV, flushed on each label | `POST /api/training/data` |
| USB OTG camera frame | binary `image/jpeg` or raw bytes | `POST /api/training/data` |
| Studio remote trigger | WebSocket `wss://remote-mgmt.edgeimpulse.com` | stream |

## Permissions

The app requests these permissions on first launch. If you deny one, only the feature that needs it stops working.

| Permission | Required by |
| - | - |
| `CAMERA` | Capture & upload image |
| `BODY_SENSORS` | PPG / heart rate |
| `ACCESS_FINE_LOCATION` | BLE scanning on Android \< 12 |
| `BLUETOOTH_SCAN` / `BLUETOOTH_CONNECT` | Zephyr BLE tab (Android 12+) |
| `RECORD_AUDIO` | Voice control ("Hey Android" wake word + on-device speech-to-text command parsing) |
| USB host permission | USB OTG tab. Granted when you plug in the board |

## Troubleshooting

> [accordion] Uploads fail with 401 Unauthorized
>
> Your API key is missing or wrong. Check that `EI_API_KEY` is set in `~/.gradle/gradle.properties` and rebuild the app. The key must start with `ei_`.

> [accordion] Zephyr BLE device not appearing in scan
>
> * Check that the device is advertising. In a serial monitor, look for `Advertising as EI-Monitor`.
> * On Android 12 and later, grant `BLUETOOTH_SCAN` and `BLUETOOTH_CONNECT` in **Settings** -> **Apps** -> **Edge Impulse Data Collector** -> **Permissions**.
> * On earlier Android versions, grant `ACCESS_FINE_LOCATION`, which Android requires for BLE scanning.
> * Check that Bluetooth is on and the device is within about 10 m.

> [accordion] BLE connects but no data arrives
>
> * Check the Zephyr serial log for `BLE GATT server started`.
> * Check that the firmware's GATT service UUID matches the one in `GattProfile.kt`: `12345678-1234-5678-1234-56789abcdef0`.
> * Disconnect and reconnect. The notification subscription sometimes fails on the first connection.

> [accordion] Camera capture does nothing
>
> Grant the `CAMERA` permission in **Settings** -> **Apps** -> **Edge Impulse Data Collector** -> **Permissions** and try again. If your build is older than 25 May 2026, pull the latest source, which fixes dropped camera frames.

> [accordion] USB OTG device not detected
>
> * Check that you're using an OTG adapter and that the cable is seated at both ends.
> * Some phones don't support USB host mode on their USB-C port. Try another OTG adapter or another phone.
> * Let the Arduino finish booting the sketch before you plug it in.
> * The app includes drivers for CP2102, CH340, and FTDI FT232 USB-serial chips. It might not recognize other chips.

> [accordion] USB OTG streams IMU but camera capture does nothing
>
> * Check that you uploaded `nano33ble_ai_kit_otg.ino` or `esp32_eye_otg.ino`. The other sketches send IMU data only.
> * Tap the camera icon on the **USB OTG** tab, or send `c`, to capture a frame. The tab doesn't stream camera frames continuously.
> * Check that the OV7675 module is seated in the TinyML Kit shield connector.
> * Open a serial monitor at 115200 baud. A `# ERROR: camera not available` line means the camera failed to start.

> [accordion] Offline CSV upload fails
>
> Fill in the **Edge Impulse label** field before you tap **Upload stored CSV to Edge Impulse**. The CSV file doesn't store labels.

> [accordion] WearOS tab shows no data
>
> [Install the Wear OS companion](#install-the-wear-os-companion-optional) on your watch, then pair the watch again in the Wear OS app.

> [accordion] adb pair fails with 'protocol fault (couldn't read status message)'
>
> The pairing code and port work once and expire quickly. Close **Pair new device** on the watch, reopen it to get a new address and code, and pass both on the command line:
>
> ```bash theme={"system"}
> adb kill-server && adb start-server
> adb pair <WATCH_IP>:<NEW_PORT> <NEW_CODE>
> ```
>
> If `nc -zv <WATCH_IP> <NEW_PORT>` succeeds but `adb pair` still fails, the code has probably expired, so get a new one. If the port is unreachable, the watch and computer are on different networks, or a VPN or firewall is blocking the connection.

## Next steps

* Create an impulse with a processing block, for example spectral analysis for IMU data, and a learning block, then train it.
* Run the trained model on Android with [static buffer inference](https://docs.edgeimpulse.com/tutorials/topics/android/static-buffer-inference.md), [keyword spotting](https://docs.edgeimpulse.com/tutorials/topics/android/keyword-spotting.md), or [camera inference](https://docs.edgeimpulse.com/tutorials/topics/android/camera-inference.md).
* Speed up inference on Snapdragon devices with [QNN hardware acceleration](https://docs.edgeimpulse.com/tutorials/topics/android/qnn-acceleration.md).

## Resources

* [Repository: android-data-collector](https://github.com/edgeimpulse/example-android-inferencing/tree/main/android-data-collector)
* [Companion firmware: ei-zephyr-ble-gatt-client](https://docs.edgeimpulse.com/tutorials/topics/zephyr/zephyr-ble-gatt-client.md)
* [Ingestion API reference](https://docs.edgeimpulse.com/apis/ingestion.md)
* [Android Bluetooth LE guide](https://developer.android.com/develop/connectivity/bluetooth/ble/ble-overview)
