Bluetooth not turning on in ESP32

#include <WiFi.h>
#include <BluetoothSerial.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void loop2( void * parameter )
{
  for (;;) {
     SerialBT.begin("XXXXXX");
  }
}

void setup() {
  Serial.begin(115200);
  xTaskCreatePinnedToCore(loop2, "buttonCheck", 1000, NULL, 1, &Task1, 0);
}

void loop() {

}

This is my reproducible code, I am using two loops in my project but Bluetooth not starting in my second loop and loop stuck at that point but if I write

SerialBT.begin("XXXXXX");

inside main loop then it's working fine. Why it's happening? I don't want to write my code in main loop so please guide me.

Put it in setup()

void loop2( void * parameter )
{
  for (;;) {
     SerialBT.begin("XXXXXX");
  }
}

Why do you continuously begin() the SerialBT connection in an endless for loop when just once would be good enough ?

Actually I don't want to start bluetooth at startup and need to open the connection when user need it that's why I have not written it in setup

Sorry my bad, actually In my code there is a if condition like this

If(isPairing()) {
   SerialBT.begin("xxxx");
   isPairing = false;
}

That's why it's only iterate one time in the loop and I tried by printing a text before and after begin then it's just printing text that written before the begin statement, and statement just after the begin not printing means loop stuck at the beign() part.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.