Connection on ESP32 takes a long time to connect to a Bluetooth device

Hi everyone! I am having some trouble trying to connect the ESP32 with a Bluetooth device. Let me explain the situation:

I have the following code:

#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

#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;

esp_spp_sec_t sec_mask = ESP_SPP_SEC_AUTHENTICATE;
esp_spp_role_t role = ESP_SPP_ROLE_MASTER;

void setup(){
    Serial.println("Enable wireless communication -> Ok");
    // Enable bluetooth serial
    if (!SerialBT.begin("ESP32test", true))
    {
        Serial.println("========== serialBT failed!");
        abort();
    }

    SerialBT.setPin("1234");
    SerialBT.enableSSP();

    uint8_t macBT[6] = {0x1A, 0x4E, 0x11, 0x25, 0x18, 0x6B};

    // Connect to device

    while (!SerialBT.connect(macBT, 0, sec_mask, role))
    {
        Serial.print(".");
        delay(1000);
    }
    Serial.println("Connection -> Ok");
}

void loop()
{
}


This code can actually connect to the other device, but in some situations, the connection can take less than one minute, while in other situations it can take 10 minutes or more. What could be the reason for this? and how can I fix it?

Thanks!

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