Fix: Improve BLE data reception and WiFi connection stability
- Add BLE data timeout handling: auto-process data when timeout occurs - Fix WiFi reconnection: add scanning conflict detection with isScanning flag - Fix WiFi scanning timeout: reset WiFi hardware state before scanning - Improve scan conflict handling: wait for scan completion instead of skipping - Add WiFi.scanDelete() calls to prevent hardware state issues - Add proper isScanning flag management in all scanning functions
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,6 +3,5 @@
|
|||||||
.vscode/c_cpp_properties.json
|
.vscode/c_cpp_properties.json
|
||||||
.vscode/launch.json
|
.vscode/launch.json
|
||||||
.vscode/ipch
|
.vscode/ipch
|
||||||
GIT_GUIDE.md
|
Git提交方法.md
|
||||||
git_upload.bat
|
|
||||||
.trae/
|
.trae/
|
||||||
|
|||||||
@@ -170,10 +170,21 @@ bool WiFiManager::connectToNetwork(const char* ssid, const char* password) {
|
|||||||
*/
|
*/
|
||||||
bool WiFiManager::scanAndMatchNetworks() {
|
bool WiFiManager::scanAndMatchNetworks() {
|
||||||
if (isScanning) {
|
if (isScanning) {
|
||||||
Serial.println("⚠️ [WiFi] 正在扫描中,跳过本次扫描");
|
Serial.println("⏳ [WiFi] 正在扫描中,等待扫描完成...");
|
||||||
|
int waitCount = 0;
|
||||||
|
while (isScanning && waitCount < 50) {
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
waitCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isScanning) {
|
||||||
|
Serial.println("⚠️ [WiFi] 等待超时,跳过本次扫描");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Serial.println("✅ [WiFi] 扫描已完成,开始新的扫描");
|
||||||
|
}
|
||||||
|
|
||||||
Serial.println("🔍 [WiFi] 开始扫描WiFi网络...");
|
Serial.println("🔍 [WiFi] 开始扫描WiFi网络...");
|
||||||
currentState = WIFI_SCANNING;
|
currentState = WIFI_SCANNING;
|
||||||
isScanning = true;
|
isScanning = true;
|
||||||
@@ -279,14 +290,25 @@ bool WiFiManager::initializeWiFi() {
|
|||||||
*/
|
*/
|
||||||
void WiFiManager::scanAndSendResults() {
|
void WiFiManager::scanAndSendResults() {
|
||||||
if (isScanning) {
|
if (isScanning) {
|
||||||
Serial.println("⚠️ [WiFi] 正在扫描中,跳过本次扫描");
|
Serial.println("⏳ [WiFi] 正在扫描中,等待扫描完成...");
|
||||||
|
int waitCount = 0;
|
||||||
|
while (isScanning && waitCount < 50) {
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
waitCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isScanning) {
|
||||||
|
Serial.println("⚠️ [WiFi] 等待超时,跳过本次扫描");
|
||||||
if (deviceConnected) {
|
if (deviceConnected) {
|
||||||
String errorMsg = String("{\"type\":\"scanWiFiResult\",\"success\":false,\"message\":\"正在扫描中,请稍后再试\",\"networks\":[],\"count\":0}");
|
String errorMsg = String("{\"type\":\"scanWiFiResult\",\"success\":false,\"message\":\"等待扫描超时,请稍后再试\",\"networks\":[],\"count\":0}");
|
||||||
sendJSONDataToBLE(errorMsg);
|
sendJSONDataToBLE(errorMsg);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Serial.println("✅ [WiFi] 扫描已完成,开始新的扫描");
|
||||||
|
}
|
||||||
|
|
||||||
Serial.println("📱 [BLE-WiFi] 开始WiFi扫描...");
|
Serial.println("📱 [BLE-WiFi] 开始WiFi扫描...");
|
||||||
isScanning = true;
|
isScanning = true;
|
||||||
|
|
||||||
@@ -399,13 +421,39 @@ bool WiFiManager::handleConfigurationData(const char* ssid, const char* password
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isScanning) {
|
||||||
|
Serial.println("⏳ [WiFi] 正在扫描中,等待扫描完成...");
|
||||||
|
int waitCount = 0;
|
||||||
|
while (isScanning && waitCount < 50) {
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
waitCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isScanning) {
|
||||||
|
Serial.println("⚠️ [WiFi] 等待超时");
|
||||||
|
if (deviceConnected) {
|
||||||
|
String errorMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"等待扫描超时,请稍后再试\"}");
|
||||||
|
sendJSONDataToBLE(errorMsg);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("✅ [WiFi] 扫描已完成,开始新的扫描");
|
||||||
|
}
|
||||||
|
|
||||||
// 先扫描WiFi网络,检查是否存在匹配的网络
|
// 先扫描WiFi网络,检查是否存在匹配的网络
|
||||||
Serial.println("🔍 [WiFi] 扫描WiFi网络,检查是否存在匹配的网络...");
|
Serial.println("🔍 [WiFi] 扫描WiFi网络,检查是否存在匹配的网络...");
|
||||||
|
currentState = WIFI_SCANNING;
|
||||||
|
isScanning = true;
|
||||||
|
|
||||||
int n = WiFi.scanNetworks();
|
int n = WiFi.scanNetworks();
|
||||||
Serial.printf("🔍 扫描到 %d 个WiFi网络\n", n);
|
Serial.printf("🔍 扫描到 %d 个WiFi网络\n", n);
|
||||||
|
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
Serial.println("❌ 未扫描到任何WiFi网络");
|
Serial.println("❌ 未扫描到任何WiFi网络");
|
||||||
|
WiFi.scanDelete();
|
||||||
|
isScanning = false;
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
|
||||||
if (deviceConnected) {
|
if (deviceConnected) {
|
||||||
String resultMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"未扫描到任何WiFi网络,请检查设备位置\"}");
|
String resultMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"未扫描到任何WiFi网络,请检查设备位置\"}");
|
||||||
@@ -445,12 +493,20 @@ bool WiFiManager::handleConfigurationData(const char* ssid, const char* password
|
|||||||
Serial.println("❌ 未找到目标WiFi网络");
|
Serial.println("❌ 未找到目标WiFi网络");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WiFi.scanDelete();
|
||||||
|
isScanning = false;
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
|
||||||
if (deviceConnected) {
|
if (deviceConnected) {
|
||||||
sendJSONDataToBLE(errorMsg);
|
sendJSONDataToBLE(errorMsg);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WiFi.scanDelete();
|
||||||
|
isScanning = false;
|
||||||
|
vTaskDelay(300 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
// 尝试连接到指定网络
|
// 尝试连接到指定网络
|
||||||
if (connectToNetwork(ssid, password)) {
|
if (connectToNetwork(ssid, password)) {
|
||||||
// 连接成功后保存配置
|
// 连接成功后保存配置
|
||||||
@@ -466,6 +522,8 @@ bool WiFiManager::handleConfigurationData(const char* ssid, const char* password
|
|||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("❌ WiFi配置失败");
|
Serial.println("❌ WiFi配置失败");
|
||||||
|
isScanning = false;
|
||||||
|
currentState = WIFI_DISCONNECTED;
|
||||||
|
|
||||||
if (deviceConnected) {
|
if (deviceConnected) {
|
||||||
String resultMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"WiFi配置失败,请检查密码是否正确\"}");
|
String resultMsg = String("{\"type\":\"wifiConfigResult\",\"success\":false,\"message\":\"WiFi配置失败,请检查密码是否正确\"}");
|
||||||
|
|||||||
Reference in New Issue
Block a user