修复蓝牙接收数据固定字节的问题
authord15521469561 <1731678236@qq.com>
Tue, 7 Apr 2026 07:36:17 +0000 (15:36 +0800)
committerd15521469561 <1731678236@qq.com>
Tue, 7 Apr 2026 07:36:17 +0000 (15:36 +0800)
main/app_main.c
main/ble.c
main/gap.c
main/gatt_svc.c
main/http.c
main/project_common.c
main/project_common.h
main/user_flash.c

index cd5fcd4..e663ead 100644 (file)
@@ -24,7 +24,6 @@ static void onesec_timer_callback(void* arg)
 
 
 #if (IOT_ENABLE)
-#if (1)
 const char* device_name = "PXN-W GTL";
 const char* http_url = "http://192.168.66.85:8080";//"http://107.23.202.137:8080";//"http://192.168.66.80:8080";//http://107.23.202.137:8080
 const char* http_token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImJhZWRiZjUyLTUzMDgtNDRlOS04MWI3LTE1MmU4NGYwYzk5ZCJ9.fyxIGmmYD-pKd7VQ02DrL6FwBKVkqXoIu4iKSVMOUj8WubDvuSdbX6j8YzQw8K_t6UTRVZsRDTlO5me2KKhs7A";//"eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjRhNjBlZDdiLTExMDUtNDU5Ny1iNDBhLTg5ZDRiMzMzNTIyYSJ9.CUxkgU2F6BoxzE3HHVdXAin2OEDD1-D_iI0zh7VWTnlNsjsHHulvZVMq2hQLXF4cAQIJpLWwiMZJQO6Bvy9vwg";
@@ -34,7 +33,7 @@ const char* wifi_pwd = "XiaoMing1919810";
 
 const uint16_t mcu_vid = 0x36E6;
 const uint16_t mcu_pid = 0xA00B;
-#endif
+
 #endif
 
 void Main_Loop_Task(void *pvParameters)
@@ -56,10 +55,17 @@ void Main_Loop_Task(void *pvParameters)
     ESP_LOGI(MAIN_TAG, "1 second periodic timer started");
 
 #if (IOT_ENABLE)
-    if(common_storage.net_info.wifi_info_flag == 1 && common_storage.net_info.serial_number_flag == 1 && common_storage.net_info.device_id_flag == 1 && common_storage.ssl_cert[0].length != 0 && common_storage.ssl_cert[1].length != 0 && common_storage.ssl_cert[2].length != 0)
+    if(common_storage.net_info.wifi_info_flag == 1 && common_storage.net_info.serial_number_flag == 1 && common_storage.ssl_cert[0].length != 0 && common_storage.ssl_cert[1].length != 0 && common_storage.ssl_cert[2].length != 0)
     {
         Wifi_Queue_Send(0, 0x03, 0);
     }
+    common_setting.mcu_info.vid = mcu_vid;
+    common_setting.mcu_info.pid = mcu_pid;
+    common_setting.mcu_info.fw_version = 0x0001;
+    common_setting.mcu_info.build_version = 0x0001;
+    memcpy(common_setting.mcu_info.product_name, device_name, strlen(device_name));
+    common_setting.mcu_info.product_name_len = strlen(device_name);
+    common_setting.mcu_info_got_flag = 1;
 #if (USER_TEST_ENABLE)
     // BLE Test
     common_setting.mcu_info.vid = mcu_vid;
@@ -69,8 +75,9 @@ void Main_Loop_Task(void *pvParameters)
     memcpy(common_setting.mcu_info.product_name, device_name, strlen(device_name));
     common_setting.mcu_info.product_name_len = strlen(device_name);
     common_setting.mcu_info_got_flag = 1;
+    common_setting.net_pairing_info.serial_number_flag =1;//等同于device_id
     // Ble_Queue_Send(0xFF, 0x01);
-    xEventGroupSetBits(ble_event_group, BLE_ON_BIT);
+    // xEventGroupSetBits(ble_event_group, BLE_ON_BIT);
 
     memset(common_setting.net_pairing_info.http_url, 0x00, sizeof(common_setting.net_pairing_info.http_url));
     memset(common_setting.net_pairing_info.http_token, 0x00, sizeof(common_setting.net_pairing_info.http_token));
@@ -89,7 +96,7 @@ void Main_Loop_Task(void *pvParameters)
     memcpy(common_setting.wifi_status.cur_pwd, wifi_pwd, strlen(wifi_pwd));
     common_setting.wifi_status.cur_ssid_len = strlen(wifi_ssid);
     common_setting.wifi_status.cur_pwd_len = strlen(wifi_pwd);
-    Wifi_Queue_Send(0, 0x02, 0);
+    // Wifi_Queue_Send(0, 0x02, 0);
 #endif
 #endif
     for(;;)
index 0e73fca..b320fd8 100644 (file)
@@ -122,13 +122,13 @@ void Ble_Enable_Status_Check(void)
 {
     if(common_setting.mcu_info_got_flag)
     {
-        if(common_storage.net_info.device_id_flag == 0 && common_storage.net_info.wifi_info_flag == 0 && common_setting.ble_status.ble_state == 0)
+        if(common_storage.net_info.serial_number_flag == 0 && common_storage.net_info.wifi_info_flag == 0 && common_setting.ble_status.ble_state == 0)
         {
             // Ble_Queue_Send(0xFF, 0x01);
             xEventGroupSetBits(ble_event_group, BLE_ON_BIT);
             ESP_LOGI(BLE_TAG, "[APP] Free memory device_id_flag1 on: %" PRIu32 " bytes", esp_get_free_heap_size());
         }
-        else if(common_storage.net_info.device_id_flag && common_storage.net_info.wifi_info_flag && common_setting.ble_status.ble_state != 0)
+        else if(common_storage.net_info.serial_number_flag && common_storage.net_info.wifi_info_flag && common_setting.ble_status.ble_state != 0)
         {
             // Ble_Queue_Send(0xFF, 0xFF);
             xEventGroupSetBits(ble_event_group, BLE_OFF_BIT);
index fd70370..7ba57a0 100644 (file)
@@ -21,8 +21,8 @@ void stop_advertising(void);
 /* Private variables */
 static uint8_t own_addr_type = 0;
 static uint8_t addr_val[6] = {0};
-uint8_t adv_vendor_data[29] = {0xFF, 0xFF, 0xE6, 0x36, 0x0B, 0xA0, 'P', 'X', 'N', '-', 'W', ' ', 'G', 'T', 'L'};
-uint8_t adv_vendor_len = 15;
+uint8_t adv_vendor_data[31] = {0};
+uint8_t adv_vendor_len = 0;
 
 /* Private functions */
 inline static void format_addr(char *addr_str, uint8_t addr[]) {
@@ -64,16 +64,26 @@ void start_advertising(void)
     struct ble_hs_adv_fields adv_fields = {0};
     struct ble_hs_adv_fields rsp_fields = {0};
     struct ble_gap_adv_params adv_params = {0};
-
+    memset(&adv_fields, 0, sizeof(adv_fields));
+    memset(&rsp_fields, 0, sizeof(rsp_fields));
+    memset(&adv_params, 0, sizeof(adv_params));
     /* Set advertising flags */
     adv_fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
-
+    adv_vendor_len=0;
     *(uint16_t*)&adv_vendor_data[0] = 0xFFFF; // BLE默认厂商字段前2字节为蓝牙联盟成员厂商ID,因此固定2个字节0xFFFF
+    adv_vendor_len+=2;
     *(uint16_t*)&adv_vendor_data[2] = common_setting.mcu_info.vid;
+    adv_vendor_len+=2;
     *(uint16_t*)&adv_vendor_data[4] = common_setting.mcu_info.pid;
+    adv_vendor_len=adv_vendor_len+common_setting.mcu_info.product_name_len;
     memcpy(&adv_vendor_data[6], common_setting.mcu_info.product_name, common_setting.mcu_info.product_name_len);
 
     adv_fields.mfg_data = adv_vendor_data;
+    ESP_LOGE(GAP_TAG, "advertising data,len: %d", adv_vendor_len);
+    if(adv_vendor_len > 31)
+    {
+        adv_vendor_len=31;
+    }
     adv_fields.mfg_data_len = adv_vendor_len;
 
     /* Set device name */
index 48267bf..e0dfb56 100644 (file)
@@ -107,15 +107,23 @@ static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
                 printf("%02x ", ctxt->om->om_data[i]);
             }
             printf("\n");
-
-            memset(&ble_notify,0x00,sizeof(BLE_MESSAGE_T));
-            if(ctxt->om->om_len <= sizeof(ble_notify.data))
+            static BLE_MESSAGE_T ble_message_temp = {0};
+            memset(&ble_message_temp,0x00,sizeof(BLE_MESSAGE_T));
+            if(ctxt->om->om_len <= sizeof(ble_message_temp.data))
             {
-                ble_notify.length = ctxt->om->om_len;
-                memcpy(ble_notify.data, ctxt->om->om_data, ctxt->om->om_len);
+                ble_message_temp.length = ctxt->om->om_len;
+                memcpy(ble_message_temp.data, ctxt->om->om_data, ctxt->om->om_len);
                 // xQueueSend(ble_notify_queue_handle, &ble_message_temp, 0); // 回传测试
-                xQueueSend(ble_write_queue_handle, &ble_notify, 0);
+                xQueueSend(ble_write_queue_handle, &ble_message_temp, 0);
             }
+            // memset(&ble_notify,0x00,sizeof(BLE_MESSAGE_T));
+            // if(ctxt->om->om_len <= sizeof(ble_notify.data))
+            // {
+            //     ble_notify.length = ctxt->om->om_len;
+            //     memcpy(ble_notify.data, ctxt->om->om_data, ctxt->om->om_len);
+            //     // xQueueSend(ble_notify_queue_handle, &ble_message_temp, 0); // 回传测试
+            //     xQueueSend(ble_write_queue_handle, &ble_notify, 0);
+            // }
             return rc;
         }
         goto unknown;
index 553018d..6d95ee2 100644 (file)
@@ -410,7 +410,7 @@ void http_get_url_task(void *pvParameters)
                                         common_setting.net_pairing_info.serial_number[serial_number_len] = '\0';
                                         common_setting.net_pairing_info.serial_number_length = serial_number_len;
                                         common_setting.net_pairing_info.serial_number_flag = 1;
-
+                                        
                                         //真正需要存储的flash数据
                                         memset(common_storage.net_info.serial_number, 0x00, SERIAL_NUMBER_MAX_LEN+1);
                                         memccpy(common_storage.net_info.serial_number, str_serialNumber, 0, serial_number_len);
index 536b763..aa7cbed 100644 (file)
@@ -1106,6 +1106,7 @@ void Ble_Pairing_Handle(uint8_t *data ,uint16_t len, uint8_t host_type)
                     memset(common_storage.net_info.wifi_pwd,0,64);
                     memcpy(common_storage.net_info.wifi_pwd, wifi_pwd, wifi_pwd_len);
                     common_storage.net_info.wifi_pwd_len=wifi_pwd_len;
+                    common_storage.net_info.wifi_info_flag=1;
 
 
                     ESP_LOG_BUFFER_CHAR(COMMON_TAG, common_setting.wifi_status.cur_ssid, wifi_ssid_len);
index fb3aead..9476d8c 100644 (file)
@@ -59,7 +59,7 @@
 #define FW_HEADER_CODE_SIZE                    12
 #define FW_HEADER_CODE                         "WGTL_ESP32C2"
 #define IOT_ENABLE                   1
-#define USER_TEST_ENABLE             1
+#define USER_TEST_ENABLE             0
 #define BLE_TEST_WIFI_CONNECT_ENABLE 1//BLE下发wifixin不需要做htttp token user_id校验
 #define IS_CLOSE_BLE                 0  //是否关闭蓝牙广播暂时测试不关闭 一直广播
 
index 0f5a7e9..5cb6ef9 100644 (file)
@@ -21,6 +21,7 @@ void User_Flash_Check(void)
                printf("%s read common_storage failed\n",__func__);
                common_storage.net_info.wifi_info_flag = 0;
                common_storage.net_info.device_id_flag = 0;
+               common_storage.net_info.serial_number_flag=0;
                
        }else{
                printf("%s read serialNumber %s\n",__func__,common_storage.net_info.serial_number);