3#if CONFIG_BT_BLE_ENABLED
17#include <esp32-hal-bt.h>
18#include <esp_bt_main.h>
19#include <esp_gap_ble_api.h>
20#include <esp_gatt_common_api.h>
24const char *midi_ble_name =
"Control Surface (BLE)";
29 if (!btStarted() && !btStart())
34 esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
35 if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
36 ret = esp_bluedroid_init();
38 ESP_LOGE(
"MIDIBLE",
"Init bluetooth failed: %s",
39 esp_err_to_name(ret));
44 if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
45 ret = esp_bluedroid_enable();
47 ESP_LOGE(
"MIDIBLE",
"Enable bluetooth failed: %s",
48 esp_err_to_name(ret));
53 ret = esp_ble_gatt_set_local_mtu(ESP_GATT_MAX_MTU_SIZE);
55 ESP_LOGE(
"MIDIBLE",
"set local MTU failed: %s", esp_err_to_name(ret));
59 ret = esp_ble_gap_set_device_name(midi_ble_name);
61 ESP_LOGE(
"MIDIBLE",
"set device name failed: %s", esp_err_to_name(ret));
67 ESP_LOGE(
"MIDIBLE",
"gap register error: %s", esp_err_to_name(ret));
73 ESP_LOGE(
"MIDIBLE",
"GATTS register error: %s", esp_err_to_name(ret));
79 ESP_LOGE(
"MIDIBLE",
"GATTS app register error: %s",
80 esp_err_to_name(ret));
87 esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND;
89 esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
90 uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
92 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req,
95 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_AUTHEN_REQ_MODE: %s",
96 esp_err_to_name(ret));
100 ret = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap,
103 ESP_LOGE(
"MIDIBLE",
"Failed to set ESP_BLE_SM_IOCAP_MODE: %s",
104 esp_err_to_name(ret));
108 ret = esp_ble_gap_set_security_param(
109 ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option,
110 sizeof(auth_option));
113 "Failed to set ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH: %s",
114 esp_err_to_name(ret));
118 vTaskDelay(100 / portTICK_PERIOD_MS);
123bool midi_deinit(
void) { assert(!
"Not implemented"); }
Handlers for Bluetooth and BLE events.
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
Declarations of internal functions for the MIDI over BLE system, used in the midi-*....
uint16_t midi_get_app_id(void)
bool midi_deinit()
Cleanup the MIDI BLE application and de-initialize the Bluetooth stack.
bool midi_init()
Initialize the Bluetooth stack and register the MIDI BLE application with the Bluedroid driver.
void set_midi_ble_name(const char *name)
Set the name of the BLE device. Must be set before calling midi_init().