Engineering

Collecting ECG on iOS in Real-Time

Author avatar

Jakub Domaszewicz

Updated onDecember 3, 2024

Collecting ECG on iOS in Real-Time

Turn an iPhone into a ECG monitor with the Aidlab iOS SDK.

Want to capture ECG data from your Aidlab on iPhone? The current Aidlab Apple SDK exposes ECG through DeviceDelegate.didReceiveECG(_:timestamp:value:) after you discover, connect, and start collecting .ecg.

Note: This tutorial assumes you are familiar with Xcode and Swift Package Manager.

Install the SDK

Add the Swift package to your app:

https://github.com/Aidlab/aidlab-apple-sdk

Then add the Bluetooth usage description to your app's Info.plist:

<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app connects to Aidlab over Bluetooth.</string>

Scan and connect

Create a controller or manager that owns AidlabManager. The manager reports discovered devices through AidlabManagerDelegate.didDiscover(_:). This excerpt shows the ECG path; because DeviceDelegate covers every supported signal, your full class must also provide no-op implementations for callbacks you do not use, as shown in the SDK example app.

import Aidlab
import UIKit

final class ECGViewController: UIViewController, AidlabManagerDelegate, DeviceDelegate {
    private lazy var aidlabManager = AidlabManager(delegate: self)
    private var connectedDevice: Device?

    override func viewDidLoad() {
        super.viewDidLoad()
        aidlabManager.scan(scanMode: .aggressive)
    }

    func didDiscover(_ device: Device) {
        aidlabManager.stopScan()
        connectedDevice = device
        device.connect(delegate: self)
    }

    func didConnect(_ device: Device) {
        device.collect(dataTypes: [.ecg], dataTypesToStore: [])
    }

    func didReceiveECG(_ device: Device, timestamp: UInt64, value: Float) {
        print("ECG @ \(timestamp): \(value)")
    }

    func didDisconnect(_ device: Device, reason: DisconnectReason) {
        connectedDevice = nil
        print("Disconnected: \(reason)")
    }

    func didReceiveError(_ device: Device, error: AidlabError) {
        print("Aidlab error: \(error)")
    }
}

DeviceDelegate contains callbacks for all supported signals, so a real app must implement the remaining protocol methods as well. The full Apple example app in the SDK repository shows a complete delegate implementation.

Wear Aidlab on the chest before collecting ECG. If the device is loose or detached, use wearStateDidChange(_:wearState:) to update your UI and ask the user to adjust the strap.

The received values are ECG samples measured in volts. For sample rate and measurement details, see Measurements range and accuracy.


Back to Blog

READ ALSO

Aidlab™ is a registered trademark. Copyright © 2026