Bluetooth Controller Setup


Bluetooth Controller Configuration

Our next step involves initializing a Bluetooth Controller with default settings. This is done through the use of a predefined Configuration Structure which has the default settings:

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); // default cfg
ret = esp_bt_controller_init(&bt_cfg); // Struct which contains all the args

As per convention, ret is then used in potential error logging, and premature returning ensues:

if (ret) {
    ESP_LOGE(GATTS_TAG, "%s initialize controller failed", __func__);
    return;
}

Bluetooth Controller Enabling

After we have successfully configured our Bluetooth Controller, we can then focus on actually enabling the Bluetooth Controller:

ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
    ESP_LOGE(GATT_TAG, "%s Error occurred while enabling Bluetooth controller", __func__);
    return;
}

Bluedroid Setup


Next, we concern ourselves with the setup of the Bluedroid Stack, which includes the common definitions and APIs for both BT Classic and BLE. It is initialized and enabled by using these two functions:

Bluedroid Initialization

Firstly, we enable the Bluedroid stack through the use of esp_bluedroid_init():

ret = esp_bluedroid_init();
if (ret) {
    ESP_LOGE(GATT_TAG, "%s Error occurred while initializing Bluedroid stack", __func__);
    return;
}

Bluedroid Enabling

Next, we enable the Bluedroid stack using esp_bluedroid_enable():

ret = esp_bluedroid_enable();
if (ret) {
    ESP_LOGE(GATT_TAG, "%s Error occurred while enabling Bluedroid Stack", __func__);
    return;
}