ESP32 BLE Exception when trying to connect to Characteristics

I'm working on a personal project to connect to a bluetooth device and control it from an ESP32.
Using a bluetooth app on my phone, I can connect to this device, access the service + characteristic, and write a value e.g. to change the color (similar to this project: )

I wrote the following program to scan for devices and connect based on the name. I also tested it using callbacks but I currently do it in the setup.

#include "BLEDevice.h"
static BLEUUID serviceUUID("390d9772-3889-11ec-8d3d-0242ac130003");
static BLEUUID charUUID("318c0fa6-3889-11ec-8d3d-0242ac130003");
bool connected = false;

BLEClient* pMyClient = BLEDevice::createClient();
BLERemoteService* pMyRemoteService;
BLERemoteCharacteristic* pMyRemoteCharacteristic;
static BLEAdvertisedDevice* pMyDevice;

static String robotName = "MyRobot";
uint8_t rgb_color_data[] = { 0x00, 0xFF, 0xFF, 0x21, 0x20, 0xFF };

void setup() {
  Serial.println("Starting Controller application...");
  BLEScan* pBLEScan = BLEDevice::getScan();
  BLEScanResults pfoundDevices = pBLEScan->start(5, false);
  int count = pfoundDevices.getCount();
    Serial.print("Found "); Serial.print(count); Serial.println(" devices");
    for (int i = 0; i < count; i++) {
      BLEAdvertisedDevice bluetoothDevice = pfoundDevices.getDevice(i); 
      Serial.print("[INFO] Checking device "); Serial.println(bluetoothDevice.getName().c_str());
      String deviceName = bluetoothDevice.getName().c_str();
       if (deviceName == robotName) {
          Serial.print("Connecting with "); Serial.println(deviceName);
          pMyDevice = new BLEAdvertisedDevice(bluetoothDevice); 
           if (pMyClient->connect(pMyDevice)) {
            Serial.println("[OK] device connected");
            Serial.print("[INFO] Connecting to service ");   Serial.println(serviceUUID.toString().c_str());
            pMyRemoteService = pMyClient->getService(serviceUUID);
            Serial.println("[OK] service connected");
            Serial.print("[INFO] Connecting to characteristic ");   Serial.println(charUUID.toString().c_str());
            pMyRemoteCharacteristic = pMyRemoteService->getCharacteristic(charUUID);
            Serial.println("[OK] characteristic connected");
            connected = true;

void loop() {
  if ( connected ) {
    Serial.println("[OK] Updating value");
    if ( pMyRemoteCharacteristic != NULL ) {
      if ( pMyRemoteCharacteristic->canWriteNoResponse() ) {
        r = random(0,256);
        g = random(0,256);
        b = random(0,256);        
        rgb_color_data[4] = r;
        rgb_color_data[5] = g;
        rgb_color_data[3] = b;
        Serial.println("[INFO] Writing value...");
        pMyRemoteCharacteristic->writeValue(rgb_color_data, sizeof(rgb_color_data));        
        Serial.println("[OK] Done");

Below is the log from the Serial Monitor. First it takes 1 minute to connect to the service. Then accessing the characteristic leads to the Exception error.

I welcome any comment on anything I might be doing wrong.

09:14:52.210 -> Starting Controller application...
09:14:57.201 -> Found 5 devices
09:14:57.201 -> [INFO] Checking device 
09:14:57.201 -> [INFO] Checking device 
09:14:57.201 -> [INFO] Checking device 
09:14:57.201 -> [INFO] Checking device 
09:14:57.201 -> [INFO] Checking device MyRobot
09:14:57.201 -> Name: MyRobot
09:14:57.235 -> Connecting with MyRobot
09:15:37.527 -> [OK] device connected
09:15:37.527 -> [INFO] Connecting to service UUID
09:16:37.507 -> [OK] service connected
09:16:37.507 -> [INFO] Connecting to characteristic UUID
09:16:37.542 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
09:16:37.542 -> Core 1 register dump:
09:16:37.542 -> PC      : 0x400d4fce  PS      : 0x00060330  A0      : 0x800d19cd  A1      : 0x3ffc7350  
09:16:37.542 -> A2      : 0x00000000  A3      : 0x62679b60  A4      : 0x3ffc427c  A5      : 0x4548a885  
09:16:37.542 -> A6      : 0x136c5f13  A7      : 0x00018903  A8      : 0x80177ccd  A9      : 0x3ffc7370  
09:16:37.542 -> A10     : 0x3ffdcff8  A11     : 0x3ffdcff8  A12     : 0x3ffc737c  A13     : 0x00000000  
09:16:37.576 -> A14     : 0x00000003  A15     : 0x00000013  SAR     : 0x00000008  EXCCAUSE: 0x0000001c  
09:16:37.576 -> EXCVADDR: 0x00000034  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  
09:16:37.576 -> 
09:16:37.576 -> ELF file SHA256: 0000000000000000
09:16:37.576 -> 
09:16:37.576 -> Backtrace: 0x400d4fce:0x3ffc7350 0x400d19ca:0x3ffc73b0 0x400d7da6:0x3ffc7520 0x4008ff66:0x3ffc7540
09:16:37.576 -> 
09:16:37.576 -> Rebooting...

Note that I also tried connecting to the service and char with
pMyRemoteService = pMyClient->getService(BLEUUID(serviceUUID));

After you have looked up ESP Exception Decoder, have installed it and pasted your exception into the decoder and the decoder prints out its results, could you post those results here?

Here is a suggestion for your code:

1 Like

Thank you for your replies.
After some further investigations, I started over with the BLE Beacon Scanner example.
This appears to be an issue with the device I'm trying to use i.e. my ESP32 doesn't see any service.
advertisedDevice.haveServiceUUID() returns 0 and advertisedDevice.getServiceUUID() is null

Looking at my device on an iPhone app (LightBlue), it sees the robot device and it mentions "No services" below (or BlueCap app says Services :0)
When I connect to it, I can see and browse into 5 services, including the one I want to use. Opening this service, I have access to the 2 characteristics I want to use.

So I tried adding the following in the MyAdvertisedDeviceCallbacks

 Serial.print("Device name: ");
   BLEClient* pMyClient = BLEDevice::createClient(); 
      Serial.print("Nb of services  "); Serial.println(String(pMyClient->getServices()->size()));

Nb of services is never displayed so I'm not sure what I'm doing wrong.
I'd like after to find a way to loop on pMyClient->getServices(); but not unsure how to do this.
Am I looking at the right info to attempt finding the service I need?


Again, you should check for null pointer:

BLEClient* pMyClient = BLEDevice::createClient();
if (pMyClient == NULL) Serial.println("Unable to create client");
  Serial.println("Client created, connecting...");
  //Convert coffee to more code here..

Could some sort of failed pairing be the problem?

I tried your suggestion and the client is created ok.
The issue is when I connect to the device ; nothing happens (the message Client connected... is never displayed). I tried with and without connecting with the address type (1).

 BLEClient* pMyClient = BLEDevice::createClient(); 
        if (pMyClient == NULL) Serial.println("Unable to create client");
            esp_ble_addr_type_t type = advertisedDevice.getAddressType(); 
            Serial.println("Server address type: " + String(type));
            Serial.print("Client created, connecting...");
            pMyClient->connect(advertisedDevice.getAddress(), type); 
            Serial.println("Client connected...");
            Serial.print("Nb of services  "); Serial.println(String(pMyClient->getServices()->size()));

Try this instead:

BLEAddress *addr = new BLEAddress(advertisedDevice.getAddress());

Unfortunately there's an error when doing the verify:

no matching function for call to 'BLEClient::connect(BLEAddress*&)'

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.