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());
}