Skip to content

Connecting to Multiple Devices

Example: stream Respiration from two devices at once.

  1. Turn on devices and wait for the pulsating green LED (ready to connect).
  2. Run the routine below.
  3. Wait for both connections to establish.

Examples

python
import asyncio
from aidlab import AidlabManager, DataType, DeviceDelegate

FIRST_ADDRESS = "<FIRST DEVICE ADDRESS>"
SECOND_ADDRESS = "<SECOND DEVICE ADDRESS>"

class MainManager(DeviceDelegate):
    async def run(self):
        devices = await AidlabManager().scan()
        first = next((d for d in devices if d.address == FIRST_ADDRESS), None)
        second = next((d for d in devices if d.address == SECOND_ADDRESS), None)
        if first:
            await first.connect(self)
        if second:
            await second.connect(self)
        while True:
            await asyncio.sleep(1)

    async def did_connect(self, device):
        await device.collect([DataType.RESPIRATION], [])

    def did_disconnect(self, device):
        print("Disconnected from:", device.address)

    def did_receive_respiration(self, device, _, values):
        print("Respiration:", values, device.address)

asyncio.run(MainManager().run())
swift
let firstDeviceAddress = "<FIRST DEVICE UUID>"
let secondDeviceAddress = "<SECOND DEVICE UUID>"

class MainManager: AidlabManagerDelegate, DeviceDelegate {
    let aidlabManager = AidlabManager(delegate: nil)
    init() { aidlabManager.delegate = self }

    func run() { aidlabManager.scan() }

    func didDiscover(_ device: Device) {
        if let addr = device.address?.uuidString,
           addr == firstDeviceAddress || addr == secondDeviceAddress {
            device.connect(delegate: self, dataTypes: [.respiration], dataTypesToStore: [])
        }
    }

    func didDisconnect(_ device: Device) {
        print("Disconnected from: \(device.address?.uuidString ?? "")")
    }

    func didReceiveRespiration(_ device: Device, timestamp: UInt64, value: Float) {
        print("Respiration: \(value) from \(device.address?.uuidString ?? "")")
    }
}

Tip: filter by address to avoid connecting to unintended devices when many are nearby.

Troubleshooting

  • Connection stability depends on the Bluetooth chipset. More than ~5 devices per dongle can get flaky.
  • In tests, each BT dongle on a Raspberry Pi held ~5 devices; total stable connections scale with the number of dongles.
  • Real-world limits vary by hardware and chipset version.