Hello all,
I am facing a weird issue with Bluetooth pairing process with esp32s3.
Hardware module: ESP32-S3-MINI-1-N8
Dev Platform: Platform IO
[env:esp32-s3-devkitc-1]
platform = espressif32@6.4.0
framework = arduino
board = esp32-s3-devkitc-1
board_build.f_cpu = 80000000L
App Used to test:
nRF connect
I have attached a screen shot of where the problem is happening
In the Screenshot above the the Serial Number characteristic UUID 2A25 and Firmware revision characteristic UUID 2A26 receive correct string response from ESP. This is without pairing and the connection is authenticated by a button press on the device.
When pairing is enabled, the string response for the Serial Number UUID 2A25 and Firmware revision UUID 2A26 is swapped. Meaning the request for UUID 2A25 would return a value 2.9.3 and the request for UUID 2A26 would return XX:XX:XX:XX:1C:28.
(MAC Address blacked out for security reasons)
This happens only when pairing is enabled. The details of pairing mechanism used -
- Initialization:
- The BLE device is initialized with a custom name (device_name + “-” + ble_name).
- Security Configuration:
- The pairing process uses Secure Connections with MITM (Man-in-the-Middle) protection using -
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT_MITM);
- A custom security callback class (MySecurity) is used to handle pairing events like passkey requests, notifications, and authentication completion.
- Passkey Handling:
A predefined passkey (6 digits) is returned during the pairing process - The onAuthenticationComplete callback handles the result of the pairing process:
- If successful, BLE services are enabled, and the client is notified of the success.
- If failed, the client is disconnected
- Passkey Handling:
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl)
{
if (cmpl.success) {
bleServicesEnabled = true;
send_ble_ack_status("ss"); // Notify success
}
else {
bleServicesEnabled = false;
pServer->disconnect(idclient); // Disconnect client
}
}
- Dynamic Security Levels**:
The gapEventHandler
dynamically adjusts the security level based on the connecting device type (e.g., gateway or phone)
esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_BOND; // Bonding with numeric passkey
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(auth_req));
- Post-Pairing
After pairing, the BLE server starts advertising again if the client disconnects:
BLEDevice::startAdvertising();
We use a custom self developed app to connect to our device via Bluetooth and due to this swapped values it goes into a connection loop and terminates after the timeout since the values were swapped and could not be verified.
When it fails to connect on first try, turning Bluetooth on and off on the mobile phone helps and when we try connecting after having restarted the Bluetooth on the phone, the connection establishment with pairing works again. This happened on the custom app and we thought it might be a app issue, but when we tested it using nrf connect app, the same issue persists.
The frequency of this issue is very less and it has been very hard to reproduce the issue.
Any idea why this could be happening?
Thanks in advance.