Open
Description
ArduinoBLE library supports up to ATT_MAX_PEERS
multiple concurrent connections.
Lines 29 to 35 in 8a49b8c
However, using the latest versions of the nano33ble core there are problems when trying to handle multiple connections.
- With version 1.1.3 of the core arduino/ArduinoCore-nRF528x-mbedos@67ef327 the board does not present any problems.
- With version 1.1.4 arduino/ArduinoCore-nRF528x-mbedos@f4ddc96 and following, if the nano33ble board works as a central device, then it cannot connect to multiple peripherals. On the other hand, if it works as a peripheral device, then it is able to handle multiple central devices connected to it.
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);
}
}
}