From 821c264400205bc3b68d26d5ba239a53e99175a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=96=E5=8F=81?= Date: Fri, 27 Dec 2024 10:16:08 +0800 Subject: [PATCH] feat: update device name --- include/ble.h | 2 ++ include/handlers.h | 4 ++++ include/storage.h | 1 + packet.md | 12 ++++++++++++ src/ble.cpp | 21 +++++++++++++++++++++ src/handlers.cpp | 19 +++++++++++++++++++ src/main.cpp | 2 +- src/storage.cpp | 2 +- 8 files changed, 61 insertions(+), 2 deletions(-) diff --git a/include/ble.h b/include/ble.h index 3480a42..c7f91dd 100644 --- a/include/ble.h +++ b/include/ble.h @@ -17,6 +17,8 @@ public: static BLECharacteristic *pTxCharacteristic; static void init(String deviceName); + static void updateDeviceName(const String &newName); + static void restart(); }; #endif diff --git a/include/handlers.h b/include/handlers.h index 7bfbc29..f95b924 100644 --- a/include/handlers.h +++ b/include/handlers.h @@ -12,6 +12,9 @@ #define CMD_GET_BT_STATUS 0x10 #define CMD_GET_SPIFFS_STATUS 0x11 #define CMD_GET_DISTANCE 0x12 + +#define CMD_SET_NAME 0xA1 + #define CMD_MOTOR_MOVE_CONTROL 0x20 #define CMD_MOTOR_STEER_CONTROL 0x21 #define CMD_MOTOR_SINGLE_CONTROL 0x22 @@ -28,6 +31,7 @@ public: static void getBTStatus(BLECharacteristic &characteristic); static void getSPIFFSStatus(BLECharacteristic &characteristic); static void getDistance(BLECharacteristic &characteristic); + static void setName(BLECharacteristic &characteristic, uint8_t *packet); static void motorMoveControl(BLECharacteristic &characteristic, uint8_t *packet); static void motorSteerControl(BLECharacteristic &characteristic, uint8_t *packet); static void motorRotateControl(BLECharacteristic &characteristic, uint8_t *packet); diff --git a/include/storage.h b/include/storage.h index 31747ed..a11cf84 100644 --- a/include/storage.h +++ b/include/storage.h @@ -3,6 +3,7 @@ #include #include +#include "consts.h" class Storage { diff --git a/packet.md b/packet.md index 0f99f66..5308cab 100644 --- a/packet.md +++ b/packet.md @@ -110,6 +110,18 @@ 控制示例 `00 07 24 01 01 01 FF` +## 设置 + +### 设置设备名称 `0xA1` + +> ⚠️ 注意: 设备名称长度不能超过 16 个字符,此时最大包体长度 20,仅支持 ASCII 码 + +包体 `00 包体长度 A1 设备名称 FF` + +> 示例:WhiteTiger + +设置示例 `00 0E A1 57 68 69 74 65 54 69 67 65 72 FF` + ## 状态回报 ### 电机状态汇报 `0xE0` diff --git a/src/ble.cpp b/src/ble.cpp index 67b6ae7..42312e0 100644 --- a/src/ble.cpp +++ b/src/ble.cpp @@ -57,3 +57,24 @@ void BLEManager::init(String deviceName) pService->start(); pServer->getAdvertising()->start(); } + +void BLEManager::updateDeviceName(const String &newName) { + // 保存新的设备名称 + Storage::setName(newName); + + // 停止广播 + if (pServer) { + pServer->getAdvertising()->stop(); + } + + // 更新设备名称 + BLEDevice::deinit(true); + BLEDevice::init(newName.c_str()); + + // 重新初始化 BLE 服务 + init(newName); +} + +void BLEManager::restart() { + ESP.restart(); // 重启 ESP32 +} diff --git a/src/handlers.cpp b/src/handlers.cpp index eca7241..1ea56d3 100644 --- a/src/handlers.cpp +++ b/src/handlers.cpp @@ -42,6 +42,25 @@ void Handlers::getDistance(BLECharacteristic &characteristic) characteristic.notify(); } +void Handlers::setName(BLECharacteristic &characteristic, uint8_t *packet) +{ + unsigned char packetLength = packet[1]; + unsigned char nameLength = packetLength - 4; + char name[17]; + for (int i = 0; i <= nameLength; i++) + { + if (i == nameLength) + { + name[i] = '\0'; + } + else + { + name[i] = packet[i + 3]; + } + } + BLEManager::updateDeviceName(name); +} + void Handlers::motorMoveControl(BLECharacteristic &characteristic, uint8_t *packet) { uint8_t direction = packet[3]; diff --git a/src/main.cpp b/src/main.cpp index 6d86a73..2b269f4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ void setup() Storage::init(); // 初始化 BLE - BLEManager::init(DEVICE_NAME); + BLEManager::init(Storage::getName()); // 初始化电机 MotorController::init( diff --git a/src/storage.cpp b/src/storage.cpp index 01ba4b1..fdb06f7 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -75,7 +75,7 @@ void Storage::setName(String name) String Storage::getName() { if (!isMounted) - return ""; + return DEVICE_NAME; File file = SPIFFS.open("/name.txt", "r"); String name = file.readStringUntil('\n');