static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) { uint16_t conn_handle = p_ble_evt->evt.gap_evt.conn_handle; uint16_t role = ble_conn_state_role(conn_handle); #if __USER_1_DEBUG__ test_printf("role = %d\r\n", role); #endif // Based on the role this device plays in the connection, dispatch to the right handler. if (role == BLE_GAP_ROLE_PERIPH || ble_evt_is_advertising_timeout(p_ble_evt)) { ble_evt_dispatch(p_ble_evt, p_context); } else if ((role == BLE_GAP_ROLE_CENTRAL) || (p_ble_evt->header.evt_id == BLE_GAP_EVT_ADV_REPORT)) { ble_module_central_evt(p_ble_evt, p_context); } }
时间: 2023-10-23 13:10:54 浏览: 122
这段代码实现了一个 BLE 事件处理函数,当发生 BLE 事件时,该函数会被调用。函数首先通过连接句柄获取 BLE 设备的角色(是从设备还是主设备),然后根据角色类型分发事件到不同的事件处理函数中。
如果该设备是从设备(BLE_GAP_ROLE_PERIPH),或者广播超时(ble_evt_is_advertising_timeout(p_ble_evt)),则直接调用 ble_evt_dispatch() 函数处理该事件。
如果该设备是主设备(BLE_GAP_ROLE_CENTRAL),或者事件 ID 是 BLE_GAP_EVT_ADV_REPORT,则调用 ble_module_central_evt() 函数处理该事件。
其中,p_ble_evt 是一个 BLE 事件结构体指针,包含了 BLE 事件的各种信息,而 p_context 则是一个用户定义的上下文指针,可以用来传递一些额外的参数或数据。
相关问题
nordic函数库里面的sd_ble_gattc_read()函数的参数,并给出实际调用例子
nordic函数库里面的sd_ble_gattc_read()函数的参数如下:
```c
uint32_t sd_ble_gattc_read(uint16_t conn_handle, uint16_t handle, uint16_t offset, uint8_t *p_data, uint16_t *p_len, uint8_t options);
```
其中,参数的含义和作用如下:
- `conn_handle`:连接句柄,表示要读取哪一个连接上的属性值。
- `handle`:属性句柄,表示要读取哪一个属性的值。
- `offset`:偏移量,表示从属性值的哪一个位置开始读取数据。
- `p_data`:数据指针,用于存储读取到的属性值数据。
- `p_len`:数据长度指针,用于存储读取到的属性值数据的长度。
- `options`:选项,用于控制读取操作的行为,例如是否使用长特征值读取等。
下面是一个实际调用例子:
```c
#include "ble_gattc.h"
// 定义回调函数
void gattc_read_response_handler(ble_gattc_evt_t const *p_evt)
{
// 处理读取结果
}
// 在合适的地方调用 sd_ble_gattc_read 函数
uint16_t handle = 0x0002; // 要读取的属性句柄
uint8_t data[20]; // 存储读取结果的缓冲区
uint16_t len = 0; // 存储读取结果的长度
uint16_t conn_handle = 0x0001; // 连接句柄
uint8_t options = 0; // 读取选项
ret_code_t err_code = sd_ble_gattc_read(conn_handle, handle, 0, data, &len, options);
if (err_code != NRF_SUCCESS) {
// 处理错误
}
// 注册回调函数
err_code = sd_ble_gattc_register_handles_read_response(gattc_read_response_handler);
if (err_code != NRF_SUCCESS) {
// 处理错误
}
```
在上述代码中,我们首先定义了一个回调函数 `gattc_read_response_handler`,用于处理读取操作完成后的结果。然后在合适的地方调用 `sd_ble_gattc_read` 函数,传入要读取的属性句柄、缓冲区、连接句柄和选项等参数。最后,我们还注册了回调函数,以便在读取操作完成后能够及时处理结果。
esp32 蓝牙启动流程_ESP32教你简单进行蓝牙串口调试
ESP32的蓝牙启动流程如下:
1. 初始化蓝牙协议栈
2. 配置蓝牙参数,如设备名称、服务UUID等
3. 注册蓝牙事件回调函数
4. 启动蓝牙广播
5. 开启蓝牙可见性
6. 等待连接
以下是一个简单的ESP32蓝牙串口调试的示例:
```C
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#define GATTS_TAG "GATTS_DEMO"
#define TEST_DEVICE_NAME "ESP32_BLE_UART"
#define TEST_MANUFACTURER_DATA_LEN 17
/* The max length of characteristic value. When the gatt client write or prepare write,
* the data length must be less than MAX_VALUE_LENGTH.
*/
#define MAX_VALUE_LENGTH 500
/* Declare global variable */
static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x4c, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD};
static uint8_t test_service_uuid128[32] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x13, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x14, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_service_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x01, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x01, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_char_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x02, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x02, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static esp_gatt_char_prop_t test_property = 0;
static uint8_t char1_str[] = {0x11,0x22,0x33};
static esp_attr_value_t gatts_demo_char1_val = {
.attr_max_len = MAX_VALUE_LENGTH,
.attr_len = sizeof(char1_str),
.attr_value = char1_str,
};
static uint16_t gatts_demo_handle_table[3];
/* Full Database Description - Used to add attributes into the database */
static const esp_gatts_attr_db_t gatt_db[HRS_IDX_NB] =
{
// Service Declaration
[IDX_SVC] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
sizeof(test_service_uuid), sizeof(test_service_uuid), (uint8_t *)&test_service_uuid},
ESP_GATT_UUID_PRI_SERVICE,
ESP_GATT_PERM_READ,
sizeof(test_service_uuid),
sizeof(test_service_uuid),
(uint8_t *)&test_service_uuid,
0
},
/* Characteristic Declaration */
[IDX_CHAR_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&test_property},
ESP_GATT_UUID_CHAR_DECLARE,
ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,
CHAR_DECLARATION_SIZE,
(uint8_t *)&test_property,
0
},
/* Characteristic Value */
[IDX_CHAR_VAL_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_128, (uint8_t *)&test_char_uuid, ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH, sizeof(gatts_demo_char1_val), gatts_demo_char1_val.attr_value},
ESP_UUID_LEN_128,
ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH,
sizeof(gatts_demo_char1_val),
gatts_demo_char1_val.attr_value,
0
},
};
static esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = true,
.include_txpower = true,
.min_interval = 0x20,
.max_interval = 0x40,
.appearance = 0x00,
.manufacturer_len = TEST_MANUFACTURER_DATA_LEN,
.p_manufacturer_data = test_manufacturer,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = sizeof(test_service_uuid),
.p_service_uuid = test_service_uuid,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
.peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE(GATTS_TAG, "advertising start failed");
}
break;
default:
break;
}
}
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
switch (event) {
case ESP_GATTS_REG_EVT:
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
esp_ble_gap_config_adv_data(&adv_data);
break;
case ESP_GATTS_CREAT_ATTR_TAB_EVT:
if (param->add_attr_tab.status != ESP_GATT_OK){
ESP_LOGE(GATTS_TAG, "create attribute table failed, error code=0x%x", param->add_attr_tab.status);
}
else if (param->add_attr_tab.num_handle != HRS_IDX_NB) {
ESP_LOGE(GATTS_TAG, "create attribute table abnormally, num_handle (%d) \
doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, HRS_IDX_NB);
}
else {
ESP_LOGI(GATTS_TAG, "create attribute table successfully, the number handle = %d\n",param->add_attr_tab.num_handle);
memcpy(gatts_demo_handle_table, param->add_attr_tab.handles, sizeof(gatts_demo_handle_table));
esp_ble_gatts_start_service(gatts_demo_handle_table[IDX_SVC]);
}
break;
case ESP_GATTS_CONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONNECT_EVT");
break;
case ESP_GATTS_DISCONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT");
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GATTS_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_WRITE_EVT");
break;
case ESP_GATTS_MTU_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
break;
case ESP_GATTS_CONF_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT");
break;
case ESP_GATTS_EXEC_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_EXEC_WRITE_EVT");
break;
case ESP_GATTS_START_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_START_EVT");
break;
case ESP_GATTS_STOP_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_STOP_EVT");
break;
case ESP_GATTS_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_OPEN_EVT");
break;
case ESP_GATTS_CANCEL_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CANCEL_OPEN_EVT");
break;
case ESP_GATTS_CLOSE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CLOSE_EVT");
break;
case ESP_GATTS_LISTEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_LISTEN_EVT");
break;
case ESP_GATTS_CONGEST_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONGEST_EVT");
break;
case ESP_GATTS_UNREG_EVT:
case ESP_GATTS_DELETE_EVT:
default:
break;
}
}
void app_main()
{
esp_err_t ret;
ESP_LOGI(GATTS_TAG, "ESP_BLUETOOTH_BLE example started.");
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
return;
}
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
return;
}
ret = esp_ble_gatts_register_callback(gatts_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
return;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
return;
}
ret = esp_ble_gatts_app_register(ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
return;
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret){
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
ret = esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, HRS_IDX_NB, ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "create attr table failed, error code = %x", ret);
}
}
```
在这个示例中,我们使用了ESP-IDF提供的蓝牙协议栈和GATT Server框架,实现了一个简单的GATT Server,并且开启了蓝牙广播和可见性,使得其他蓝牙设备可以扫描到并连接我们的设备。在连接建立后,我们可以向GATT Server中的特定Characteristic写入数据,也可以从特定Characteristic读取数据。