1、注释了BLE JSON Service 相关实现代码,因为当前蓝牙通讯仍然使用二进制方式进行通讯,只是替换了之前官方的BluFi方式

This commit is contained in:
Rdzleo 2026-02-12 16:53:30 +08:00
parent 60a2c7b068
commit 7431a630e4
6 changed files with 53 additions and 93 deletions

2
dzbj

@ -1 +1 @@
Subproject commit 99aef2c4e50894af43b8b93483f8d788f572b3c4 Subproject commit 58fb9aab86c044b5a951fd7044fd335d0bae906f

View File

@ -19,7 +19,7 @@ set(SOURCES "audio_codecs/audio_codec.cc"
"settings.cc" "settings.cc"
"background_task.cc" "background_task.cc"
"bluetooth_provisioning.cc" # "bluetooth_provisioning.cc" #
"ble_service.cc" # BLE JSON #"ble_service.cc" # BLE JSON 使
"weather_api.cc" "weather_api.cc"
"main.cc" "main.cc"
) )

View File

@ -1,5 +1,5 @@
#include "application.h" #include "application.h"
#include "ble_service_config.h" // #include "ble_service_config.h" // BLE JSON Service 暂不使用
#include "board.h" #include "board.h"
#include "wifi_board.h" #include "wifi_board.h"
#include "display.h" #include "display.h"
@ -3054,45 +3054,39 @@ const char* Application::DeviceStateToString(DeviceState state) {
return "unknown"; return "unknown";
} }
// BLE JSON Service 命令处理(暂不使用,保留代码)
#if 0
void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON* data, BleJsonService& service) { void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON* data, BleJsonService& service) {
auto& board = Board::GetInstance(); auto& board = Board::GetInstance();
// ---- ping ----
if (cmd == "ping") { if (cmd == "ping") {
service.SendResponse(cmd, msg_id, 0, "pong"); service.SendResponse(cmd, msg_id, 0, "pong");
return; return;
} }
// ---- status: 返回设备运行状态 ----
if (cmd == "status") { if (cmd == "status") {
cJSON* resp = cJSON_CreateObject(); cJSON* resp = cJSON_CreateObject();
cJSON_AddStringToObject(resp, "s", DeviceStateToString(device_state_)); cJSON_AddStringToObject(resp, "s", DeviceStateToString(device_state_));
int battery_level = 0; int battery_level = 0;
bool charging = false, discharging = false; bool charging = false, discharging = false;
if (board.GetBatteryLevel(battery_level, charging, discharging)) { if (board.GetBatteryLevel(battery_level, charging, discharging)) {
cJSON_AddNumberToObject(resp, "bat", battery_level); cJSON_AddNumberToObject(resp, "bat", battery_level);
cJSON_AddBoolToObject(resp, "chg", charging); cJSON_AddBoolToObject(resp, "chg", charging);
} }
auto* codec = board.GetAudioCodec(); auto* codec = board.GetAudioCodec();
if (codec) { if (codec) {
cJSON_AddNumberToObject(resp, "vol", codec->output_volume()); cJSON_AddNumberToObject(resp, "vol", codec->output_volume());
} }
// 当前 WiFi 信息
wifi_ap_record_t ap{}; wifi_ap_record_t ap{};
if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK) { if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK) {
cJSON_AddStringToObject(resp, "ssid", reinterpret_cast<const char*>(ap.ssid)); cJSON_AddStringToObject(resp, "ssid", reinterpret_cast<const char*>(ap.ssid));
cJSON_AddNumberToObject(resp, "rssi", ap.rssi); cJSON_AddNumberToObject(resp, "rssi", ap.rssi);
} }
service.SendResponse(cmd, msg_id, 0, "ok", resp); service.SendResponse(cmd, msg_id, 0, "ok", resp);
cJSON_Delete(resp); cJSON_Delete(resp);
return; return;
} }
// ---- dev_info: 返回设备信息 ----
if (cmd == "dev_info") { if (cmd == "dev_info") {
cJSON* resp = cJSON_CreateObject(); cJSON* resp = cJSON_CreateObject();
cJSON_AddStringToObject(resp, "mac", SystemInfo::GetMacAddress().c_str()); cJSON_AddStringToObject(resp, "mac", SystemInfo::GetMacAddress().c_str());
@ -3101,13 +3095,11 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
cJSON_AddStringToObject(resp, "fw", app_desc->version); cJSON_AddStringToObject(resp, "fw", app_desc->version);
cJSON_AddStringToObject(resp, "chip", SystemInfo::GetChipModelName().c_str()); cJSON_AddStringToObject(resp, "chip", SystemInfo::GetChipModelName().c_str());
cJSON_AddStringToObject(resp, "idf", app_desc->idf_ver); cJSON_AddStringToObject(resp, "idf", app_desc->idf_ver);
service.SendResponse(cmd, msg_id, 0, "ok", resp); service.SendResponse(cmd, msg_id, 0, "ok", resp);
cJSON_Delete(resp); cJSON_Delete(resp);
return; return;
} }
// ---- set_wifi: 设置 WiFi 凭证 ----
if (cmd == "set_wifi") { if (cmd == "set_wifi") {
cJSON* ssid_item = cJSON_GetObjectItem(data, "ssid"); cJSON* ssid_item = cJSON_GetObjectItem(data, "ssid");
cJSON* pwd_item = cJSON_GetObjectItem(data, "pwd"); cJSON* pwd_item = cJSON_GetObjectItem(data, "pwd");
@ -3115,7 +3107,6 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
service.SendResponse(cmd, msg_id, -1, "missing ssid"); service.SendResponse(cmd, msg_id, -1, "missing ssid");
return; return;
} }
wifi_config_t wifi_config = {}; wifi_config_t wifi_config = {};
strncpy(reinterpret_cast<char*>(wifi_config.sta.ssid), strncpy(reinterpret_cast<char*>(wifi_config.sta.ssid),
ssid_item->valuestring, sizeof(wifi_config.sta.ssid) - 1); ssid_item->valuestring, sizeof(wifi_config.sta.ssid) - 1);
@ -3123,14 +3114,11 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
strncpy(reinterpret_cast<char*>(wifi_config.sta.password), strncpy(reinterpret_cast<char*>(wifi_config.sta.password),
pwd_item->valuestring, sizeof(wifi_config.sta.password) - 1); pwd_item->valuestring, sizeof(wifi_config.sta.password) - 1);
} }
esp_err_t ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_err_t ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
if (ret != ESP_OK) { if (ret != ESP_OK) {
service.SendResponse(cmd, msg_id, -2, "set config failed"); service.SendResponse(cmd, msg_id, -2, "set config failed");
return; return;
} }
// 断开当前连接并重新连接
esp_wifi_disconnect(); esp_wifi_disconnect();
ret = esp_wifi_connect(); ret = esp_wifi_connect();
service.SendResponse(cmd, msg_id, 0, service.SendResponse(cmd, msg_id, 0,
@ -3138,26 +3126,22 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
return; return;
} }
// ---- wifi_list: 扫描 WiFi 列表 ----
if (cmd == "wifi_list") { if (cmd == "wifi_list") {
wifi_scan_config_t scan_config = {}; wifi_scan_config_t scan_config = {};
scan_config.show_hidden = false; scan_config.show_hidden = false;
esp_err_t ret = esp_wifi_scan_start(&scan_config, true); // 阻塞扫描 esp_err_t ret = esp_wifi_scan_start(&scan_config, true);
if (ret != ESP_OK) { if (ret != ESP_OK) {
service.SendResponse(cmd, msg_id, -1, "scan failed"); service.SendResponse(cmd, msg_id, -1, "scan failed");
return; return;
} }
uint16_t ap_count = 0; uint16_t ap_count = 0;
esp_wifi_scan_get_ap_num(&ap_count); esp_wifi_scan_get_ap_num(&ap_count);
if (ap_count > BLE_JSON_WIFI_LIST_MAX) { if (ap_count > BLE_JSON_WIFI_LIST_MAX) {
ap_count = BLE_JSON_WIFI_LIST_MAX; ap_count = BLE_JSON_WIFI_LIST_MAX;
} }
wifi_ap_record_t* ap_list = nullptr; wifi_ap_record_t* ap_list = nullptr;
cJSON* resp = cJSON_CreateObject(); cJSON* resp = cJSON_CreateObject();
cJSON* arr = cJSON_AddArrayToObject(resp, "list"); cJSON* arr = cJSON_AddArrayToObject(resp, "list");
if (ap_count > 0) { if (ap_count > 0) {
ap_list = static_cast<wifi_ap_record_t*>(malloc(sizeof(wifi_ap_record_t) * ap_count)); ap_list = static_cast<wifi_ap_record_t*>(malloc(sizeof(wifi_ap_record_t) * ap_count));
if (ap_list && esp_wifi_scan_get_ap_records(&ap_count, ap_list) == ESP_OK) { if (ap_list && esp_wifi_scan_get_ap_records(&ap_count, ap_list) == ESP_OK) {
@ -3172,13 +3156,11 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
} }
free(ap_list); free(ap_list);
} }
service.SendResponse(cmd, msg_id, 0, "ok", resp); service.SendResponse(cmd, msg_id, 0, "ok", resp);
cJSON_Delete(resp); cJSON_Delete(resp);
return; return;
} }
// ---- set_vol: 设置音量 ----
if (cmd == "set_vol") { if (cmd == "set_vol") {
cJSON* vol_item = cJSON_GetObjectItem(data, "vol"); cJSON* vol_item = cJSON_GetObjectItem(data, "vol");
if (!vol_item || !cJSON_IsNumber(vol_item)) { if (!vol_item || !cJSON_IsNumber(vol_item)) {
@ -3188,28 +3170,23 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
int vol = vol_item->valueint; int vol = vol_item->valueint;
if (vol < 0) vol = 0; if (vol < 0) vol = 0;
if (vol > 100) vol = 100; if (vol > 100) vol = 100;
auto* codec = board.GetAudioCodec(); auto* codec = board.GetAudioCodec();
if (codec) { if (codec) {
codec->SetOutputVolume(vol); codec->SetOutputVolume(vol);
// 同时持久化到 NVS
Settings s("audio", true); Settings s("audio", true);
s.SetInt("output_volume", vol); s.SetInt("output_volume", vol);
} }
service.SendResponse(cmd, msg_id, 0, "ok"); service.SendResponse(cmd, msg_id, 0, "ok");
return; return;
} }
// ---- reboot: 重启设备 ----
if (cmd == "reboot") { if (cmd == "reboot") {
service.SendResponse(cmd, msg_id, 0, "rebooting"); service.SendResponse(cmd, msg_id, 0, "rebooting");
vTaskDelay(pdMS_TO_TICKS(500)); // 等待响应发出 vTaskDelay(pdMS_TO_TICKS(500));
Reboot(); Reboot();
return; return;
} }
// ---- ota: 触发 OTA 升级 ----
if (cmd == "ota") { if (cmd == "ota") {
if (device_state_ == kDeviceStateUpgrading) { if (device_state_ == kDeviceStateUpgrading) {
service.SendResponse(cmd, msg_id, -1, "already upgrading"); service.SendResponse(cmd, msg_id, -1, "already upgrading");
@ -3222,7 +3199,6 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
return; return;
} }
// ---- iot: 转发 IoT 命令 ----
if (cmd == "iot") { if (cmd == "iot") {
auto& thing_manager = iot::ThingManager::GetInstance(); auto& thing_manager = iot::ThingManager::GetInstance();
std::string states; std::string states;
@ -3236,6 +3212,6 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
return; return;
} }
// ---- 未知命令 ----
service.SendResponse(cmd, msg_id, -99, "unknown cmd"); service.SendResponse(cmd, msg_id, -99, "unknown cmd");
} }
#endif

View File

@ -20,7 +20,7 @@
#include "ota.h" #include "ota.h"
#include "background_task.h" #include "background_task.h"
#include "audio/simple_pipeline.h" #include "audio/simple_pipeline.h"
#include "ble_service.h" // #include "ble_service.h" // BLE JSON Service 暂不使用
#if CONFIG_USE_WAKE_WORD_DETECT #if CONFIG_USE_WAKE_WORD_DETECT
#include "wake_word_detect.h" #include "wake_word_detect.h"
@ -109,8 +109,8 @@ public:
bool IsDialogUploadEnabled() const { return dialog_upload_enabled_; }// 是否启用对话上传 bool IsDialogUploadEnabled() const { return dialog_upload_enabled_; }// 是否启用对话上传
void SetDialogUploadEnabled(bool enabled);// 设置对话上传状态 void SetDialogUploadEnabled(bool enabled);// 设置对话上传状态
// BLE JSON 命令处理 (由 WifiBoard 中的 BleJsonService 回调) // // BLE JSON 命令处理(暂不使用)
void HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON* data, BleJsonService& service); // void HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON* data, BleJsonService& service);
private: private:
Application();// 构造函数 Application();// 构造函数

View File

@ -410,60 +410,44 @@ bool WifiBoard::StartBleProvisioning() {
return true;// 启动成功返回true return true;// 启动成功返回true
} }
// 使用 BLE JSON Service 进行配网 (替代 BLE) // // BLE JSON Service 配网(暂不使用,保留代码)
bool WifiBoard::StartBleJsonProvisioning() { // bool WifiBoard::StartBleJsonProvisioning() {
ESP_LOGI(TAG, "🔵 正在启动BLE JSON配网服务..."); // ESP_LOGI(TAG, "🔵 正在启动BLE JSON配网服务...");
// Application::GetInstance().StopAudioProcessor();
Application::GetInstance().StopAudioProcessor(); // Application::GetInstance().ClearAudioQueue();
Application::GetInstance().ClearAudioQueue(); // if (!ble_json_service_.Initialize()) {
// ESP_LOGE(TAG, "❌ BLE JSON服务初始化失败");
// 初始化 BLE JSON 服务 // return false;
if (!ble_json_service_.Initialize()) { // }
ESP_LOGE(TAG, "❌ BLE JSON服务初始化失败"); // ble_json_service_.SetCommandCallback(
return false; // [this](const std::string& cmd, int msg_id, cJSON* data) {
} // Application::GetInstance().HandleBleJsonCommand(cmd, msg_id, data, ble_json_service_);
// });
// 设置命令回调,转发给 Application 处理 // if (!ble_json_service_.Start("Airhub_Ble")) {
ble_json_service_.SetCommandCallback( // ESP_LOGE(TAG, "❌ BLE JSON服务启动失败");
[this](const std::string& cmd, int msg_id, cJSON* data) { // return false;
Application::GetInstance().HandleBleJsonCommand(cmd, msg_id, data, ble_json_service_); // }
}); // ESP_LOGI(TAG, "✅ BLE JSON配网启动成功");
// ble_provisioning_active_ = true;
// 启动 BLE JSON 服务 // ble_start_time_ = xTaskGetTickCount();
if (!ble_json_service_.Start("Airhub_Ble")) { // auto display = GetDisplay();
ESP_LOGE(TAG, "❌ BLE JSON服务启动失败"); // if (display) {
return false; // display->ShowNotification("BLE配网模式", 30000);
} // }
// auto& application = Application::GetInstance();
ESP_LOGI(TAG, "✅ BLE JSON配网启动成功"); // if (strcmp(CONFIG_DEVICE_ROLE, "KAKA") == 0) {
// application.Alert("BLE配网模式", "请使用手机APP搜索Airhub_开头的蓝牙设备", "", Lang::Sounds::P3_KAKA_WIFICONFIG);
ble_provisioning_active_ = true; // } else if (strcmp(CONFIG_DEVICE_ROLE, "RTC_Test") == 0) {
ble_start_time_ = xTaskGetTickCount(); // application.Alert("BLE配网模式", "请使用手机APP搜索Airhub_开头的蓝牙设备", "", Lang::Sounds::P3_LALA_WIFICONFIG);
// }
// 显示配网通知 // while (true) {
auto display = GetDisplay(); // int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
if (display) { // int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
display->ShowNotification("BLE配网模式", 30000); // ESP_LOGI(TAG, "BLE配网等待中... Free internal: %u minimal internal: %u", free_sram, min_free_sram);
} // vTaskDelay(pdMS_TO_TICKS(10000));
// }
// 播放配网提示音 // return true;
auto& application = Application::GetInstance(); // }
if (strcmp(CONFIG_DEVICE_ROLE, "KAKA") == 0) {
application.Alert("BLE配网模式", "请使用手机APP搜索Airhub_开头的蓝牙设备", "", Lang::Sounds::P3_KAKA_WIFICONFIG);
} else if (strcmp(CONFIG_DEVICE_ROLE, "RTC_Test") == 0) {
application.Alert("BLE配网模式", "请使用手机APP搜索Airhub_开头的蓝牙设备", "", Lang::Sounds::P3_LALA_WIFICONFIG);
}
// 配网状态等待循环
while (true) {
int free_sram = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
int min_free_sram = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
ESP_LOGI(TAG, "BLE配网等待中... Free internal: %u minimal internal: %u", free_sram, min_free_sram);
vTaskDelay(pdMS_TO_TICKS(10000));
}
return true;
}
// 监控BLE配网状态 // 监控BLE配网状态
void WifiBoard::MonitorBleProvisioning() { void WifiBoard::MonitorBleProvisioning() {

View File

@ -12,7 +12,7 @@
#include "board.h" #include "board.h"
#include "bluetooth_provisioning.h" #include "bluetooth_provisioning.h"
#include "ble_service.h" // #include "ble_service.h" // BLE JSON Service 暂不使用
#include <string> #include <string>
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
@ -35,7 +35,7 @@ protected:
TickType_t ble_start_time_ = 0; ///< BLE配网开始时间戳 TickType_t ble_start_time_ = 0; ///< BLE配网开始时间戳
static const TickType_t BLE_PROV_TIMEOUT_MS = 300000; ///< BLE配网超时时间5分钟 static const TickType_t BLE_PROV_TIMEOUT_MS = 300000; ///< BLE配网超时时间5分钟
BluetoothProvisioning bluetooth_provisioning_; ///< BLE蓝牙配网实例对象 BluetoothProvisioning bluetooth_provisioning_; ///< BLE蓝牙配网实例对象
BleJsonService ble_json_service_; ///< BLE JSON 配网服务实例 // BleJsonService ble_json_service_; ///< BLE JSON 配网服务实例(暂不使用)
/** /**
* @brief * @brief
@ -65,8 +65,8 @@ protected:
*/ */
bool StartBleProvisioning(); bool StartBleProvisioning();
// 使用 BLE JSON Service 进行配网 // // 使用 BLE JSON Service 进行配网(暂不使用)
bool StartBleJsonProvisioning(); // bool StartBleJsonProvisioning();
/** /**
* @brief BLE配网进程 * @brief BLE配网进程