1

This is my setup.

  1. An ESP32 running as BLE Server
  2. An nRF52840 mbed board (XIAO BLE Sense) running as BLE Client

The problem is on the Client

Everything seems to work apart for the Disconnected event...

This is the code I almost copy/pasted from the documentation:

  // BLE initialization
  if (!BLE.begin()) {
    Serial.println("Starting Bluetooth® Low Energy module failed!");
    while (1);
  }
  MAC = BLE.address().c_str();
  Serial.print("BLE MAC: "); Serial.println(MAC.c_str());
  BLE.setEventHandler(BLEConnected, bleCentralConnectHandler);
  BLE.setEventHandler(BLEDisconnected, bleCentralDisconnectHandler);

Here there are the actual callback functions:

void bleCentralConnectHandler(BLEDevice peripheral) {
  // central connected event handler
  Serial.print("Connected event, peripheral: ");
  Serial.println(peripheral.address());
}
void bleCentralDisconnectHandler(BLEDevice peripheral) {
  // central disconnected event handler
  Serial.print("Disconnected event, peripheral: ");
  Serial.println(peripheral.address());
  startScanning();
}

I let them connect and then powering off the ESP32... I expected to see "Disconnected event, peripheral:...." but nothing shows up...

P.S. The connected event fires correctly

What could be ?

2 Answers2

2

I did not found any solution, so I went back to old school "ping".

I made this function:

void heartBeat()  {
  // If is not passed at least 1 second exit function
  if (millis() < last_beat + 950) return;
    
  digitalWrite(LEDR, HIGH); // Turn off
  delay(50);
  digitalWrite(LEDR, LOW); // Turn on
  last_beat = millis();
  
  if ((!BLE.connected()) && (BLE_Connected == true)) {
    ConnectionLost();
  }
}

and I call it in the loop() it checks every second if the BLE is still connected, if not It call another function, in this case ConnectionLost();

1

You need to put BLE.poll() in the loop for callbacks to work, took me a while to find that as well.

Kunal Kalwar
  • 685
  • 6
  • 20
Brian C
  • 11
  • 1