ESP32 BLE library not working

I have an ESP32 Pico kit and Arduino nano 33 BLE sense. I am sending some data from Arduino nano 33 to ESP32.
The following code doesn't seem to be working. Though, ESP32 is finding a device but it is not able to get the services or characteristics.!

#include "BLEDevice.h"

static BLEUUID serviceUUID("19B1000-E8F2-537E-4F6C-D104768A1214");
static BLEUUID charUUID("19B10001-E8F2-537E-4F6C-D104768A1214");
static BLEAdvertisedDevice* rgbdevice;


#define add "d9:34:f3:35:96:24"

static BLEAddress rgbadd(add);

class MyClientCallback : public BLEClientCallbacks{
  void onConnect(BLEClient* pclient){
    }
  void onDisconnect(BLEClient* pclient){
    Serial.println("Disconnected");
    }
  };
  
class MyCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
// Do something with the found device ...
Serial.println("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());

if(advertisedDevice.haveServiceUUID()){
  advertisedDevice.getScan()->stop();
  rgbdevice = new BLEAdvertisedDevice(advertisedDevice);
  Serial.println("- it has serviceUUID");
  Serial.println(advertisedDevice.getAddress().toString().c_str());
  BLEClient *RGBMonitor = BLEDevice::createClient();
  delay(1000);
  RGBMonitor->setClientCallbacks(new MyClientCallback());
  RGBMonitor->connect(rgbadd);
  Serial.println("- it has serviceUUID");
  Serial.println("...Connected to the server...");
  BLERemoteService* rgbservice = RGBMonitor->getService(serviceUUID);
  if(rgbservice == nullptr){
    Serial.println("Failed to find out service UUID");
    }
  else{
    Serial.println("- Found a device - ");
    }
  BLERemoteCharacteristic* rgbcharacteristic = rgbservice->getCharacteristic(charUUID);
  if(rgbcharacteristic == nullptr){
    Serial.println("Failed to find Charateristic UUID");
    }
  else{
    Serial.println("Found a characteristic");
    }
  if(rgbcharacteristic->canRead()){
    std::string Value = rgbcharacteristic->readValue();  
    Serial.println(Value.c_str());
    RGBMonitor->disconnect();
    }
  }
}
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  BLEDevice::init("");
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyCallbacks());
  BLEScanResults result= pBLEScan->start(30);
  Serial.println(result.getCount());

}

Have you got one of the ESP32 BLE examples to run?
I ask because I notice how your setup compares to the setup in the example.

BLE example code setup():

void setup() {
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
} // End of setup.

Like in the above BLEDevice::init(""); is used only once in setup()
With your code you wrote

void loop() {
  // put your main code here, to run repeatedly:
  BLEDevice::init("");

Where the init is happening 100's or 1000's of times a second. Also leads me to think other things are coded incorrectly as well. So could you try using one of the ESP32 BLE examples to test your code and to the example as a guide on how to do the thing?

Hi,

I tried the example server code, and it works okay. In my code, I changed the BLE::scan() part to setup() but still not working!