ESP32 Bluetooth Serial and BLE server running together
There is a new kid coming into the arduino-esp32 world. A Bluetooth Serial library that allows you to use Bluetooth (not BLE) like a serial port. This way you can connect the ESP32 over Bluetooth to an Android app like theĀ Serial Bluetooth Terminal and use it as a serial in/out interface.
The library is not yet in the arduino-esp32 repository, but I grabbed a preview from copercini’s repo. It is working fine, I could connect my Android tablet to my ESP32 over Bluetooth and send and receive messages between the two.
Now I wanted to integrate this into my weather station project, where I have already a BLE server running. And it didn’t work. I could get either the BLE server or the Bluetooth serial to work, but not both together.
The mistake I made was that I tried to initialize both in separate function calls. First it was like calling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
void initBLE() { // Initialize BLE BLEDevice::init(apName); BLEDevice::setPower(ESP_PWR_LVL_P7); // Create BLE Server pServer = BLEDevice::createServer(); // ... // initialize BLE callbacks and characteristics // ... // Start the service pService->start(); // Start advertising pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } |
and then calling
1 2 3 4 5 6 |
bool initBtSerial() { if (!SerialBT.begin(apName)) { return false; } return true; } |
depending on the sequence I called the two functions the system either crashed or only one of the two services were working.
After a lot of trial and error and discussions with copercini I figured out the solution. The initialization of BLE and Bluetooth Serial must somehow be at the same time. The working solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
void initBlueTooth() { // Initialize BLE BLEDevice::init(apName); BLEDevice::setPower(ESP_PWR_LVL_P7); SerialBT.begin(apName); // Create BLE Server pServer = BLEDevice::createServer(); // ... // initialize BLE callbacks and characteristics // ... // Start the service pService->start(); // Start advertising pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } |
The working sequence seems to be
1 2 3 4 5 6 7 8 |
Init BLE device => BLEDevice::init(apName); Init Bluetooth Serial => SerialBT.begin(apName); Then do the rest of the BLE initialisation like - create server - add callbacks to server - add characteristics - start server - start advertising |
The full code of the initialization routine can be found in my Github repo.







23:17
First I want to say I’m a big fan of your open source work. I use actively your ESP32 WiFi Ble in one of my projects:
https://github.com/martinberlin/Remora
And I’m also making an Android application but I do not know have, so I’m making something similar based on your examples (And still supporting your ESP32 WiFi BLE app) but in Cordova. Also open source so other people can use it
Just wanted to leave you my contact details to get in touch. And kind regards from another developer and electronics fan living in Berlin