in

BLE server requiring PIN to pair/bound not working using BLEDevice and BLEServer in Arduino


I tried to make a BLE server protected so adding encryption and security. They are just 5 general SLOCs more and, eventually, one SLOC for each characteristics but it does not work. Testing with my smartphones (Andorid 8 and 10) usinf nrfConnect, they require the static PIN to pair/bond with the ESP32, they connect but they disconnect soon also, just matter a couple of seconds. Am I alone having thsi problem?
This is my test code:

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID “4fafc201-1fb5-459e-8fcc-c5c9c331914b”
#define SSID_CHAR_UUID “1afb81ce-36e1-4688-b7f5-ea07361b26a8”
#define PSWD_CHAR_UUID “1be107e0-2d9e-4091-a0d3-6407e01b2a30”
#define WIFI_CHAR_UUID “1afb81ce-a705-4ab5-aaab-294269ce9a52”
#define IP_CHAR_UUID “beb5483e-36e1-4688-b7f5-ea07361b26a8”

const char* ssid = “ESP32-TT22”;
const char* password = “pippo21931”;
char g_strWiFiEnabled[] = “0”;
const char* ip = “192.168.4.1”;

void setupBLE()
{
BLEDevice::init(“Long name works now”);
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);

BLECharacteristic *pCharSSID = pService->createCharacteristic(
SSID_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
BLECharacteristic *pCharPSWD = pService->createCharacteristic(
PSWD_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);

BLECharacteristic *pCharWIFI = pService->createCharacteristic(
WIFI_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);

BLECharacteristic *pCharIP = pService->createCharacteristic(
IP_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);

pCharSSID->setValue(ssid);
pCharPSWD->setValue(password);
pCharWIFI->setValue(g_strWiFiEnabled);
pCharIP->setValue(ip);
pService->start();

#define COND 1
#if COND == 0
// [MM] enable encrypting PIN protected pairing
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
#endif
#if COND == 0
// [MM] enable encrypting PIN protected pairing
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
#endif
// [MM] setup the encrypted PIN for protected pairing
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setStaticPIN(2193);

// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println(“Characteristic defined! Now you can read it in your phone!”);
}
//—————————————————————

void setup()
{
Serial.begin(115200);
Serial.println(“Starting BLE work!”);
setupBLE();

}

void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}



Source: https://stackoverflow.com/questions/70626105/ble-server-requiring-pin-to-pair-bound-not-working-using-bledevice-and-bleserver

an advanced neural biomedical namedentity recognition and normalization tool

Magpress v3.3 – Magazine Responsive Blogger Template – ULC