Arduino – ESP32 classic Bluetooth set password

arduinobluetoothesp32

I am using the ESP32 classic Bluetooth feature for data transfer.

I am using the SerialToSerialBT example.

I am able to scan and discover the Bluetooth device from my smartphone, but the pairing is without any security/passcode. It simply pairs immediately. How to make the pairing use PIN/passkey entry?

Hardware:
ESP32 DEV Kit V1

Software
Arduino IDE

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

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  delay(5000);
  Serial.println("Starting..");
  SerialBT.begin("MyESP32"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

Best Answer

As far as I know, ESP32 doesn't have inbuilt password configuration mechanism. So your requirement may not be straightforward as there could also be a standard Android OS/IOS handshaking protocol between any password protected bluetooth device and the phone during connection, which you need to figure out.

Instead, why not try something simpler like this at application level?:

  1. Connect the smart phone and ESP32 via bluetooth.
  2. Send a request for password from ESP32.
  3. Print this request in bluetooth terminal on phone, send back the password.
  4. If the password is correct, flag this, and keep the connection for further communication, else disconnect from the phone using serial.end().
Related Topic