From a9c835459990db39ae2db5157d7ad835dcd25b44 Mon Sep 17 00:00:00 2001
From: lefengyang <lefengyang@tencent.com>
Date: Tue, 15 Sep 2026 13:43:56 +0800
Subject: [PATCH] 发送优化

---
 ledApp/pages/index/index.vue |   61 ++++++++++++++++++++++++++----
 1 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/ledApp/pages/index/index.vue b/ledApp/pages/index/index.vue
index 2bd63fe..d5f0d68 100644
--- a/ledApp/pages/index/index.vue
+++ b/ledApp/pages/index/index.vue
@@ -192,7 +192,9 @@
 				devKwh: '',
 				platform: '',
 				screenOn: true,
-				waitDallThenSend: false // 标记:收到DALL清除所有信息指令回应后,再开始发送INST帧
+				waitDallThenSend: false, // 标记:收到DALL清除所有信息指令回应后,再开始发送INST帧
+				instTimer: null, // INST发送超时计时器:发送INST帧后等待回应,超过5秒未收到则判定发送失败
+				instSendAborted: false // 标记:INST发送已失败/中止,收到迟到的回应时忽略,不再继续发送
 			}
 		},
 		onLoad() {
@@ -403,6 +405,9 @@
 				}
 
 				if (this.ledData.length > 0) {
+					// 重置发送状态:清除残留计时器、重置失败标记
+					this.clearInstTimer();
+					this.instSendAborted = false;
 					// 向智能壳发送INST帧前,先发送一次清除所有信息指令(DALL),待智能壳回应后再开始发送
 					this.waitDallThenSend = true;
 					this.commandDALL();
@@ -497,7 +502,7 @@
 				var that = this
 				uni.setBLEMTU({
 					deviceId: this.deviceId,
-					mtu: 80, // 最大值一般 512,蓝牙 4.2 通常支持 247
+					mtu: 512, // 最大值一般 512,蓝牙 4.2 通常支持 247
 					success: (res) => {
 						console.log('设置MTU成功:', res);
 						uni.hideLoading();
@@ -748,6 +753,31 @@
 					this.writeBLECharacteristicValue(buffer, true)
 				}, 50);
 			},
+			// 启动INST发送超时计时器:发送INST帧后等待智能壳回应,超过5秒未收到则判定发送失败
+			startInstTimer() {
+				this.clearInstTimer();
+				this.instTimer = setTimeout(() => {
+					this.handleInstTimeout();
+				}, 5000);
+			},
+			// 清除INST发送超时计时器
+			clearInstTimer() {
+				if (this.instTimer) {
+					clearTimeout(this.instTimer);
+					this.instTimer = null;
+				}
+			},
+			// INST发送超时处理:停止发送并提示发送失败
+			handleInstTimeout() {
+				this.instTimer = null;
+				this.instSendAborted = true;
+				uni.hideLoading();
+				uni.showToast({
+					title: '发送失败',
+					icon: 'none',
+					mask: true
+				});
+			},
 			//添加信息指令(INST)
 			commandINST(hexString) {
 				// 1.手机发指令:启动+帧头+包头段(0x43+简易绑定号+屏大小)+INST+信息属性+文件名+总帧数+当前帧*+块描述+块数据+CRC校验码+帧尾
@@ -812,11 +842,13 @@
 					title: '发送中' + sendPercent + '%',
 					mask: true
 				});
-				setTimeout(() => {
+				setTimeout(async () => {
 					//this.writeBLECharacteristicValue(buffer)
-					this.sendBLEDataInChunks(
-						this.hexArrayToHexString(data)
-					)
+					const ok = await this.sendBLEDataInChunks(this.hexArrayToHexString(data))
+					if (ok) {
+						// 数据发送成功后启动超时计时器,等待智能壳回应,超过5秒未收到则判定发送失败
+						this.startInstTimer()
+					}
 				}, 50);
 			},
 			// 发送结束针
@@ -870,6 +902,8 @@
 				const endbuffer = new Uint8Array(endData).buffer;
 				setTimeout(() => {
 					this.writeBLECharacteristicValue(endbuffer, true)
+					// 结束帧发送后启动超时计时器,等待智能壳回应,超过5秒未收到则判定发送失败
+					this.startInstTimer()
 				}, 50);
 			},
 			async sendBLEDataInChunks(hexStr) {
@@ -900,14 +934,19 @@
 							})
 						})
 					} catch (err) {
-						console.error(`🚫 发送中断,第 ${i + 1} 包发送失败`)
+						console.error(`🚫 发送中断,第 ${i + 1} 帧发送失败`, err)
 						uni.hideLoading();
-						break
+						uni.showToast({
+							title: `🚫 发送中断,第 ${i + 1} 帧发送失败`,
+							duration: 4000
+						});
+						return false
 					}
 					// 延迟下一包发送
 					await new Promise(r => setTimeout(r, interval))
 				}
 				//console.log("🎉 数据发送完成")
+				return true
 			},
 			// 读取电量
 			commandPOWR() {
@@ -1025,6 +1064,12 @@
 							} else if (resName == 'PLAY') {
 
 							} else if (resName == 'INST') {
+								// 收到INST回应,清除发送超时计时器
+								that.clearInstTimer();
+								// 若已因超时中止发送,忽略迟到的回应,不再继续发送
+								if (that.instSendAborted) {
+									return;
+								}
 								// 添加动画帧
 								that.ledCurrent++
 								if (that.ledCurrent < that.ledTotal) {

--
Gitblit v1.10.0