NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

Autodetection if NimBLE-Arduino v1 or v2 is used

Open vovagorodok opened this issue 2 months ago • 7 comments

I'm working on automatic BLE library detection in ArduinoBleOTA library https://github.com/vovagorodok/ArduinoBleOTA/blob/main/src/BleOtaDefines.h

#if __has_include("ArduinoBLE.h")
    #include <ArduinoBLE.h>
    #define BLE_OTA_BLE_LIB_ARDUINO_BLE
    #define BLE_OTA_LIB_ARDUINO_BLE
#elif __has_include("NimBLEDevice.h")
    #include <NimBLEDevice.h>
    #define BLE_OTA_BLE_LIB_NIM_BLE_ARDUINO
    #define BLE_OTA_LIB_NIM_BLE_ARDUINO
#elif defined(ARDUINO_ARCH_ESP32)
    #include <BLEDevice.h>
    #define BLE_OTA_BLE_LIB_ESP32
    #define BLE_OTA_LIB_ESP32
#else
    #error "Unsupported BLE library. Consider ArduinoBLE or NimBLE-Arduino."
#endif

What is the best way to detect if NimBLE-Arduino v1 or v2 is used? Here is some macro that define library version? Or some files that are not in v1 and are in v2 (and will be available all the time)?

vovagorodok avatar Oct 06 '25 11:10 vovagorodok

Sorry for adding it here, maybe better place is Discussion session

vovagorodok avatar Oct 06 '25 11:10 vovagorodok

You could probably use #ifdef CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED as that was added in 2.x

h2zero avatar Oct 07 '25 13:10 h2zero

Maybe will be nice to add something like

#define NIMBLE_VERSION_MAJOR 2
#define NIMBLE_VERSION_MINOR 3
#define NIMBLE_VERSION_PATCH 6

#define NIMBLE_VERSION (NIMBLE_VERSION_MAJOR * 10000 + NIMBLE_VERSION_MINOR * 100 + NIMBLE_VERSION_PATCH)

#if NIMBLE_VERSION >= 020306
    // Use new feature introduced in 2.3.6
#endif

#if NIMBLE_VERSION_MAJOR == 2
    // Use new feature introduced in 2.x
#endif

Or you don't like idea to maintain such macros?

vovagorodok avatar Oct 07 '25 15:10 vovagorodok

Yes, I was contemplating this some time ago, just haven't implemented it yet

h2zero avatar Oct 12 '25 21:10 h2zero

Added

    #if !defined(CONFIG_NIMBLE_MAX_CONNECTIONS) && \
        !defined(CONFIG_NIMBLE_CPP_DEBUG_ASSERT_ENABLED)
        #define BLE_OTA_BLE_LIB_NIM_BLE_ARDUINO_V1
    #else
        #define BLE_OTA_BLE_LIB_NIM_BLE_ARDUINO_V2
    #endif

as temporary solution.

Notice that __has_include doesn't work correctly in ArduinoIDE. PlatformIO handles it better

vovagorodok avatar Oct 12 '25 22:10 vovagorodok

Notice that __has_include doesn't work correctly in ArduinoIDE. PlatformIO handles it better

I did not know this, I see it is used in the core as well so I would expect it to work fine?

h2zero avatar Oct 15 '25 22:10 h2zero

As I correctly understand in ArduinoIDE libraries are compiled without considering of full project/sketch. That's why was added exception (autodetection turned off) for it in https://github.com/vovagorodok/ArduinoBleOTA/blob/main/src/BleOtaDefines.h

#if !defined(PLATFORMIO) && !defined(BLE_OTA_BLE_LIB_PREDEFINED)
    #include <ArduinoBLE.h>
    #define BLE_OTA_BLE_LIB_ARDUINO_BLE
    #define BLE_OTA_LIB_ARDUINO_BLE
    #define BLE_OTA_BLE_LIB_PREDEFINED
#endif

In PlatformIO it looks like
https://community.platformio.org/t/automatic-ble-library-detection/52762

vovagorodok avatar Oct 16 '25 10:10 vovagorodok