-
-
Notifications
You must be signed in to change notification settings - Fork 182
Description
Hi,
I am currently working with the LilyGO T-SIM7000G (ESP32) board for a project that involves connecting to a BLE peripheral device. My BLE device requires pairing and bonding before communication can begin.
However, I’m facing issues while trying to establish the bond. The ESP32 seems unable to complete the pairing process successfully. I also implemented the BLEClientCallbacks to monitor the events, but I am not receiving any response in the callback methods.
I’ve attached my current code below. Could you please review it and let me know if I’m missing something or if any changes are needed to properly handle BLE bonding and secure communication?
Key points:
LilyGO T-SIM7000G (ESP32-based board)
BLE device requires pairing/bonding before communication
Not receiving expected events in BLEClientCallbacks
Unable to establish a bond with the peripheral
Thank you for your help!.
===================== Start of Code ====================
#include <Arduino.h>
#include <NimBLEDevice.h>
class ClientCallbacks : public NimBLEClientCallbacks {
void onPassKeyEntry(NimBLEConnInfo& connInfo) override {
Serial.printf("Server Passkey Entry\n");
//
// This should prompt the user to enter the passkey displayed
// on the peer device.
//
NimBLEDevice::injectPassKey(connInfo, 123456);
}
} clientCallbacks;
void setup() {
Serial.begin(115200);
Serial.println("Starting NimBLE Client");
NimBLEDevice::init("NimBLE-Client-device");
NimBLEDevice::setSecurityPasskey(579273);
NimBLEDevice::setSecurityAuth(true, true, true); /** bonding, MITM, BLE secure connections */
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT); /** passkey */
NimBLEScan* pScan = NimBLEDevice::getScan();
NimBLEScanResults results = pScan->getResults(5 * 1000);
NimBLEUUID serviceUuid("00001808-0000-1000-8000-00805f9b34fb");
for (int i = 0; i < results.getCount(); i++) {
const NimBLEAdvertisedDevice* device = results.getDevice(i);
if (device->isAdvertisingService(serviceUuid)) {
Serial.println(device->getName().c_str());
NimBLEClient* pClient = NimBLEDevice::createClient();
pClient->setClientCallbacks(&clientCallbacks, false);
if (pClient->connect(device)) {
NimBLEConnInfo connInfo = pClient->getConnInfo();
Serial.print("isBonded:");
Serial.println(connInfo.isBonded());
NimBLEDevice::injectPassKey(connInfo, 579273);
Serial.println("Connected to server");
pClient->secureConnection();
NimBLERemoteService* pService = pClient->getService(serviceUuid);
if (pService != nullptr) {
NimBLERemoteCharacteristic* pNonSecureCharacteristic = pService->getCharacteristic("1234");
if (pNonSecureCharacteristic != nullptr) {
// Testing to read a non secured characteristic, you should be able to read this even if you have mismatching passkeys.
std::string value = pNonSecureCharacteristic->readValue();
// print or do whatever you need with the value
Serial.println(value.c_str());
}
NimBLERemoteCharacteristic* pSecureCharacteristic = pService->getCharacteristic("1235");
if (pSecureCharacteristic != nullptr) {
// Testing to read a secured characteristic, you should be able to read this only if you have
// matching passkeys, otherwise you should get an error like this. E NimBLERemoteCharacteristic:
// "<< readValue rc=261" This means you are trying to do something without the proper
// permissions.
std::string value = pSecureCharacteristic->readValue();
// print or do whatever you need with the value
Serial.println(value.c_str());
}
}
} else {
// failed to connect
Serial.println("failed to connect");
}
NimBLEDevice::deleteClient(pClient);
}
}
}
void loop() {}
========================= End of Code =======================
Here is the logs,
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4832
load:0x40078000,len:16460
load:0x40080400,len:4
load:0x40080404,len:3504
entry 0x400805cc
Starting NimBLE Client
meter+05912330
isBonded:0
Connected to server