1、注释了BLE JSON Service 相关实现代码,因为当前蓝牙通讯仍然使用二进制方式进行通讯,只是替换了之前官方的BluFi方式
This commit is contained in:
parent
60a2c7b068
commit
7431a630e4
2
dzbj
2
dzbj
@ -1 +1 @@
|
||||
Subproject commit 99aef2c4e50894af43b8b93483f8d788f572b3c4
|
||||
Subproject commit 58fb9aab86c044b5a951fd7044fd335d0bae906f
|
||||
@ -19,7 +19,7 @@ set(SOURCES "audio_codecs/audio_codec.cc"
|
||||
"settings.cc"
|
||||
"background_task.cc"
|
||||
"bluetooth_provisioning.cc" # 蓝牙配网实现
|
||||
"ble_service.cc" # BLE JSON 通讯服务
|
||||
#"ble_service.cc" # BLE JSON 通讯服务(暂不使用,保留代码)
|
||||
"weather_api.cc"
|
||||
"main.cc"
|
||||
)
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "application.h"
|
||||
#include "ble_service_config.h"
|
||||
// #include "ble_service_config.h" // BLE JSON Service 暂不使用
|
||||
#include "board.h"
|
||||
#include "wifi_board.h"
|
||||
#include "display.h"
|
||||
@ -3054,45 +3054,39 @@ const char* Application::DeviceStateToString(DeviceState state) {
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
// BLE JSON Service 命令处理(暂不使用,保留代码)
|
||||
#if 0
|
||||
void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON* data, BleJsonService& service) {
|
||||
auto& board = Board::GetInstance();
|
||||
|
||||
// ---- ping ----
|
||||
if (cmd == "ping") {
|
||||
service.SendResponse(cmd, msg_id, 0, "pong");
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- status: 返回设备运行状态 ----
|
||||
if (cmd == "status") {
|
||||
cJSON* resp = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(resp, "s", DeviceStateToString(device_state_));
|
||||
|
||||
int battery_level = 0;
|
||||
bool charging = false, discharging = false;
|
||||
if (board.GetBatteryLevel(battery_level, charging, discharging)) {
|
||||
cJSON_AddNumberToObject(resp, "bat", battery_level);
|
||||
cJSON_AddBoolToObject(resp, "chg", charging);
|
||||
}
|
||||
|
||||
auto* codec = board.GetAudioCodec();
|
||||
if (codec) {
|
||||
cJSON_AddNumberToObject(resp, "vol", codec->output_volume());
|
||||
}
|
||||
|
||||
// 当前 WiFi 信息
|
||||
wifi_ap_record_t ap{};
|
||||
if (esp_wifi_sta_get_ap_info(&ap) == ESP_OK) {
|
||||
cJSON_AddStringToObject(resp, "ssid", reinterpret_cast<const char*>(ap.ssid));
|
||||
cJSON_AddNumberToObject(resp, "rssi", ap.rssi);
|
||||
}
|
||||
|
||||
service.SendResponse(cmd, msg_id, 0, "ok", resp);
|
||||
cJSON_Delete(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- dev_info: 返回设备信息 ----
|
||||
if (cmd == "dev_info") {
|
||||
cJSON* resp = cJSON_CreateObject();
|
||||
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, "chip", SystemInfo::GetChipModelName().c_str());
|
||||
cJSON_AddStringToObject(resp, "idf", app_desc->idf_ver);
|
||||
|
||||
service.SendResponse(cmd, msg_id, 0, "ok", resp);
|
||||
cJSON_Delete(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- set_wifi: 设置 WiFi 凭证 ----
|
||||
if (cmd == "set_wifi") {
|
||||
cJSON* ssid_item = cJSON_GetObjectItem(data, "ssid");
|
||||
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");
|
||||
return;
|
||||
}
|
||||
|
||||
wifi_config_t wifi_config = {};
|
||||
strncpy(reinterpret_cast<char*>(wifi_config.sta.ssid),
|
||||
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),
|
||||
pwd_item->valuestring, sizeof(wifi_config.sta.password) - 1);
|
||||
}
|
||||
|
||||
esp_err_t ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
|
||||
if (ret != ESP_OK) {
|
||||
service.SendResponse(cmd, msg_id, -2, "set config failed");
|
||||
return;
|
||||
}
|
||||
|
||||
// 断开当前连接并重新连接
|
||||
esp_wifi_disconnect();
|
||||
ret = esp_wifi_connect();
|
||||
service.SendResponse(cmd, msg_id, 0,
|
||||
@ -3138,26 +3126,22 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- wifi_list: 扫描 WiFi 列表 ----
|
||||
if (cmd == "wifi_list") {
|
||||
wifi_scan_config_t scan_config = {};
|
||||
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) {
|
||||
service.SendResponse(cmd, msg_id, -1, "scan failed");
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t ap_count = 0;
|
||||
esp_wifi_scan_get_ap_num(&ap_count);
|
||||
if (ap_count > BLE_JSON_WIFI_LIST_MAX) {
|
||||
ap_count = BLE_JSON_WIFI_LIST_MAX;
|
||||
}
|
||||
|
||||
wifi_ap_record_t* ap_list = nullptr;
|
||||
cJSON* resp = cJSON_CreateObject();
|
||||
cJSON* arr = cJSON_AddArrayToObject(resp, "list");
|
||||
|
||||
if (ap_count > 0) {
|
||||
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) {
|
||||
@ -3172,13 +3156,11 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
|
||||
}
|
||||
free(ap_list);
|
||||
}
|
||||
|
||||
service.SendResponse(cmd, msg_id, 0, "ok", resp);
|
||||
cJSON_Delete(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- set_vol: 设置音量 ----
|
||||
if (cmd == "set_vol") {
|
||||
cJSON* vol_item = cJSON_GetObjectItem(data, "vol");
|
||||
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;
|
||||
if (vol < 0) vol = 0;
|
||||
if (vol > 100) vol = 100;
|
||||
|
||||
auto* codec = board.GetAudioCodec();
|
||||
if (codec) {
|
||||
codec->SetOutputVolume(vol);
|
||||
// 同时持久化到 NVS
|
||||
Settings s("audio", true);
|
||||
s.SetInt("output_volume", vol);
|
||||
}
|
||||
|
||||
service.SendResponse(cmd, msg_id, 0, "ok");
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- reboot: 重启设备 ----
|
||||
if (cmd == "reboot") {
|
||||
service.SendResponse(cmd, msg_id, 0, "rebooting");
|
||||
vTaskDelay(pdMS_TO_TICKS(500)); // 等待响应发出
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
Reboot();
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- ota: 触发 OTA 升级 ----
|
||||
if (cmd == "ota") {
|
||||
if (device_state_ == kDeviceStateUpgrading) {
|
||||
service.SendResponse(cmd, msg_id, -1, "already upgrading");
|
||||
@ -3222,7 +3199,6 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- iot: 转发 IoT 命令 ----
|
||||
if (cmd == "iot") {
|
||||
auto& thing_manager = iot::ThingManager::GetInstance();
|
||||
std::string states;
|
||||
@ -3236,6 +3212,6 @@ void Application::HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON
|
||||
return;
|
||||
}
|
||||
|
||||
// ---- 未知命令 ----
|
||||
service.SendResponse(cmd, msg_id, -99, "unknown cmd");
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
#include "ota.h"
|
||||
#include "background_task.h"
|
||||
#include "audio/simple_pipeline.h"
|
||||
#include "ble_service.h"
|
||||
// #include "ble_service.h" // BLE JSON Service 暂不使用
|
||||
|
||||
#if CONFIG_USE_WAKE_WORD_DETECT
|
||||
#include "wake_word_detect.h"
|
||||
@ -109,8 +109,8 @@ public:
|
||||
bool IsDialogUploadEnabled() const { return dialog_upload_enabled_; }// 是否启用对话上传
|
||||
void SetDialogUploadEnabled(bool enabled);// 设置对话上传状态
|
||||
|
||||
// BLE JSON 命令处理 (由 WifiBoard 中的 BleJsonService 回调)
|
||||
void HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON* data, BleJsonService& service);
|
||||
// // BLE JSON 命令处理(暂不使用)
|
||||
// void HandleBleJsonCommand(const std::string& cmd, int msg_id, cJSON* data, BleJsonService& service);
|
||||
|
||||
private:
|
||||
Application();// 构造函数
|
||||
|
||||
@ -410,60 +410,44 @@ bool WifiBoard::StartBleProvisioning() {
|
||||
return true;// 启动成功,返回true
|
||||
}
|
||||
|
||||
// 使用 BLE JSON Service 进行配网 (替代 BLE)
|
||||
bool WifiBoard::StartBleJsonProvisioning() {
|
||||
ESP_LOGI(TAG, "🔵 正在启动BLE JSON配网服务...");
|
||||
|
||||
Application::GetInstance().StopAudioProcessor();
|
||||
Application::GetInstance().ClearAudioQueue();
|
||||
|
||||
// 初始化 BLE JSON 服务
|
||||
if (!ble_json_service_.Initialize()) {
|
||||
ESP_LOGE(TAG, "❌ BLE JSON服务初始化失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 设置命令回调,转发给 Application 处理
|
||||
ble_json_service_.SetCommandCallback(
|
||||
[this](const std::string& cmd, int msg_id, cJSON* data) {
|
||||
Application::GetInstance().HandleBleJsonCommand(cmd, msg_id, data, ble_json_service_);
|
||||
});
|
||||
|
||||
// 启动 BLE JSON 服务
|
||||
if (!ble_json_service_.Start("Airhub_Ble")) {
|
||||
ESP_LOGE(TAG, "❌ BLE JSON服务启动失败");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "✅ BLE JSON配网启动成功");
|
||||
|
||||
ble_provisioning_active_ = true;
|
||||
ble_start_time_ = xTaskGetTickCount();
|
||||
|
||||
// 显示配网通知
|
||||
auto display = GetDisplay();
|
||||
if (display) {
|
||||
display->ShowNotification("BLE配网模式", 30000);
|
||||
}
|
||||
|
||||
// 播放配网提示音
|
||||
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 JSON Service 配网(暂不使用,保留代码)
|
||||
// bool WifiBoard::StartBleJsonProvisioning() {
|
||||
// ESP_LOGI(TAG, "🔵 正在启动BLE JSON配网服务...");
|
||||
// Application::GetInstance().StopAudioProcessor();
|
||||
// Application::GetInstance().ClearAudioQueue();
|
||||
// if (!ble_json_service_.Initialize()) {
|
||||
// ESP_LOGE(TAG, "❌ BLE JSON服务初始化失败");
|
||||
// return false;
|
||||
// }
|
||||
// ble_json_service_.SetCommandCallback(
|
||||
// [this](const std::string& cmd, int msg_id, cJSON* data) {
|
||||
// Application::GetInstance().HandleBleJsonCommand(cmd, msg_id, data, ble_json_service_);
|
||||
// });
|
||||
// if (!ble_json_service_.Start("Airhub_Ble")) {
|
||||
// ESP_LOGE(TAG, "❌ BLE JSON服务启动失败");
|
||||
// return false;
|
||||
// }
|
||||
// ESP_LOGI(TAG, "✅ BLE JSON配网启动成功");
|
||||
// ble_provisioning_active_ = true;
|
||||
// ble_start_time_ = xTaskGetTickCount();
|
||||
// auto display = GetDisplay();
|
||||
// if (display) {
|
||||
// display->ShowNotification("BLE配网模式", 30000);
|
||||
// }
|
||||
// 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配网状态
|
||||
void WifiBoard::MonitorBleProvisioning() {
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
#include "board.h"
|
||||
#include "bluetooth_provisioning.h"
|
||||
#include "ble_service.h"
|
||||
// #include "ble_service.h" // BLE JSON Service 暂不使用
|
||||
#include <string>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
@ -35,7 +35,7 @@ protected:
|
||||
TickType_t ble_start_time_ = 0; ///< BLE配网开始时间戳
|
||||
static const TickType_t BLE_PROV_TIMEOUT_MS = 300000; ///< BLE配网超时时间(5分钟)
|
||||
BluetoothProvisioning bluetooth_provisioning_; ///< BLE蓝牙配网实例对象
|
||||
BleJsonService ble_json_service_; ///< BLE JSON 配网服务实例
|
||||
// BleJsonService ble_json_service_; ///< BLE JSON 配网服务实例(暂不使用)
|
||||
|
||||
/**
|
||||
* @brief 构造函数
|
||||
@ -65,8 +65,8 @@ protected:
|
||||
*/
|
||||
bool StartBleProvisioning();
|
||||
|
||||
// 使用 BLE JSON Service 进行配网
|
||||
bool StartBleJsonProvisioning();
|
||||
// // 使用 BLE JSON Service 进行配网(暂不使用)
|
||||
// bool StartBleJsonProvisioning();
|
||||
|
||||
/**
|
||||
* @brief 监控BLE配网进程
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user