Skip to content

Conversation

@markmentovai
Copy link

In espressif/arduino-esp32@822f252, first released in https://github.com/espressif/arduino-esp32/releases/tag/3.0.0, some string types in BLEDevice changed from std::string (a C++ library string) to String (an Arduino string). See espressif/arduino-esp32#8724.

This change makes BleKeyboard compatible with both the old and new versions of arduino-esp32, by detecting the version in use at compile time.

I am aware that many other pull requests are open that intend to address this problem, but as far as I can see, they all migrate straight to the arduino-esp32 3.0.0 interface, without any affordance for compatibility with older versions of arduino-esp32. This pull request makes BleKeyboard compatible with both the old and new interfaces. This pull request also uses the known size of a std::string when constructing a String.

In espressif/arduino-esp32 822f252b353c (2023-10-06), first released in
arduino-esp32 3.0.0 (2024-05-27), some string types in BLEDevice changed
from std::string (a C++ library string) to String (an Arduino string).
See espressif/arduino-esp32#8724.

This change makes BleKeyboard compatible with both the old and new
versions of arduino-esp32, by detecting the version in use at compile
time.
markmentovai added a commit to markmentovai/open-smartwatch-os that referenced this pull request Jan 5, 2026
This updates the codebase for compatibility with
https://github.com/pioarduino/platform-espressif32 55.03.35, which in
turn uses https://github.com/espressif/arduino-esp32 3.3.5. An
additional change along the lines of
T-vK/ESP32-BLE-Keyboard#382 is needed to make
lib/ESP32-BLE-Keyboard compatibile as well.

This does not change the platform definition, which remains at
https://github.com/platformio/platform-espressif32 6.8.1, using
https://github.com/espressif/arduino-esp32 2.0.17. All changed code is
compatible with both versions.
markmentovai added a commit to markmentovai/open-smartwatch-os that referenced this pull request Jan 5, 2026
This updates the codebase for compatibility with
https://github.com/pioarduino/platform-espressif32 55.03.35, which in
turn uses https://github.com/espressif/arduino-esp32 3.3.5. An
additional change along the lines of
T-vK/ESP32-BLE-Keyboard#382 is needed to make
lib/ESP32-BLE-Keyboard compatibile as well.

This does not change the platform definition, which remains at
https://github.com/platformio/platform-espressif32 6.8.1, using
https://github.com/espressif/arduino-esp32 2.0.17. All changed code is
correct regardless of Arduino-ESP version, and all changed code is
compatible with both versions.
markmentovai added a commit to markmentovai/open-smartwatch-os that referenced this pull request Jan 6, 2026
This updates the codebase for compatibility with
https://github.com/pioarduino/platform-espressif32 55.03.35, which in
turn uses https://github.com/espressif/arduino-esp32 3.3.5. An
additional change along the lines of
T-vK/ESP32-BLE-Keyboard#382 is needed to make
lib/ESP32-BLE-Keyboard compatibile as well.

This does not change the platform definition, which remains at
https://github.com/platformio/platform-espressif32 6.8.1, using
https://github.com/espressif/arduino-esp32 2.0.17. All changed code is
correct regardless of Arduino-ESP version, and all changed code is
compatible with both versions.
@MOLLYMAN01
Copy link

sorry it did realy working:

n:\Doks\Arduino\libraries\ESP32C3_BLE_Keyboard\BleKeyboard.cpp: In member function 'void BleKeyboard::begin()':
n:\Doks\Arduino\libraries\ESP32C3_BLE_Keyboard\BleKeyboard.cpp:109:19: error: cannot convert 'String' to 'const std::string&' {aka 'const std::__cxx11::basic_string<char>&'}
  109 |   BLEDevice::init(String(deviceName.c_str(), deviceName.size()));
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                   |
      |                   String

@markmentovai
Copy link
Author

@MOLLYMAN01 #382 (comment):

sorry it did realy working:

Which version of arduino-esp32 are you using?

@MOLLYMAN01
Copy link

MOLLYMAN01 commented Jan 8, 2026

3.3.4
3.3.5 is avaiable but not possible to install ( Failed to install platform: 'esp32:esp32:3.3.5'.
Error: 4 DEADLINE_EXCEEDED: context deadline exceeded (Client.Timeout or context cancellation while reading body))

@markmentovai
Copy link
Author

@MOLLYMAN01 #382 (comment):

3.3.4 3.3.5 is avaiable but not possible to install ( Failed to install platform: 'esp32:esp32:3.3.5'. Error: 4 DEADLINE_EXCEEDED: context deadline exceeded (Client.Timeout or context cancellation while reading body))

Maybe ESP_ARDUINO_VERSION_MAJOR is not defined in your case?

Can you try adding

#include "esp_arduino_version.h"

to the top of BleKeyboard.cpp?

@MOLLYMAN01
Copy link

i have do it:

#include "BleKeyboard.h"
#include "esp_arduino_version.h
```"


#if ESP_ARDUINO_VERSION_MAJOR < 3
BLEDevice::init(deviceName);
#else
BLEDevice::init(String(deviceName.c_str(), deviceName.size()));
#endif
BLEServer* pServer = BLEDevice::createServer();
pServer->setCallbacks(this);

hid = new BLEHIDDevice(pServer);
inputKeyboard = hid->inputReport(KEYBOARD_ID); // <-- input REPORTID from report map
outputKeyboard = hid->outputReport(KEYBOARD_ID);
inputMediaKeys = hid->inputReport(MEDIA_KEYS_ID);

outputKeyboard->setCallbacks(this);

#if ESP_ARDUINO_VERSION_MAJOR < 3
hid->manufacturer()->setValue(deviceManufacturer);
#else
hid->manufacturer()->setValue(String(deviceManufacturer.c_str(), deviceManufacturer.size()));
#endif


N:\Doks\Arduino\libraries\ESP32C3_BLE_Keyboard\examples\SendKeyStrokes\BleKeyboard.cpp: In member function 'void BleKeyboard::begin()':
N:\Doks\Arduino\libraries\ESP32C3_BLE_Keyboard\examples\SendKeyStrokes\BleKeyboard.cpp:110:19: error: cannot convert 'String' to 'const std::string&' {aka 'const std::__cxx11::basic_string&'}
110 | BLEDevice::init(String(deviceName.c_str(), deviceName.size()));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| String

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants