Description
First of all, hello and thank you for your libraries and excuse me for this probably very simple question.
Very recently I have begun learning BLE and the esp32 and my goal for the moment is to successully establish a BLE connection between an esp32 (as client) and the nRF52 (as server). I believe the nRF52 works, since I can connect to it using my phone and control the value of characteristics in it. However, I can't get the esp32 to connect to it successully. It is able to scan and detect nearby devices and find the nRF52's adress no problem, but when a connection attempt is made using the connect() function, the program seems to wait indefinitely and no connection is ever established. While I have done some research on semaphores, I am still quite unfamilliar with them.
I wish to understand where it gets stuck in the connect() fuction, why it does that and how can I make it work.
Here is my code (I am using the Arduino framework in PlatformIO):
#include <Arduino.h>
#include "BLEDevice.h"
static BLEUUID serviceUUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
static BLEUUID characteristicUUID("0000004f-0000-1000-8000-00805f9b34fb");
String My_BLE_Address = "d1:c7:bd:79:c4:05";
static BLERemoteCharacteristic* pRemoteCharacteristic;
BLEScan* pBLEScan;
BLEScanResults foundDevices;
static BLEAddress *Server_BLE_Address;
String Scanned_BLE_Address;
boolean paired = false; // Flag verifying if we are paired with our device
boolean foundDevice = false; // Flag verifying if we found our device
bool connectToserver (BLEAddress pAddress){
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
// Connect to the BLE Server.
pClient->connect(pAddress); // Stuck here...
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService != nullptr)
{
Serial.println(" - Found our service");
return true;
}
else
return false;
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(characteristicUUID);
if (pRemoteCharacteristic != nullptr)
Serial.println(" - Found our characteristic");
return true;
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Scan Result: %s \n", advertisedDevice.toString().c_str());
if(foundDevice == false){ // Flag verifying if we found our device
Server_BLE_Address = new BLEAddress(advertisedDevice.getAddress()); // Update Server_BLE_Address if we didn't
Scanned_BLE_Address = Server_BLE_Address->toString().c_str();
}
if(Scanned_BLE_Address == My_BLE_Address) // Compares the scanned adress to what we are looking for
{
foundDevice = true; // Found our device
}
Serial.println("Comparing to our server adress: ");
Serial.println(My_BLE_Address);
}
};
void setup() {
Serial.begin(115200); //Start serial monitor
Serial.println("ESP32 BLE Client program"); //Intro message
BLEDevice::init("ESP32");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //Call the class that is defined above
pBLEScan->setActiveScan(false); //active scan uses more power, but get results faster
}
void loop() {
foundDevices = pBLEScan->start(3); //Scan for 3 seconds to find the server
uint8_t devicesCount = foundDevices.getCount();
Serial.printf("%d devices found \n", devicesCount);
while (devicesCount >= 1 && foundDevice == true) // We found our server
{
if (foundDevice == true && paired == false) // We're not connected yet
{
Serial.println("Found our Device... connecting to Server as client \n");
if (connectToserver(*Server_BLE_Address)) // Attempt a connection to the server ...Stuck here...
{
paired = true;
Serial.println("********************CONNECTED TO SERVER************************");
break;
}
else
{
Serial.println("Pairing failed");
foundDevice = false;
break;
}
}
devicesCount = 0; // Reset the number of devices
}
pBLEScan->stop();
delay(5000);
}
And here is what the serial monitor looks like:
ESP32 BLE Client program
Scan Result: Name: , Address: 3f:43:89:02:09:99, manufacturer data: 0600010920020273111e440a64443a2bbdb913d98c83605f724bf88dd6
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 21:5f:63:6c:59:18, manufacturer data: 0600010920024cbbbc219822bf9d445e8c12280b7f0e218a9c491fd485
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 22:08:60:11:95:b3, manufacturer data: 06000109200267b9b0dc5ac0c049f892ec1bb09f3afed1bac30914b6fb
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 3b:e3:63:51:43:55, manufacturer data: 060001092002578fe17a471e0a3fff4cb07172df27a10fc4835298acd4
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: nRF52, Address: d1:c7:bd:79:c4:05, txPower: 4
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 04:b3:68:61:52:c6, manufacturer data: 060001092002810c02a9395aa4bc0d6ca4ee830ff5dc8f1dfc49e15e74
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 74:97:07:46:24:ca, manufacturer data: 4c001005131cc48f22
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 08:c0:21:ff:af:48, manufacturer data: 060001092002585e2df55dd581bf04830fe41d2f2d0cd131b433f79c76
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: f4:5c:89:a7:90:36, manufacturer data: 4c0010050b1c604ec2
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 52:33:f9:76:9e:a1, manufacturer data: 4c000c0e00f2e83ef80545a417a96ba8e194
Comparing to our server adress:
d1:c7:bd:79:c4:05
Scan Result: Name: , Address: 7c:87:5c:cc:fd:c8, manufacturer data: 4c0010050318008367
Comparing to our server adress:
d1:c7:bd:79:c4:05
11 devices found
Found our Device... connecting to Server as client
- Created client
Do you think you can help me?
Thanks in advance.