Which BLE Library to use for the NanoESP2?

BINGO! I removed the ArduinoBLE library from the libraries directory and then your sketch works! So it seems to be an issue between the ArduinoBLE library and the NanoESP32 BLE library.

I will now try forcing the sketch to use the ~/.arduino15/packages/arduino/hardware/esp32/2.0.18-20240930.arduino3/libraries/BLE library.

It seems that one cannot use the NanoESP32 bluetooth library if the ArduinoBLE library is installed. However the ArduinoBLE is required for other Arduino models using bluetooth, such as the UnoR4WiFi.

This would appear to be an IDE design error. I will report this as a compatability issue but would like your opinion before I do.

Once again, thank you very much for your patience, suggestions and help.

Hi @steveinaustria.

It is not. The IDE is working exactly as intended, as documented here:

https://arduino.github.io/arduino-cli/latest/sketch-build-process/#dependency-resolution

I'll provide a more detailed explanation below.

Background

Arduino IDE has a "library discovery" system. For each #include directive in the program being compiled, the IDE first checks to see if the file is present in the the compiler's "search path". If not, it then searches through the root source folder of each of the installed libraries to look for a file of that name.

Sometimes multiple libraries contain a file matching the #include directive. In this case, Arduino IDE must decide which of the libraries to use. It has a sophisticated algorithm for picking the best library so it usually makes the right decision, but not always.

Explanation

This is a case where Arduino IDE made the wrong decision when discovering the library for the sketch's #include "BLEDevice.h" directive.

A file of that name is present in the "ArduinoBLE" library as well as in the "ESP32 BLE Arduino" library that is bundled with the "esp32" boards platform installation. The IDE chose to use the "ArduinoBLE" library, but the sketch is meant to use the "ESP32 BLE Arduino" library.

Solution

You already discovered the solution of uninstalling the "ArduinoBLE" library. However, if you do need the "ArduinoBLE" library for use in other projects then this solution will not be acceptable.

In this case the solution will be to influence the library discovery system to chose the "ESP32 BLE Arduino" library over the "ArduinoBLE" library.

As I mentioned before, files from the include search path are always given priority by the library discovery system. Once a library is "discovered", its path is added to the include search path. So if we can cause the "ESP32 BLE Arduino" library to be discovered before Arduino IDE does discovery for the ambiguous BLEDevice.h file, then it not discover the "ArduinoBLE" library. This is accomplished by placing an #include directive for a file unique to the "ESP32 BLE Arduino" library above the ambiguous #include directive.

I will arbitrarily choose the file BLEUUID.h, which is present in the "ESP32 BLE Arduino" library, but not in the "ArduinoBLE" library. Simply add an #include directive for that file above the #include directive for BLEDevice.h in your sketch.

So change this part of your sketch:

#include "BLEDevice.h"

to this:

#include "BLEUUID.h"
#include "BLEDevice.h"

Now try compiling the sketch again. Hopefully this time the error will not occur and everything will work as expected.

1 Like

It is not the IDE, can't be.

The scan works if ArduinoBLE is also installed by moving the include for BLEDevice to the end of the includes

See post #25

Hi ptillisch, I didn't mean to insult you or the IDE as you have helped me personally here many times and I have been using the IDE successfully for over a decade with many different Arduino and non-arduino boards. Your workaround works for the NanoESP32 examples even though I've never thought I would have to go to such lengths to get two modern Arduino boards (UnoR4WiFi and NanoESP32), both with the ESP32 onboard, to both work with Bluetooth LE.

Unfortunately your workaround does not work with the third party RemoteXY Script as this seems to requre the ArduinoBLE library to connect to the board and the ESP32 BLE Arduino library for the script.

Is it not possible for there to be an Unified Arduino ESP32 BLE Library which can be used by all modern Arduino models with the ESP32 onboard? Sorry if this question is naive but as a long time Arduino user and hobbyist, libraries have always been my friend without me having to delve so deeply into what each library is capable of.

Please post the Arduino sketch that was generated by RemoteXY for your project and I'll take a look.

Yes, and we already have that library. It is ArduinoBLE.

However, the fact that the ArduinoBLE library supports ESP32 boards is irrelevant because the RemoteXY sketch code is not written to use that library.

You are welcome to rewrite the RemoteXY sketch code to use the ArduinoBLE library if you like, but I'm sure it will be much less work to simply adjust the RemoteXY code to cause the correct library to be selected by the Arduino IDE library discovery system.

Thanks for the reply, here is the RemoteXY scketch:

/*
   -- ledSwitch --
   
   This source code of graphical user interface 
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 3.1.13 or later version 
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.15.01 or later version;
     - for iOS 1.12.1 or later version;
    
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.    
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// you can enable debug logging to Serial at 115200
//#define REMOTEXY__DEBUGLOG    

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP32CORE_BLE

#include <BLEDevice.h>

// RemoteXY connection settings 
#define REMOTEXY_BLUETOOTH_NAME "RemoteXY"
#define REMOTEXY_ACCESS_PASSWORD "1111"


#include <RemoteXY.h>

// RemoteXY GUI configuration  
#pragma pack(push, 1)  
uint8_t RemoteXY_CONF[] =   // 54 bytes
  { 255,1,0,1,0,47,0,19,0,0,0,82,101,109,111,116,101,88,89,0,
  31,1,106,200,1,1,2,0,2,29,27,44,22,0,2,26,31,31,79,78,
  0,79,70,70,0,70,42,70,18,18,16,26,37,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t switch_01; // =1 if switch ON and =0 if OFF

    // output variables
  uint8_t led_01; // from 0 to 1

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;   
#pragma pack(pop)
 
/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define PIN_SWITCH_01 15


void setup() 
{
  RemoteXY_Init (); 
  
  pinMode (PIN_SWITCH_01, OUTPUT);
  
  // TODO you setup code
  
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  digitalWrite(PIN_SWITCH_01, (RemoteXY.switch_01==0)?LOW:HIGH);
  
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay(), use instead RemoteXY_delay() 


}

Then why does one have to remove the ArduinoBLE library to get the NanoESP32 examples to work?

The solution is the same as what I described in post #25

Change this line of the sketch:

to this:

#include "BLEUUID.h"
#include <BLEDevice.h>

I already explained it in post #30:

The RemoteXY sketch code is written to use the API of the the "ESP32 BLE Arduino" library that is bundled with the ESP32 boards platform installation. So it will only compile when that library is used.

So you have two options:

  • Rewrite the RemoteXY sketch code for the ArduinoBLE library's API
  • Adjust the sketch code so that the intended ESP32 BLE Arduino is used

The sketch does not work even with your alteration. It uploads OK but I cannot connet to the NanoESP32 via my Android phone. Everything works fine with the UnoR4WiFi version of the sketch.

With all due respect, I know you explained it (in post #25 actually). But you also said that the ArduinoBLE library was a united library for all Arduinos with a ESP32 on board(in post #30). Both claims cannot be true, surely. If the ArduinoBLE library is universal for Arduinos with an ESP32 on board, it should not be necessary to either work around it or remove it altogether to get the offical NanoESP32 examples to work. Or am I looking at this wrong?

You are looking at it wrong. Unfortunately I don't know I can explain this to you any better than I have done already so there isn't anything more I can do. Maybe one of the other forum helpers will be able to help you to understand.