# BLE client can't connect multi BLE server device

**URL:** <https://forum.amebaiot.com/t/ble-client-cant-connect-multi-ble-server-device/2557>\
**Category:** BT\
**Created:** [December 22, 2023, 8:24am UTC](https://forum.amebaiot.com/t/ble-client-cant-connect-multi-ble-server-device/2557 "2023-12-22T08:24:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yithwe](https://avatars.discourse-cdn.com/v4/letter/y/9fc348/32.png) [@yithwe](https://forum.amebaiot.com/u/yithwe)\
**Post date:** [December 22, 2023, 8:24am UTC](https://forum.amebaiot.com/t/ble-client-cant-connect-multi-ble-server-device/2557/1 "2023-12-22T08:24:38Z")

</div>

Hi Supporter  
I use Ameba26 board, BLE setting as Central type and try to connect to other BLE Peripheral device, it’s only one connection success if setting (BLE.beginCentral(1)), I try to set BLE Cnetral argument(BLE.beginCentral(3)) is also fail, I saw the config for link number(#define BLE\_CENTRAL\_APP\_MAX\_LINKS 3) in BLEDevice.h file.If I want to connect more than one Peripheral device, which argument I need to config?Thanks.

---

<div class="post-metadata">

**Author:** ![M-ichae-l](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/m-ichae-l/32/33_2.png) [@M-ichae-l](https://forum.amebaiot.com/u/M-ichae-l)\
**Post date:** [January 29, 2024, 5:17am UTC](https://forum.amebaiot.com/t/ble-client-cant-connect-multi-ble-server-device/2557/2 "2024-01-29T05:17:54Z")

</div>

for more info about BLE please refer to  
`https://www.amebaiot.com/en/rtl8722dm-arduino-online-api-documents/`

---

<div class="post-metadata">

**Author:** ![yithwe](https://avatars.discourse-cdn.com/v4/letter/y/9fc348/32.png) [@yithwe](https://forum.amebaiot.com/u/yithwe)\
**Post date:** [January 30, 2024, 2:00am UTC](https://forum.amebaiot.com/t/ble-client-cant-connect-multi-ble-server-device/2557/3 "2024-01-30T02:00:46Z")

</div>

Hi Sir  
For this problem we can’t success to connect more than one bluetooth device, we use this web side API to try to connect, as below 8720DF datasheet it support 3+1 BLE link, for this case testing code as below, we only success for one BLE connection, what I miss for this multi connection flow?Thanks for your support.

 ![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/2X/a/a2c4785ede5b40dfe360b7b203c24fe8a49917d0.jpeg)

/\*

Example guide:  
[BLE – Battery Client – Realtek IoT/Wi-Fi MCU Solutions](https://www.amebaiot.com/en/amebad-arduino-ble-battery-client/)  
\*/

#include “BLEDevice.h”

#define BATT\_SERVICE\_UUID “180F”  
#define BATT\_LEVEL\_UUID “2A19”  
#define jj\_MAXCONN 2

BLEAdvertData foundDevice;  
BLEAdvertData targetDevice[jj\_MAXCONN];  
BLEClient\* client\_0;  
BLEClient\* client\_1;  
BLERemoteService\* battService\_0;  
BLERemoteService\* battService\_1;  
BLERemoteCharacteristic\* battChar\_0;  
BLERemoteCharacteristic\* battChar\_1;

bool notifyState = false;  
int8\_t connID[jj\_MAXCONN];  
int8\_t connOffset=0;  
int8\_t i;

void scanCB(T\_LE\_CB\_DATA\* p\_data) {  
foundDevice.parseScanInfo(p\_data);  
if (foundDevice.hasName()) {  
if (foundDevice.getName() == String(“Nordic\_HTS”)){ //AMEBA\_BLE\_DEV")) {  
Serial.print("Found Nordic\_HTS BLE Device at address ");  
Serial.println(foundDevice.getAddr().str());  
targetDevice[connOffset] = foundDevice;  
if(++connOffset \>=jj\_MAXCONN){  
connOffset=jj\_MAXCONN-1; //fix connOffset to 2 (max of connOffset)  
Serial.print(“connection number over MAXCONN\_jj”);  
}

```
    }
}
uint8_t serviceCount = foundDevice.getServiceCount();
    if (serviceCount > 0) {
    BLEUUID* serviceList = foundDevice.getServiceList();
        for (uint8_t i = 0; i < serviceCount; i++) {
            if (serviceList[i] == BLEUUID("180F")) {
            Serial.print("Found Battery Service at address ");
            Serial.println(foundDevice.getAddr().str());
            Serial.print("Connection number ");
            Serial.println(connOffset);
        }
    }
}

```

}

void notificationCB (BLERemoteCharacteristic\* chr, uint8\_t\* data, uint16\_t len) {  
Serial.print("Notification received for chr UUID: “);  
Serial.print(chr-\>getUUID().str());  
if (len == 1) {  
Serial.print(” Battery level: ");  
Serial.println(data[0]);  
}  
}  
void notificationCB\_1 (BLERemoteCharacteristic\* chr, uint8\_t\* data, uint16\_t len) {  
Serial.print("Notification received for chr UUID: “);  
Serial.print(chr-\>getUUID().str());  
Serial.print(chr-\>getUUID().str());  
if (len == 1) {  
Serial.print(” Battery level: ");  
Serial.println(data[0]);  
}  
}

void setup() {  
Serial.begin(115200);

```
BLE.init();
BLE.configScan()->setScanMode(GAP_SCAN_MODE_ACTIVE);
BLE.configScan()->setScanInterval(500); // Start a scan every 500ms
BLE.configScan()->setScanWindow(250); // Each scan lasts for 250ms
BLE.setScanCallback(scanCB);
BLE.beginCentral(3);

BLE.configScan()->startScan(20000); // Scan for 20 seconds, then stop
for(i=0;i<=connOffset;i++){
  BLE.configConnection()->connect(targetDevice[i], 2000);
  delay(3000);
  connID[i] = BLE.configConnection()->getConnId(targetDevice[i]);
}

if (!BLE.connected(connID[0])&&!BLE.connected(connID[1])) {
    Serial.println("BLE not connected");
} else if (BLE.connected(connID[0])){
    BLE.configClient();
    client_0 = BLE.addClient(connID[0]);
    client_0->discoverServices();
    Serial.print("Discovering services of connected device");
    do {
        Serial.print(".");
        delay(1000);
    } while (!(client_0->discoveryDone()));
    Serial.println();

    battService_0 = client_0->getService(BATT_SERVICE_UUID);
    if (battService_0 != nullptr) {
        battChar_0 = battService_0->getCharacteristic(BATT_LEVEL_UUID);
        if (battChar_0 != nullptr) {
            Serial.println("Battery level characteristic found");
            battChar_0->setNotifyCallback(notificationCB);
        }
    }

}
else if (BLE.connected(connID[1])){
    client_1 = BLE.addClient(connID[1]);
    client_1->discoverServices();
    Serial.print("Discovering services of connected device");
    do {
        Serial.print(".");
        delay(1000);
    } while (!(client_1->discoveryDone()));
    Serial.println();

    battService_1 = client_1->getService(BATT_SERVICE_UUID);
    if (battService_1 != nullptr) {
        battChar_1 = battService_1->getCharacteristic(BATT_LEVEL_UUID);
        if (battChar_1 != nullptr) {
            Serial.println("Battery level characteristic found");
            battChar_1->setNotifyCallback(notificationCB_1);
        }
    }

}    

```

}

void loop() {  
if (BLE.connected(connID[0])||BLE.connected(connID[1])) {  
delay(5000);  
Serial.print("Battery level read: ");  
if (BLE.connected(connID[0])){  
Serial.println(battChar\_0-\>readData8());  
}  
if (BLE.connected(connID[1])){  
Serial.println(battChar\_1-\>readData8());  
}  
delay(5000);  
notifyState = !notifyState;  
if (notifyState) {  
Serial.println(“Notifications Enabled”);  
battChar\_0-\>enableNotifyIndicate();  
battChar\_1-\>enableNotifyIndicate();  
} else {  
Serial.println(“Notifications Disabled”);  
battChar\_0-\>disableNotifyIndicate();  
battChar\_1-\>disableNotifyIndicate();  
}  
}  
else {  
Serial.println(“BLE not connected”);  
delay(5000);  
}  
}
