Arduino Function: BLECentralRole.allowMultilink




Allow the central to be connected to multiple peripherals. it's possible to set up to 7 simultaneous connections.




periphNo: uint8_t. Number of peripherals the central can be simultaneously connected to.





 Written by Chiara Ruggeri (

 This example shows the capability of a central to be connected to multiple peripherals at the same time.
 It implements led_switch_client example, but with the function allowMultilink(), it's possible to set up to 7 simultaneously connections.
 Every time BUTTON_PIN is pressed the corresponding value will be sent to all peripherals connected.

 Peripherals have to use led_switch.ino example in File->Examples->BLE->Peripheral menu.

 This example code is in the public domain.

#include <BLECentralRole.h>

// LED and button pin

// create central instance
BLECentralRole                bleCentral                            = BLECentralRole();

// create remote service with UUID compliant
BLERemoteService              remoteLedService                      = BLERemoteService("19b10010e8f2537e4f6cd104768a1214");

// create remote characteristics with UUID and properties compliant those in led_switch example
BLERemoteCharCharacteristic       remoteSwitchCharacteristic        = BLERemoteCharCharacteristic("19b10011e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
BLERemoteCharCharacteristic       remoteButtonCharacteristic        = BLERemoteCharCharacteristic("19b10012e8f2537e4f6cd104768a1214", BLERead | BLENotify);

bool buttonState;

void setup() {


 //initialize BLE led
 pinMode(BLE_LED, OUTPUT);

 pinMode(LED_PIN, OUTPUT);

 buttonState = digitalRead(BUTTON_PIN);

 // add service and characteristics

 // assign event handlers for central events
 bleCentral.setEventHandler(BLEScanReceived, receiveAdvPck);  
 bleCentral.setEventHandler(BLEConnected, bleCentralConnectHandler);
 bleCentral.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler);
 bleCentral.setEventHandler(BLERemoteServicesDiscovered, bleCentralRemoteServicesDiscoveredHandler);

 // assign event handlers for characteristic
 remoteButtonCharacteristic.setEventHandler(BLEValueUpdated, bleRemoteButtonCharacteristicValueUpdatedHandle);

 // set scan parameters
 // interval and window in 0.625 ms increments
 bleCentral.setScanInterval(3200); // 2 sec
 bleCentral.setScanWindow(800);  // 0.5 sec

 // allows the central to be connected to 2 peripherals simultaneously
 // you can choose up to 7 peripherals

 // begin initialization and start scanning

void loop() {
   // We are connected to at least one peripheral. turn BLE led on.
   // Please note that until the maximum number of allowed peripheral is reached
   // the board will be scan at the same time.
   digitalWrite(BLE_LED, HIGH);

bool buttonValue = digitalRead(BUTTON_PIN);
bool buttonChanged = (buttonState != buttonValue);

if(buttonChanged){ // send the message if the state has changed
  buttonState = buttonValue;
  unsigned char message;

  if(buttonState == LOW) // button pressed. Send 1 to turn on the led
    message = 1;

    message = 0;
  bleCentral.writeRemoteCharacteristic(remoteSwitchCharacteristic, &message, sizeof(message));

 else{ // we are not connected to any peripheral. Signal the scan again
digitalWrite(BLE_LED, LOW);
digitalWrite(BLE_LED, HIGH);

void receiveAdvPck(BLEPeripheralPeer& peer){
 char advertisedName[31];
 byte len;

 // search for a device that advertises "LED Switch" name
 peer.getFieldInAdvPck(BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME, advertisedName, len);

 if(len == 0) // field not found
  peer.getFieldInAdvPck(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, advertisedName, len);

 if(len != 0){ // the field was found

if(!strcmp(advertisedName, "LED Switch"))

  // Name found. Connect to the peripheral

void bleCentralConnectHandler(BLEPeripheralPeer& peer) {
 // peer connected event handler
 Serial.print("Connected event, peripheral: ");

void bleCentralDisconnectHandler(BLEPeripheralPeer& peer) {
 // peer disconnected event handler
 Serial.print("Disconnected event, peripheral: ");

void bleCentralRemoteServicesDiscoveredHandler(BLEPeripheralPeer& peer) {
 // peer remote services discovered event handler
 Serial.print("Remote services discovered event, peripheral: ");

 // subscribe button characteristic in order to automatically receive
 // messages sent from peripheral
 if (remoteButtonCharacteristic.canSubscribe()) {

void bleRemoteButtonCharacteristicValueUpdatedHandle(BLEPeripheralPeer& peer, BLERemoteCharacteristic& characteristic) {
 if(remoteButtonCharacteristic.value() == 0){ // button pressed
Serial.println("LED on");
digitalWrite(LED_PIN, HIGH);

Serial.println("LED off");
digitalWrite(LED_PIN, LOW);

Written by