Initialize Bluetooth Serial and BLE server
If both BLE and Bluetooth are required to operate in the code, the initialization sequence seems to be critical.
Complete initialization of a BLE server and then separate initialization of Bluetooth Serial didn’t work. The correct sequence seems to be
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
Here is a code extract that worked for me
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();
}
Full code example for the initialization can be found in this Github repo.