| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- //
- // Created by kindring on 2025/12/25.
- //
- #include "ble_nvs.h"
- #include "esp_err.h"
- #include "esp_log.h"
- #include "nvs_flash.h"
- static const char *TAG = "BLE_NVS";
- #define NVS_STORAGE_NAMESPACE "bt_device"
- #define NVS_KEY_BT_ADDR "last_addr"
- #define NVS_KEY_BT_NAME "last_name"
- #define NVS_KEY_IS_PAIRED "is_paired"
- #define NVS_KEY_CONNECT_COUNT "connect_count"
- #define NVS_KEY_LAST_CONNECT_TIME "last_connect_time"
- #define NVS_KEY_AUTO_RECONNECT "auto_reconnect"
- static bool s_auto_reconnect = true;
- static bool s_reconnect_active = false;
- esp_err_t ble_nvs_init()
- {
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_LOGW(TAG, "NVS partition needs to be erased");
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to initialize NVS: %s", esp_err_to_name(ret));
- return ret;
- }
- ESP_LOGI(TAG, "Bluetooth Device Manager initialized");
- return ESP_OK;
- }
- esp_err_t bt_device_save_info(const bt_device_info_t *device_info)
- {
- if (!device_info) {
- return ESP_ERR_INVALID_ARG;
- }
- nvs_handle_t handle;
- esp_err_t ret;
- ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READWRITE, &handle);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Error opening NVS handle: %s", esp_err_to_name(ret));
- return ret;
- }
- // 保存蓝牙设备地址
- ret = nvs_set_blob(handle, NVS_KEY_BT_ADDR, device_info->bda, 6);
- if (ret != ESP_OK)
- {
- ESP_LOGE(TAG, "Failed to save Bluetooth address: %s", esp_err_to_name(ret));
- nvs_close(handle);
- return ret;
- }
- // 保存设备名称
- ret = nvs_set_str(handle, NVS_KEY_BT_NAME, device_info->device_name);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to save device name: %s", esp_err_to_name(ret));
- nvs_close(handle);
- return ret;
- }
- // 保存配对状态
- ret = nvs_set_u8(handle, NVS_KEY_IS_PAIRED, device_info->is_paired ? 1 : 0);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to save pairing status: %s", esp_err_to_name(ret));
- nvs_close(handle);
- return ret;
- }
- // 保存连接次数
- ret = nvs_set_u32(handle, NVS_KEY_CONNECT_COUNT, device_info->connect_count);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to save connect count: %s", esp_err_to_name(ret));
- nvs_close(handle);
- return ret;
- }
- // 保存最后连接时间
- ret = nvs_set_u64(handle, NVS_KEY_LAST_CONNECT_TIME, device_info->last_connect_time);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to save last connect time: %s", esp_err_to_name(ret));
- nvs_close(handle);
- return ret;
- }
- // 提交更改
- ret = nvs_commit(handle);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to commit NVS changes: %s", esp_err_to_name(ret));
- }
- nvs_close(handle);
- ESP_LOGI(TAG, "Bluetooth device info saved successfully");
- return ret;
- }
- esp_err_t bt_device_load_info(bt_device_info_t *device_info)
- {
- if (!device_info) {
- return ESP_ERR_INVALID_ARG;
- }
- nvs_handle_t handle;
- esp_err_t ret;
- ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READONLY, &handle);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Error opening NVS handle: %s", esp_err_to_name(ret));
- return ret;
- }
- // 读取蓝牙设备地址
- size_t addr_len = 6;
- ret = nvs_get_blob(handle, NVS_KEY_BT_ADDR, device_info->bda, &addr_len);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to load Bluetooth address: %s", esp_err_to_name(ret));
- nvs_close(handle);
- return ret;
- }
- // 读取设备名称
- size_t name_len = sizeof(device_info->device_name);
- ret = nvs_get_str(handle, NVS_KEY_BT_NAME, device_info->device_name, &name_len);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to load device name: %s", esp_err_to_name(ret));
- nvs_close(handle);
- return ret;
- }
- // 读取配对状态
- uint8_t is_paired;
- ret = nvs_get_u8(handle, NVS_KEY_IS_PAIRED, &is_paired);
- if (ret == ESP_OK) {
- device_info->is_paired = (is_paired != 0);
- } else {
- device_info->is_paired = false;
- }
- // 读取连接次数
- ret = nvs_get_u32(handle, NVS_KEY_CONNECT_COUNT, &device_info->connect_count);
- if (ret != ESP_OK) {
- device_info->connect_count = 0;
- }
- // 读取最后连接时间
- ret = nvs_get_u64(handle, NVS_KEY_LAST_CONNECT_TIME, &device_info->last_connect_time);
- if (ret != ESP_OK) {
- device_info->last_connect_time = 0;
- }
- nvs_close(handle);
- ESP_LOGI(TAG, "Bluetooth device info loaded successfully");
- return ESP_OK;
- }
- esp_err_t bt_device_clear_info(void)
- {
- nvs_handle_t handle;
- esp_err_t ret;
- ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READWRITE, &handle);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Error opening NVS handle: %s", esp_err_to_name(ret));
- return ret;
- }
- // 清除所有键值
- nvs_erase_all(handle);
- ret = nvs_commit(handle);
- nvs_close(handle);
- ESP_LOGI(TAG, "Bluetooth device info cleared");
- return ret;
- }
- void bt_device_set_auto_reconnect(bool enable)
- {
- s_auto_reconnect = enable;
- nvs_handle_t handle;
- if (nvs_open(NVS_STORAGE_NAMESPACE, NVS_READWRITE, &handle) == ESP_OK) {
- nvs_set_u8(handle, NVS_KEY_AUTO_RECONNECT, enable ? 1 : 0);
- nvs_commit(handle);
- nvs_close(handle);
- }
- }
- bool bt_device_get_auto_reconnect(void)
- {
- uint8_t auto_reconnect;
- nvs_handle_t handle;
- esp_err_t ret;
- ret = nvs_open(NVS_STORAGE_NAMESPACE, NVS_READONLY, &handle);
- if (ret == ESP_OK) {
- ret = nvs_get_u8(handle, NVS_KEY_AUTO_RECONNECT, &auto_reconnect);
- nvs_close(handle);
- if (ret == ESP_OK) {
- s_auto_reconnect = (auto_reconnect != 0);
- }
- }
- return s_auto_reconnect;
- }
|