Skip to content

[nano33ble] Multiple concurrent connections #108

Open
@polldo

Description

@polldo

ArduinoBLE library supports up to ATT_MAX_PEERS multiple concurrent connections.

#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
#define ATT_MAX_PEERS 7
#elif DM_CONN_MAX
#define ATT_MAX_PEERS DM_CONN_MAX // Mbed + Cordio
#else
#define ATT_MAX_PEERS 3
#endif

However, using the latest versions of the nano33ble core there are problems when trying to handle multiple connections.

SETUP:
ArduinoBLE version: 1.1.3-10-g89fd740 89fd740 , in branch 'multi-connection' https://github.com/arduino-libraries/ArduinoBLE/tree/multi-connection

Test sketches

  • Central, connecting to more peripherals:
#include <ArduinoBLE.h>

void setup() {
  Serial.begin(115200);
  while(!Serial);
  BLE.begin();
  BLE.debug(Serial);
  BLE.scanForName("peripheral");
}

void connectionLoop() 
{
  // If not already connected to 2 peripherals, try to connect to a new found peripheral.
  if (BLE.peripheralCount() < 2) {

    BLEDevice peripheral = BLE.available();
    if (peripheral) {
      BLE.stopScan();

      if (!peripheral.connect()) {
        Serial.println("Failed to connect!");
        return;
      }

      if (!peripheral.discoverAttributes()) {
        Serial.println("Attribute discovery failed!");
        peripheral.disconnect();
        return;
      }
    }
  }
}

auto timeRef = millis();
void loop() {
  connectionLoop();

  if (millis() - timeRef >= 5000) {
    timeRef = millis();

    int periphCount = BLE.peripheralCount();
    Serial.print(" Peripheral connected: ");
    Serial.println(periphCount);

    if (periphCount < 2) {
      BLE.scanForName("peripheral");
    }

    // Loop through all connected peripherals
    for (int periphIdx = 0; periphIdx < periphCount; periphIdx++) {
      BLEDevice peripheral = BLE.peripheral(periphIdx);
      if (peripheral) {
        BLECharacteristic batteryLevelChar = peripheral.characteristic("2A19");

        if (!batteryLevelChar) {
          Serial.println("Peripheral does not have battery level characteristic!");
          peripheral.disconnect();
        } else {
          Serial.print("Peripheral connected, value: ");
          batteryLevelChar.read();
          Serial.println(*batteryLevelChar.value());
        }
      }
    }
  }
}
  • Peripherals:
#include <ArduinoBLE.h>

BLEService batteryService("180F");
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLEWrite); 

void setup() {
  Serial.begin(115200);    
  pinMode(LED_BUILTIN, OUTPUT); 
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  // Configure peripheral
  BLE.setLocalName("peripheral");
  BLE.setAdvertisedService(batteryService); 
  batteryService.addCharacteristic(batteryLevelChar); 
  BLE.addService(batteryService);
  BLE.advertise();

  Serial.println("peripheral configured - central connected: 0");
}

auto timeRef = millis();
void loop() {
  static uint8_t battery = 0;
  BLE.poll();
  if (millis() - timeRef >= 3000) {
    timeRef = millis();
    if (BLE.centralCount() > 0) {
      digitalWrite(LED_BUILTIN, HIGH);
      batteryLevelChar.writeValue(battery);
      battery++;
    } else {
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedAssistance from the community is especially welcometype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions