XIAO_ESP32S3 BLE.end() crashes; BLE.stopAdvertise() not working

I have several BLE problems:

In the following example the call to BLE.end(); crashes the board.
Additionally the call to BLE.stopAdvertise(); does not stop adverticing the BLE service.
In the bleOnCharacteristicStringRead function the value changed to “<123454321>” gets truncated when sent the first time.

Does somebody has suggestions to resolve the problems?

#include <ArduinoBLE.h>

BLEService bleServiceTest("776982a9-8315-4bad-80f7-325e6a521aa6");
BLECharacteristic bleCharacteristicSwitch("df172997-36a6-446c-b70c-ac1556012427", BLERead | BLEWrite, 32);

void bleOnConnected(BLEDevice bleDevice)
{
  Serial.println("bleOnConnected: bleDevice.address()=" + bleDevice.address());
  Serial.println("BLE.stopAdvertise()");
  BLE.stopAdvertise();  //no effect on XIAO_ESP32S3
}

void bleOnDisconnected(BLEDevice bleDevice)
{
  Serial.println("bleOnDisconnected: bleDevice.address()=" + bleDevice.address());
  Serial.println("BLE.stopAdvertise()");
  BLE.stopAdvertise();  //no effect on XIAO_ESP32S3
  BLE.end(); //crashes on XIAO_ESP32S3
}

void bleOnCharacteristicStringWritten(BLEDevice bleDevice, BLECharacteristic characteristic)
{
  String sWritten = String (((char*)characteristic.value()), characteristic.valueLength());
  Serial.println("bleOnCharacteristicStringWritten: " + sWritten);
}

void bleOnCharacteristicStringRead(BLEDevice bleDevice, BLECharacteristic characteristic)
{
  characteristic.setValue(String("<123454321>").c_str());
  String sRead = String (((char*)characteristic.value()), characteristic.valueLength());
  Serial.println("bleOnCharacteristicStringRead: " + sRead);
}

void setup() 
{
  Serial.begin(115200);
  while (!Serial) 
    delay(10);

  Serial.println("setup: calling BLE.begin()");
  if (!BLE.begin())
    Serial.println("BLE.begin() failed");
  BLE.setLocalName("BLE Server Test");
  BLE.setAdvertisedService(bleServiceTest);
  bleServiceTest.addCharacteristic(bleCharacteristicSwitch);
  BLE.addService(bleServiceTest);
  BLE.setEventHandler(BLEConnected, bleOnConnected);
  BLE.setEventHandler(BLEDisconnected, bleOnDisconnected);
  
  bleCharacteristicSwitch.setEventHandler(BLEWritten, bleOnCharacteristicStringWritten);
  bleCharacteristicSwitch.setEventHandler(BLERead, bleOnCharacteristicStringRead);
  bleCharacteristicSwitch.setValue(String("INIT").c_str());
  
  BLE.advertise();
}

void loop() 
{
  static uint32_t uLoopCount = 0;
  uLoopCount++;
  
  if (uLoopCount == 1)
    Serial.println("loop: first call"); 

  // poll for Bluetooth® Low Energy events
  BLE.poll();

  delay(10);
}

Hi source2k,

While observing the current waveform of the C3, I repeated the connection, data read, and disconnection using the application LightBlue. As far as analyzing the current waveforms, C3 is,

  1. advertise at 100mS intervals,
  2. stop advertising when connected to LightBlue,
  3. responds to data reads while maintaining the connection at 30 mS intervals,
  4. when LightBlue disconnects, it resumes advertising.

BLE.stopAdvertise(); does not stop adverticing the BLE service.

When connected, advertising will stop, but services will continue.

BLE.end(); crashes the board.

When disconnected, the advertising resumed without crashing.

I want to stop advertising and deinit BLE to save power. Can you try the example?
I forgot to mention that I’m using ArduinoIDE and the ArduinoBLE library.

I have tried this with your sketch.
Even when the advertisement is stopped, the current does not decrease as long as it is connected, with an average current of 65 mA.
Sleep is an easy way to reduce the current.
The following link may be helpful.
Comparison of Sleep Currents for XIAO ESP32C6, S3, and C3

I need to stop BLE advertise also to hide the device some time.
Running the device without activated BLE gives me 20 mA at 80 MHz CPU clock.
So I want to shutdown BLE but BLE.end() crashes.
I’d like to use deep sleep but the documentation says " …the application must disable Wi-Fi and Bluetooth…" (see Sleep Mode)

So I want to shutdown BLE but BLE.end() crashes.

What exactly happens?

I added “Sleep/Wakeup” to your sketch. The LED blinks to confirm the operation.
As far as the current waveform is concerned, it sleeps without crashing and the current is reduced to 15uA.
ESP32S3_BLE_Sleep.zip (1.1 KB)

Thank you msfujino for caring for my problem.
Your example works as expected. That is great!
My board still crashes when not using deep sleep.
I added a delay of 2s after BLE.end() and the board crashes after the delay. I think when loop() continues.

I attached the sketch and the crash log.
ESP_S32S3_BLE_crashing.zip (1.8 KB)

Also only “<123” of “<123454321>” is received on my phone at the first read (length of “INIT” string. The second time the complete value is sent.

I have confirmed that it crashes.
I am using the ESP32 BLE library for ESP below, not ArduinoBLE, so I don’t know any more. Why don’t you search for “Guru Meditation Error: Core 1 panic’ed (LoadProhibited)” or ask on GitHub for ArduinoBLE?

\Arduino15\packages\esp32\hardware\esp32\3.0.1\libraries\BLE

Hi there,
Idunno if this matters or not, But I have found If I have ArduinoBLE installed, even if not using that LIB
it effects the compile, and some sketches do Crash. YMMV :v:
Can you post the compiler output?
first 4 lines, and the last 20 would be sufficient to possibly see what it’s doing.
HTH
GL :slight_smile: PJ

also I was under the impression that only Nimble.h supports a BLE end… So?

I was able to decode the “Backtrace: 0x403826fa:0x3fcebe00…” and the crash originates from poll().
Hence I changed the sketch to do the “BLE.end;” in “loop()” but it is never called.
Changing the variable “iBLEState = 2;” in function “bleOnDisconnected” has no effect. Hopefully somebody knows why.
Here is the sketch and log:
testBLEESP32S3_240611_2_no_BLEend.zip (1.5 KB)

Also ArduinoIDE uses “.arduino15/packages/esp32/tools/esp32-arduino-libs/idf-release_v5.1-442a798083” but current stable version is 5.2.2.