Browse Source

蓝牙主动上报试验(未找到)

master
zwsd 3 years ago
parent
commit
c3a007b3a6
  1. 55
      main/gatts_demo.c
  2. 26
      main/t_bluetooth.c
  3. 10
      main/t_bluetooth.h

55
main/gatts_demo.c

@ -66,9 +66,11 @@ uint32_t total = 0;
#define set_position "setPosition"
#define get_status "getStatus"
esp_gatts_cb_event_t constructor_event;
esp_gatt_if_t constructor_gatts_if;
esp_ble_gatts_cb_param_t *constructor_param;
ble_gatts_str_t ble_gatts_a_str = {
.attr_handle = 0,
.send_conn_id = 0,
.send_gatts_if = 0,
};
/***********************************************************************************************************************
* **********************************************user_function_statement********************************************** *
@ -149,20 +151,40 @@ void timer_group_init(int group, int timer, bool auto_reload, int timer_interval
***********************************************************************************************************************/
void app_main(void)
{
esp_ble_gatts_init();
esp_ble_gatts_init(&ble_gatts_a_str);
// motor_init();
// motor_encoder_init();
// timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
motor_init();
motor_encoder_init();
timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms);
// uint8_t temp[5] = "hello";
// esp_gatt_rsp_t rsp;
// memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
// rsp.attr_value.len = 4;
// rsp.attr_value.value[0] = 0x11;
// rsp.attr_value.value[1] = 0x22;
// rsp.attr_value.value[2] = 0x33;
// rsp.attr_value.value[3] = 0x44;
while (true)
{
bluetooth_gatts_try_process_data();
if (s_bluetooth_processer.actively_report_flag)
{
receipt_json_get_status();
s_bluetooth_processer.actively_report_flag = false;
}
// if (ble_gatts_a_str.attr_handle != 0)
// {
// rsp.attr_value.handle = ble_gatts_a_str.attr_handle;
// ESP_LOGI(GATTS_TAG, "%d,%d,%d", ble_gatts_a_str.attr_handle, ble_gatts_a_str.send_conn_id, ble_gatts_a_str.send_gatts_if);
// esp_ble_gatts_send_indicate(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id,
// ble_gatts_a_str.attr_handle, 5, temp, true);
// esp_ble_gatts_send_response(ble_gatts_a_str.send_gatts_if, ble_gatts_a_str.send_conn_id, 1,
// ESP_GATT_OK, &rsp);
// ets_delay_us(1000000);
// }
// esp_ble_gatts_set_attr_value(ble_gatts_a_str.constructor_param->read.handle, 5, temp);
// bluetooth_gatts_try_process_data();
// if (s_bluetooth_processer.actively_report_flag)
// {
// receipt_json_get_status();
// s_bluetooth_processer.actively_report_flag = false;
// }
}
return;
@ -417,10 +439,3 @@ void receipt_json_get_status()
cJSON_Delete(pRoot);
}
void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
constructor_event = event;
constructor_gatts_if = gatts_if;
constructor_param = param;
}

26
main/t_bluetooth.c

@ -1,5 +1,14 @@
#include "t_bluetooth.h"
/***********************************************************************************************************************
* *******************************************************user******************************************************** *
***********************************************************************************************************************/
static ble_gatts_str_t *ble_gatts_a_structer;
/***********************************************************************************************************************
* ******************************************************************************************************************* *
***********************************************************************************************************************/
/// Declare the static function
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
@ -70,8 +79,6 @@ static esp_ble_adv_data_t scan_rsp_data = {
.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,
@ -252,7 +259,6 @@ void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble
static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
// gatts_profile_a_constructor(event, gatts_if, param);
switch (event)
{
case ESP_GATTS_REG_EVT:
@ -303,6 +309,7 @@ static void gatts_profile_a_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
break;
case ESP_GATTS_READ_EVT:
{
gatts_profile_a_constructor(event, gatts_if, param);
ESP_LOGI(GATTS_TAG, "GATT_READ_EVT, conn_id %d, trans_id %d, handle %d\n", param->read.conn_id, param->read.trans_id, param->read.handle);
esp_gatt_rsp_t rsp;
memset(&rsp, 0, sizeof(esp_gatt_rsp_t));
@ -470,7 +477,7 @@ static void gatts_profile_b_event_handler(esp_gatts_cb_event_t event, esp_gatt_i
{
ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
ESP_LOGI(GATTS_TAG,"ye!!!!!!!!!");
ESP_LOGI(GATTS_TAG, "ye!!!!!!!!!");
// start_receive_data_to_buffer(param->write.len, param->write.value);
// for (int i = 0; i < param->write.len; i++)
// {
@ -601,10 +608,12 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
} while (0);
}
void esp_ble_gatts_init()
void esp_ble_gatts_init(ble_gatts_str_t *ble_gatts_a_str)
{
esp_err_t ret;
ble_gatts_a_structer = ble_gatts_a_str;
// Initialize NVS.
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
@ -680,3 +689,10 @@ void esp_ble_gatts_init()
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
}
void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
ble_gatts_a_structer->attr_handle = param->read.handle;
ble_gatts_a_structer->send_conn_id = param->read.conn_id;
ble_gatts_a_structer->send_gatts_if = gatts_if;
}

10
main/t_bluetooth.h

@ -68,7 +68,15 @@ typedef struct
int prepare_len;
} prepare_type_env_t;
typedef struct ble_gatts_str
{
uint8_t send_gatts_if;
uint16_t send_conn_id;
uint16_t attr_handle;
} ble_gatts_str_t;
void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
void example_exec_write_event_env(prepare_type_env_t *prepare_write_env, esp_ble_gatts_cb_param_t *param);
void esp_ble_gatts_init();
void esp_ble_gatts_init(ble_gatts_str_t* ble_gatts_a_str);
void gatts_profile_a_constructor(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
Loading…
Cancel
Save