Commit 68c9a9f1 authored by 李茂军's avatar 李茂军

解决冲突

parents 2fc7171c 79378b94
......@@ -9,5 +9,6 @@ idf_component_register(SRCS "mcudata.c" "app_BT_User.c" "bt_app_main.c" "bt_app_
"app_Ble_User.c"
"main_user.c"
"../source/wifi"
"gattc_demo.c"
INCLUDE_DIRS "."
EMBED_TXTFILES ${project_dir}/server_certs/ca_cert.pem)
......@@ -215,7 +215,7 @@ extern void wifiServiceStart(void);
uint32_t SwitchMode=0;
static void Protocol_UartHandle(const Protocol_Data_t *pData)//esp接收底板数据
{
if ( pData->CmdID == 0x20 )//收到底板发送的数据后,传状态
if ( pData->CmdID == 0x20 )//收到底板发送的数据后,才会发送0x12,0x01,0x02的id
{
//printf("0x20 is get\r\n");
if(Prot_User.State < Prot_Start)
......@@ -243,22 +243,22 @@ static void Protocol_UartHandle(const Protocol_Data_t *pData)//esp接收底板
BT_User.HF_Ctrl = pData->Data[0];//esp32接收到的数据
}
if(strncmp((const char *) ble_uerid, (const char *)&(pData->Data[1]), 32) != 0)
{
memcpy(( uint8_t * )ble_uerid, (uint8_t *)&(pData->Data[1]), 32);
}
// if(strncmp((const char *) ble_uerid, (const char *)&(pData->Data[1]), 32) != 0)
// {
// memcpy(( uint8_t * )ble_uerid, (uint8_t *)&(pData->Data[1]), 32);
// }
// esp_log_buffer_hex(TAG, ble_uerid, 32);
// esp_log_buffer_hex(TAG, &(pData->Data[1]), 32);
// // esp_log_buffer_hex(TAG, ble_uerid, 32);
// // esp_log_buffer_hex(TAG, &(pData->Data[1]), 32);
if(Ble_User.UseridUpdate == 0)
{
bsp_Ble_Init();
// if(Ble_User.UseridUpdate == 0)
// {
// bsp_Ble_Init();
printf("get uuid and init ble!!! \r\n");
}
// printf("get uuid and init ble!!! \r\n");
// }
Ble_User.UseridUpdate = 0xAA;
// Ble_User.UseridUpdate = 0xAA;
}
}
}
......@@ -334,19 +334,26 @@ void Uart_Send_Id12_Pro(void )//收到底板回复的0x20后,发送蓝牙类型
void Uart_Send_Id01_Pro(void)
{
uint8_t ID01[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
if(Ble_User.Tpms_Updat)
uint8_t ID01[7] = {0x00,0x00,0x00,0x00,0x00,0x00};
//if(Ble_User.Tpms_Updat)
{
ID01[0] = Ble_Tpms_Data.Tpms_Sts;
ID01[1] = Ble_Tpms_Data.Tpms_Turn;
ID01[2] = Ble_Tpms_Data.Tpms_Dte[0];
ID01[3] = Ble_Tpms_Data.Tpms_Dte[1];
ID01[4] = Ble_Tpms_Data.Tpms_Dte[2];
ID01[5] = Ble_Tpms_Data.Tpms_Dte[3];
// ID01[0] = Ble_Tpms_Data.Tpms_Sts;
// ID01[1] = Ble_Tpms_Data.Tpms_Turn;
// ID01[2] = Ble_Tpms_Data.Tpms_Dte[0];
// ID01[3] = Ble_Tpms_Data.Tpms_Dte[1];
// ID01[4] = Ble_Tpms_Data.Tpms_Dte[2];
// ID01[5] = Ble_Tpms_Data.Tpms_Dte[3];
ID01[0] = 0xFF;//胎压状态
ID01[1] = 1;//前轮高字节气压值
ID01[2] = 200;//前轮低字节气压值
ID01[3] = 0x28;//前轮胎压温度
ID01[4] = 1;//后轮高字节气压值
ID01[5] = 45;//后轮低字节气压值
ID01[6] = 0xAB;//后轮胎压温度
//printf("转向编码:%d\r\n",Ble_Tpms_Data.Tpms_Turn);
}
Protocol_Send(0x01,ID01,6);
Protocol_Send(0x01,ID01,7);
}
......@@ -369,20 +376,21 @@ void Prot_Send_Msg_Process(void )
// printf("send 0x10 \r\n");
}
if(Prot_User.State >= Prot_Start)
//printf("Prot_User.State = %d\r\n",Prot_User.State);
if(Prot_User.State >= Prot_Start)//收到底板发送的0x20后,才执行
{
if(Prot_User.TimeDelay == 0)
{
Uart_Send_Id12_Pro();
Prot_User.TimeDelay++;
// printf("send 0x12 \r\n");
//printf("send 0x12 \r\n");
}
else
{
Uart_Send_Id01_Pro();
Uart_Send_Id02_Pro();
Prot_User.TimeDelay = 0;
//printf("send 0x01 0x02 \r\n");
//printf("send 0x01 0x02 \r\n");
}
}
}
......
......@@ -75,62 +75,7 @@ uint8_t Ble_Get_User_Info(void )
{
uint8_t i = 0;
uint8_t ret = 0;
// for(i = 24; i<32;i++)
// {
// ble_uerid[i] = 0x30+i-24;
// ble_pwd[i] = 0x30+i-24;
// }
// ble_uerid[0] = 'v';
// ble_uerid[1] = 'i';
// ble_uerid[2] = 'v';
// ble_uerid[3] = 'a';
// ble_uerid[4] = 'v';
// ble_uerid[5] = 'i';
// ble_uerid[6] = 'v';
// ble_uerid[7] = 'a';
// ble_uerid[8] = 'v';
// ble_uerid[9] = 'i';
// ble_uerid[10] = 'v';
// ble_uerid[11] = 'a';
// ble_uerid[12] = 'v';
// ble_uerid[13] = 'i';
// ble_uerid[14] = 'v';
// ble_uerid[15] = 'a';
// ble_uerid[16] = 'v';
// ble_uerid[17] = 'i';
// ble_uerid[18] = 'v';
// ble_uerid[19] = 'a';
// ble_uerid[20] = 'v';
// ble_uerid[21] = 'i';
// ble_uerid[22] = 'v';
// ble_uerid[23] = 'a';
// ble_pwd[0] = 'C';
// ble_pwd[1] = 'C';
// ble_pwd[2] = 'N';
// ble_pwd[3] = 'C';
// ble_pwd[4] = 'v';
// ble_pwd[5] = 'i';
// ble_pwd[6] = 'v';
// ble_pwd[7] = 'a';
// ble_pwd[8] = 'v';
// ble_pwd[9] = 'i';
// ble_pwd[10] = 'v';
// ble_pwd[11] = 'a';
// ble_pwd[12] = 'v';
// ble_pwd[13] = 'i';
// ble_pwd[14] = 'v';
// ble_pwd[15] = 'a';
// ble_pwd[16] = 'v';
// ble_pwd[17] = 'i';
// ble_pwd[18] = 'v';
// ble_pwd[19] = 'a';
// ble_pwd[20] = 'v';
// ble_pwd[21] = 'i';
// ble_pwd[22] = 'v';
// ble_pwd[23] = 'a';
if(Ble_User.UseridUpdate)
{
......
......@@ -9,4 +9,5 @@
void bsp_BT_Init(void);
uint8_t app_BT_Get_BondDev(void );
void app_tpms_main(void);
#endif
\ No newline at end of file
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
/****************************************************************************
*
* This demo showcases BLE GATT client. It can scan BLE devices and connect to one device.
* Run the gatt_server demo, the client demo will automatically connect to the gatt_server demo.
* Client demo will enable gatt_server's notify after connection. The two devices will then exchange
* data.
*
****************************************************************************/
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#include "esp_gattc_api.h"
#include "esp_gatt_defs.h"
#include "esp_bt_main.h"
#include "esp_gatt_common_api.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#define GATTC_TAG "GATTC_DEMO"
#define REMOTE_SERVICE_UUID 0x00FF
#define REMOTE_NOTIFY_CHAR_UUID 0xFF01
#define PROFILE_NUM 1
#define PROFILE_A_APP_ID 0
#define INVALID_HANDLE 0
static const char remote_device_name[] = "ESP_GATTS_DEMO";
static bool connect = false;
static bool get_server = false;
static esp_gattc_char_elem_t *char_elem_result = NULL;
static esp_gattc_descr_elem_t *descr_elem_result = NULL;
/* Declare static functions */
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param);
//服务
static esp_bt_uuid_t remote_filter_service_uuid = {
.len = ESP_UUID_LEN_16,
.uuid = {.uuid16 = REMOTE_SERVICE_UUID,},
};
//特性
static esp_bt_uuid_t remote_filter_char_uuid = {
.len = ESP_UUID_LEN_16,
.uuid = {.uuid16 = REMOTE_NOTIFY_CHAR_UUID,},
};
//描述符
static esp_bt_uuid_t notify_descr_uuid = {
.len = ESP_UUID_LEN_16,
.uuid = {.uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,},
};
static esp_ble_scan_params_t ble_scan_params = {
.scan_type = BLE_SCAN_TYPE_ACTIVE,//主动扫描
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,//
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,//扫描所有设备
.scan_interval = 0x50,
.scan_window = 0x30,
.scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE//禁用重复扫描
};
//esp_gattc_cb_t gattc_cb;:这是一个回调函数类型,用于处理来自GATT客户端的事件。当某些事件(如连接状态改变、属性读写完成等)发生时,系统会调用这个回调函数,并传递相关的事件信息。
//uint16_t gattc_if;: 这通常是GATT客户端的接口ID,用于在多个GATT客户端实例之间进行区分。
//uint16_t app_id;: 应用程序ID,用于标识这个GATT客户端实例所属的应用程序。
//uint16_t conn_id;: 连接ID,用于标识特定的BLE连接。当设备之间建立BLE连接时,系统会为该连接分配一个唯一的ID。
//uint16_t service_start_handle, service_end_handle;:这两个字段定义了远程设备上一个服务(Service)的句柄范围。句柄是GATT协议中用于唯一标识属性(如服务、特性、描述符)的数字。
//uint16_t char_handle;: 这是远程设备上某个特性(Characteristic)的句柄。特性是服务中的一个属性,它包含了实际的数据值。
//esp_bd_addr_t remote_bda;:这是一个结构体,用于存储远程设备的蓝牙设备地址(BD_ADDR)。这个地址是唯一的,用于在BLE网络中标识和定位远程设备。
struct gattc_profile_inst {
esp_gattc_cb_t gattc_cb;
uint16_t gattc_if;
uint16_t app_id;
uint16_t conn_id;
uint16_t service_start_handle;
uint16_t service_end_handle;
uint16_t char_handle;
esp_bd_addr_t remote_bda;
};
/* One gatt-based profile one app_id and one gattc_if, this array will store the gattc_if returned by ESP_GATTS_REG_EVT */
static struct gattc_profile_inst gl_profile_tab[PROFILE_NUM] = {
[PROFILE_A_APP_ID] = {
.gattc_cb = gattc_profile_event_handler,
.gattc_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
},
};
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
{
esp_ble_gattc_cb_param_t *p_data = (esp_ble_gattc_cb_param_t *)param;
switch (event) {
case ESP_GATTC_REG_EVT:// 当GATT客户端注册成功时触发
ESP_LOGI(GATTC_TAG, "REG_EVT");
esp_err_t scan_ret = esp_ble_gap_set_scan_params(&ble_scan_params);
if (scan_ret){
ESP_LOGE(GATTC_TAG, "set scan params error, error code = %x", scan_ret);
}
break;
case ESP_GATTC_CONNECT_EVT:{// 当GATT客户端连接成功时触发
ESP_LOGI(GATTC_TAG, "ESP_GATTC_CONNECT_EVT conn_id %d, if %d", p_data->connect.conn_id, gattc_if);
gl_profile_tab[PROFILE_A_APP_ID].conn_id = p_data->connect.conn_id;
memcpy(gl_profile_tab[PROFILE_A_APP_ID].remote_bda, p_data->connect.remote_bda, sizeof(esp_bd_addr_t));
ESP_LOGI(GATTC_TAG, "REMOTE BDA:");
esp_log_buffer_hex(GATTC_TAG, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, sizeof(esp_bd_addr_t));
esp_err_t mtu_ret = esp_ble_gattc_send_mtu_req (gattc_if, p_data->connect.conn_id);
if (mtu_ret){
ESP_LOGE(GATTC_TAG, "config MTU error, error code = %x", mtu_ret);
}
break;
}
case ESP_GATTC_OPEN_EVT://打开时触发
if (param->open.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "open failed, status %d", p_data->open.status);
break;
}
ESP_LOGI(GATTC_TAG, "open success");
break;
case ESP_GATTC_DIS_SRVC_CMPL_EVT:
if (param->dis_srvc_cmpl.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "discover service failed, status %d", param->dis_srvc_cmpl.status);
break;
}
ESP_LOGI(GATTC_TAG, "discover service complete conn_id %d", param->dis_srvc_cmpl.conn_id);
esp_ble_gattc_search_service(gattc_if, param->dis_srvc_cmpl.conn_id, &remote_filter_service_uuid);
break;
case ESP_GATTC_CFG_MTU_EVT://当MTU大小配置完成时触发
if (param->cfg_mtu.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG,"config mtu failed, error status = %x", param->cfg_mtu.status);
}
ESP_LOGI(GATTC_TAG, "ESP_GATTC_CFG_MTU_EVT, Status %d, MTU %d, conn_id %d", param->cfg_mtu.status, param->cfg_mtu.mtu, param->cfg_mtu.conn_id);
break;// 当发现服务时触发
case ESP_GATTC_SEARCH_RES_EVT: {
ESP_LOGI(GATTC_TAG, "SEARCH RES: conn_id = %x is primary service %d", p_data->search_res.conn_id, p_data->search_res.is_primary);
ESP_LOGI(GATTC_TAG, "start handle %d end handle %d current handle value %d", p_data->search_res.start_handle, p_data->search_res.end_handle, p_data->search_res.srvc_id.inst_id);
if (p_data->search_res.srvc_id.uuid.len == ESP_UUID_LEN_16 && p_data->search_res.srvc_id.uuid.uuid.uuid16 == REMOTE_SERVICE_UUID)
{
ESP_LOGI(GATTC_TAG, "service found");
get_server = true;
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle = p_data->search_res.start_handle;
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle = p_data->search_res.end_handle;
ESP_LOGI(GATTC_TAG, "UUID16: %x", p_data->search_res.srvc_id.uuid.uuid.uuid16);
}
break;
}
case ESP_GATTC_SEARCH_CMPL_EVT:// 当服务搜索完成时触发
if (p_data->search_cmpl.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "search service failed, error status = %x", p_data->search_cmpl.status);
break;
}
if(p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_REMOTE_DEVICE) {
ESP_LOGI(GATTC_TAG, "Get service information from remote device");
} else if (p_data->search_cmpl.searched_service_source == ESP_GATT_SERVICE_FROM_NVS_FLASH) {
ESP_LOGI(GATTC_TAG, "Get service information from flash");
} else {
ESP_LOGI(GATTC_TAG, "unknown service source");
}
ESP_LOGI(GATTC_TAG, "ESP_GATTC_SEARCH_CMPL_EVT");
if (get_server){
uint16_t count = 0;
esp_gatt_status_t status = esp_ble_gattc_get_attr_count( gattc_if,
p_data->search_cmpl.conn_id,
ESP_GATT_DB_CHARACTERISTIC,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
INVALID_HANDLE,
&count);
if (status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
}
if (count > 0){
char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count);
if (!char_elem_result){
ESP_LOGE(GATTC_TAG, "gattc no mem");
}else{
status = esp_ble_gattc_get_char_by_uuid( gattc_if,
p_data->search_cmpl.conn_id,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
remote_filter_char_uuid,
char_elem_result,
&count);
if (status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error");
}
/* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */
if (count > 0 && (char_elem_result[0].properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)){
gl_profile_tab[PROFILE_A_APP_ID].char_handle = char_elem_result[0].char_handle;
esp_ble_gattc_register_for_notify (gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, char_elem_result[0].char_handle);
}
}
/* free char_elem_result */
free(char_elem_result);
}else{
ESP_LOGE(GATTC_TAG, "no char found");
}
}
break;
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
ESP_LOGI(GATTC_TAG, "ESP_GATTC_REG_FOR_NOTIFY_EVT");
if (p_data->reg_for_notify.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "REG FOR NOTIFY failed: error status = %d", p_data->reg_for_notify.status);
}else{
uint16_t count = 0;
uint16_t notify_en = 1;
esp_gatt_status_t ret_status = esp_ble_gattc_get_attr_count( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
ESP_GATT_DB_DESCRIPTOR,
gl_profile_tab[PROFILE_A_APP_ID].service_start_handle,
gl_profile_tab[PROFILE_A_APP_ID].service_end_handle,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error");
}
if (count > 0){
descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count);
if (!descr_elem_result){
ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem");
}else{
ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
p_data->reg_for_notify.handle,
notify_descr_uuid,
descr_elem_result,
&count);
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error");
}
/* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */
if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){
ret_status = esp_ble_gattc_write_char_descr( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
descr_elem_result[0].handle,
sizeof(notify_en),
(uint8_t *)&notify_en,
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
}
if (ret_status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "esp_ble_gattc_write_char_descr error");
}
/* free descr_elem_result */
free(descr_elem_result);
}
}
else{
ESP_LOGE(GATTC_TAG, "decsr not found");
}
}
break;
}
case ESP_GATTC_NOTIFY_EVT:// 当收到通知时触发
if (p_data->notify.is_notify){
ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive notify value:");
}else{
ESP_LOGI(GATTC_TAG, "ESP_GATTC_NOTIFY_EVT, receive indicate value:");
}
esp_log_buffer_hex(GATTC_TAG, p_data->notify.value, p_data->notify.value_len);
break;
case ESP_GATTC_WRITE_DESCR_EVT:
if (p_data->write.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "write descr failed, error status = %x", p_data->write.status);
break;
}
ESP_LOGI(GATTC_TAG, "write descr success ");
uint8_t write_char_data[35];
for (int i = 0; i < sizeof(write_char_data); ++i)
{
write_char_data[i] = i % 256;
}
esp_ble_gattc_write_char( gattc_if,
gl_profile_tab[PROFILE_A_APP_ID].conn_id,
gl_profile_tab[PROFILE_A_APP_ID].char_handle,
sizeof(write_char_data),
write_char_data,
ESP_GATT_WRITE_TYPE_RSP,
ESP_GATT_AUTH_REQ_NONE);
break;
case ESP_GATTC_SRVC_CHG_EVT: {
esp_bd_addr_t bda;
memcpy(bda, p_data->srvc_chg.remote_bda, sizeof(esp_bd_addr_t));
ESP_LOGI(GATTC_TAG, "ESP_GATTC_SRVC_CHG_EVT, bd_addr:");
esp_log_buffer_hex(GATTC_TAG, bda, sizeof(esp_bd_addr_t));
break;
}
case ESP_GATTC_WRITE_CHAR_EVT: // 当写入特性完成时触发
if (p_data->write.status != ESP_GATT_OK){
ESP_LOGE(GATTC_TAG, "write char failed, error status = %x", p_data->write.status);
break;
}
ESP_LOGI(GATTC_TAG, "write char success ");
break;
case ESP_GATTC_DISCONNECT_EVT:
connect = false;
get_server = false;
ESP_LOGI(GATTC_TAG, "ESP_GATTC_DISCONNECT_EVT, reason = %d", p_data->disconnect.reason);
break;
default:
break;
}
}
//GAP层 这些事件可能包括设备扫描结果、连接状态变化、认证请求等。
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
uint8_t *adv_name = NULL;
uint8_t adv_name_len = 0;
switch (event) {
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: {//BLE 扫描参数设置操作已经完成
//the unit of the duration is second
uint32_t duration = 30;
esp_ble_gap_start_scanning(duration);//启动扫描
break;
}
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT://用于表示扫描操作的开始或完成状态。
//scan start complete event to indicate scan start successfully or failed
if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE(GATTC_TAG, "scan start failed, error status = %x", param->scan_start_cmpl.status);
break;
}
ESP_LOGI(GATTC_TAG, "scan start success");
break;
case ESP_GAP_BLE_SCAN_RESULT_EVT: {//扫描到设备
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
switch (scan_result->scan_rst.search_evt)
{
case ESP_GAP_SEARCH_INQ_RES_EVT://用于在设备执行蓝牙扫描时报告发现的结果。在扫描过程中发现一个或多个BLE设备时,会触发这个事件
esp_log_buffer_hex(GATTC_TAG, scan_result->scan_rst.bda, 6);//尤其是当你需要查看设备地址、UUID ,Mac地址。
if(scan_result->scan_rst.bda[4] == 0x22)
{
esp_log_buffer_hex("1111111111111111111111111111111111111",scan_result->scan_rst.ble_adv,scan_result->scan_rst.adv_data_len);
ESP_LOGI(GATTC_TAG, "searched Adv Data Len %d, Scan Response Len %d", scan_result->scan_rst.adv_data_len, scan_result->scan_rst.scan_rsp_len);
adv_name = esp_ble_resolve_adv_data(scan_result->scan_rst.ble_adv,ESP_BLE_AD_TYPE_NAME_CMPL, &adv_name_len);
ESP_LOGI(GATTC_TAG, "searched Device Name Len %d", adv_name_len);
esp_log_buffer_char(GATTC_TAG, adv_name, adv_name_len);
}
#if CONFIG_EXAMPLE_DUMP_ADV_DATA_AND_SCAN_RESP
if (scan_result->scan_rst.adv_data_len > 0) {
ESP_LOGI(GATTC_TAG, "adv data:");
esp_log_buffer_hex(GATTC_TAG, &scan_result->scan_rst.ble_adv[0], scan_result->scan_rst.adv_data_len);
}
if (scan_result->scan_rst.scan_rsp_len > 0) {
ESP_LOGI(GATTC_TAG, "scan resp:");
esp_log_buffer_hex(GATTC_TAG, &scan_result->scan_rst.ble_adv[scan_result->scan_rst.adv_data_len], scan_result->scan_rst.scan_rsp_len);
}
#endif
ESP_LOGI(GATTC_TAG, "\n");
if (adv_name != NULL) {
if (strlen(remote_device_name) == adv_name_len && strncmp((char *)adv_name, remote_device_name, adv_name_len) == 0) {
ESP_LOGI(GATTC_TAG, "searched device %s\n", remote_device_name);
if (connect == false) {
connect = true;
ESP_LOGI(GATTC_TAG, "connect to the remote device.");
esp_ble_gap_stop_scanning();
esp_ble_gattc_open(gl_profile_tab[PROFILE_A_APP_ID].gattc_if, scan_result->scan_rst.bda, scan_result->scan_rst.ble_addr_type, true);
}
}
}
break;
case ESP_GAP_SEARCH_INQ_CMPL_EVT://用于控制扫描的持续时间和重启扫描
break;
default:
break;
}
break;
}
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
if (param->scan_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
ESP_LOGE(GATTC_TAG, "scan stop failed, error status = %x", param->scan_stop_cmpl.status);//扫描停止失败,进行错误处理
break;
}
ESP_LOGI(GATTC_TAG, "stop scan successfully");//扫描成功停止,可以进行后续操作,如重启扫描、连接其他设备等
break;
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:// ESP32 蓝牙设备成功或失败地停止其广告活动时,此事件会被触发。
if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS){
ESP_LOGE(GATTC_TAG, "adv stop failed, error status = %x", param->adv_stop_cmpl.status);
break;
}
ESP_LOGI(GATTC_TAG, "stop adv successfully");
break;
case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT://连接参数被更新或请求更新时触发
ESP_LOGI(GATTC_TAG, "update connection params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d",
param->update_conn_params.status,
param->update_conn_params.min_int,
param->update_conn_params.max_int,
param->update_conn_params.conn_int,
param->update_conn_params.latency,
param->update_conn_params.timeout);
break;
default:
break;
}
}
//您的代码段描述了一个esp_gattc_cb函数,该函数是ESP-IDF中BLE GATT客户端(GATT Client)的回调函数。
//该函数处理从BLE堆栈发送过来的各种事件,并且对于特定的配置文件(profile),
//它会将gattc_if(GATT客户端接口)存储起来,并调用相应配置文件的回调函数
static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
{
/* If event is register event, store the gattc_if for each profile */
//如果状态为ESP_GATT_OK(表示注册成功),它会将gattc_if存储到gl_profile_tab数组中对应app_id的条目里。
//gl_profile_tab看起来是一个全局数组,用于存储所有配置文件的信息,包括gattc_if。
if (event == ESP_GATTC_REG_EVT) {//(即GATT客户端注册事件)时,函数会检查注册状态。
if (param->reg.status == ESP_GATT_OK) {
gl_profile_tab[param->reg.app_id].gattc_if = gattc_if;
} else {
ESP_LOGI(GATTC_TAG, "reg app failed, app_id %04x, status %d",
param->reg.app_id,
param->reg.status);
return;
}
}
/* If the gattc_if equal to profile A, call profile A cb handler,
* so here call each profile's callback */
do {
int idx;
for (idx = 0; idx < PROFILE_NUM; idx++) {
if (gattc_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */
gattc_if == gl_profile_tab[idx].gattc_if) {
if (gl_profile_tab[idx].gattc_cb) {
gl_profile_tab[idx].gattc_cb(event, gattc_if, param);
}
}
}
} while (0);
}
void app_tpms_main(void)
{
// Initialize NVS.
esp_err_t ret;
//register the callback function to the gap module
ret = esp_ble_gap_register_callback(esp_gap_cb);
if (ret){
ESP_LOGE(GATTC_TAG, "%s gap register failed, error code = %x\n", __func__, ret);
return;
}
//register the callback function to the gattc module
ret = esp_ble_gattc_register_callback(esp_gattc_cb);
if(ret){
ESP_LOGE(GATTC_TAG, "%s gattc register failed, error code = %x\n", __func__, ret);
return;
}
ret = esp_ble_gattc_app_register(PROFILE_A_APP_ID);
if (ret){
ESP_LOGE(GATTC_TAG, "%s gattc app register failed, error code = %x\n", __func__, ret);
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret){
ESP_LOGE(GATTC_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
}
......@@ -155,7 +155,8 @@ void app_main(void)
Ble_User_Init();
Get_Mac_Init();
bsp_BT_Init();
bsp_Ble_Init();
// bsp_Ble_Init();
app_tpms_main();
bsp_Uart_Init();
printf("KL30 INIT OVER~~~~~~\r\n");
......@@ -167,8 +168,8 @@ void app_main(void)
void delele_tasks(void)
{
vTaskSuspend(Sys_Run_Taskshdl);
vTaskSuspend(Ble_User_Task_hdl);
bt_app_task_shut_down( );
vTaskDelay(pdMS_TO_TICKS(100));
// vTaskSuspend(Sys_Run_Taskshdl);
// vTaskSuspend(Ble_User_Task_hdl);
// bt_app_task_shut_down( );
// vTaskDelay(pdMS_TO_TICKS(100));
}
......@@ -308,8 +308,8 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
# CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set
# CONFIG_PARTITION_TABLE_TWO_OTA is not set
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="QJ500_7C_partitions_two_ota.csv"
CONFIG_PARTITION_TABLE_FILENAME="QJ500_7C_partitions_two_ota.csv"
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="RT200T-2_partitions_two_ota.csv"
CONFIG_PARTITION_TABLE_FILENAME="RT200T-2_partitions_two_ota.csv"
CONFIG_PARTITION_TABLE_OFFSET=0xA000
CONFIG_PARTITION_TABLE_MD5=y
# end of Partition Table
......@@ -317,7 +317,7 @@ CONFIG_PARTITION_TABLE_MD5=y
#
# Example Configuration
#
CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL="http://192.168.137.1:3200/Qj500-7C.bin"
CONFIG_EXAMPLE_FIRMWARE_UPGRADE_URL="http://192.168.137.1:3200/RT200T-2.bin"
# CONFIG_EXAMPLE_USE_CERT_BUNDLE is not set
CONFIG_EXAMPLE_SKIP_COMMON_NAME_CHECK=y
# CONFIG_EXAMPLE_FIRMWARE_UPGRADE_BIND_IF is not set
......@@ -1355,7 +1355,7 @@ CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y
#
# LWIP
#
CONFIG_LWIP_LOCAL_HOSTNAME="QJ500-7C"
CONFIG_LWIP_LOCAL_HOSTNAME="RT200T-2"
# CONFIG_LWIP_NETIF_API is not set
CONFIG_LWIP_TCPIP_TASK_PRIO=18
# CONFIG_LWIP_TCPIP_CORE_LOCKING is not set
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment