From 500b461bdd86a7c352e11af1e4827f9f1bb33ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=BE=AE=E4=B8=80=E7=AC=91?= <709648985@qq.com> Date: Sat, 1 Nov 2025 17:21:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D7305=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/7305/BJQ7305.vue | 94 ++++++++++++++++++++++++++++-------- utils/BleHelper.js | 107 ++++++++++++++++++++++++++++------------- utils/BleReceive.js | 22 ++++++--- 3 files changed, 162 insertions(+), 61 deletions(-) diff --git a/pages/7305/BJQ7305.vue b/pages/7305/BJQ7305.vue index dc872dd..45287c8 100644 --- a/pages/7305/BJQ7305.vue +++ b/pages/7305/BJQ7305.vue @@ -301,6 +301,17 @@ these.showBleUnConnect(); return; } + + // 确保设备对象的detailPageUrl正确设置,以便ReceiveData能找到对应的handler + if (f.device) { + f.device.detailPageUrl = "/pages/7305/BJQ7305"; + } else { + f.device = { + ...device, + detailPageUrl: "/pages/7305/BJQ7305" + }; + } + let form = f.formData; if (form) { let keys = Object.keys(form); @@ -314,9 +325,23 @@ these.formData.img = device.devicePic; these.formData.id = device.id; these.formData.deviceId = f.deviceId; + // 更新缓存中的设备信息 + ble.updateCache(); + ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => { - console.log("连接成功") + console.log("连接成功"); these.formData.bleStatu = true; + + // 确保订阅消息已开启 + let linkedDevice = ble.data.LinkedList.find(v => v.deviceId == f.deviceId); + if (linkedDevice && !linkedDevice.notifyState) { + console.log("连接成功但未订阅,主动订阅消息"); + ble.subScribe(f.deviceId, true).then(() => { + console.log("订阅消息成功"); + }).catch(err => { + console.error("订阅消息失败:", err); + }); + } }); these.setBleFormData(); these.getDetail(); @@ -386,6 +411,18 @@ } if (res.deviceId == these.formData.deviceId) { this.formData.bleStatu = true; + + // 连接恢复后,确保重新订阅消息以接收设备上报的数据 + let f = this.getDevice(); + if (f && (f.notifyServiceid || f.notifyCharactId)) { + console.log("连接恢复,重新订阅消息"); + ble.subScribe(res.deviceId, true).then(() => { + console.log("订阅消息成功,等待设备上报数据"); + }).catch(err => { + console.error("订阅消息失败:", err); + }); + } + setTimeout(() => { hideLoading(these, 1000); }); @@ -475,30 +512,49 @@ } let json = recei.ReceiveData(receive, device, path, recArr); - if (!json) { + // 检查返回的数据是否有效(应该是解析后的对象,不是原始receive对象) + // 如果返回的是原始receive对象(有bytes属性),说明handler没有匹配或解析失败 + if (!json || (json.hasOwnProperty('bytes') && (!json.battary && json.battary !== 0))) { + console.log("收到7305数据但未解析或解析失败:", json); return; } + + console.log("收到7305解析数据:", json); + console.log("formData更新前 - battary:", these.formData.battary, "xuhang:", these.formData.xuhang, "statu:", these.formData.statu); + + // 确保电量和续航时间正确更新(使用 $set 确保 Vue 响应式) + if (json.battary !== undefined && json.battary !== null) { + this.$set(these.formData, 'battary', json.battary); + console.log("更新电量:", json.battary, "->", these.formData.battary); + if (json.battary <= 20) { + this.showPop({ + message: "设备电量低", + iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png", + borderColor: "#e034344d", + buttonBgColor: "#E03434", + }); + } + } + if (json.xuhang !== undefined && json.xuhang !== null) { + this.$set(these.formData, 'xuhang', json.xuhang); + console.log("更新续航时间:", json.xuhang, "->", these.formData.xuhang); + } + + // 更新充电状态 + if (json.statu !== undefined && json.statu !== null) { + this.$set(these.formData, 'statu', json.statu); + console.log("更新充电状态:", json.statu, "->", these.formData.statu); + } + + // 更新其他字段 let keys = Object.keys(json); keys.forEach((key) => { - if (key in these.formData) { - these.formData[key] = json[key]; + if (key in these.formData && key !== 'battary' && key !== 'xuhang' && key !== 'statu') { + this.$set(these.formData, key, json[key]); } }); - if ('statu' in json) { - these.formData.statu = json.statu == '1' ? '充电中' : '未充电'; - } - if ('xuhang' in json) { - these.formData.xuhang = json.xuhang; - } - - if ('battary' in json && this.formData.battary <= 20) { - this.showPop({ - message: "设备电量低", - iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png", - borderColor: "#e034344d", - buttonBgColor: "#E03434", - }); - } + + console.log("formData更新后 - battary:", these.formData.battary, "xuhang:", these.formData.xuhang, "statu:", these.formData.statu); }, diff --git a/utils/BleHelper.js b/utils/BleHelper.js index 7a861bd..bba40b3 100644 --- a/utils/BleHelper.js +++ b/utils/BleHelper.js @@ -752,30 +752,35 @@ class BleHelper { } try { - let receivJson = JSON.parse(str); - let key = "sta_address"; //HBY100以此方式上传mac地址 - if (key in receivJson) { - this.data.LinkedList.find((v) => { - if (v.deviceId == receive - .deviceId) { - let macStr = receivJson[ - key]; - if (macStr.includes(':')) { - v.macAddress = macStr; - } else { - v.macAddress = macStr - .replace(/(.{2})/g, - '$1:').slice(0, - -1) + let trimmedStr = str.trim(); + if (trimmedStr && (trimmedStr.startsWith('{') || trimmedStr.startsWith('['))) { + let receivJson = JSON.parse(str); + let key = "sta_address"; //HBY100以此方式上传mac地址 + if (key in receivJson) { + this.data.LinkedList.find((v) => { + if (v.deviceId == receive + .deviceId) { + let macStr = receivJson[ + key]; + if (macStr.includes(':')) { + v.macAddress = macStr; + } else { + v.macAddress = macStr + .replace(/(.{2})/g, + '$1:').slice(0, + -1) + } + + isUpdate = true; } + }); - isUpdate = true; - } - }); - + } } } catch (convertException) { - console.error("文本无法转json", convertException) + if (str && (str.trim().startsWith('{') || str.trim().startsWith('['))) { + console.error("JSON解析失败(可能是格式错误的数据)", convertException); + } } if (isUpdate) { @@ -1029,7 +1034,7 @@ class BleHelper { //订阅消息 subScribe(deviceId, state) { - // console.log("开始订阅消息", state); + console.log("开始订阅消息", deviceId, state); return new Promise((resolve, reject) => { setTimeout(() => { @@ -1150,13 +1155,12 @@ class BleHelper { results.forEach((result, index) => { if (result.status === "fulfilled") { - // console.log(`操作${index + 1}成功:`, result.value); + console.log(`订阅消息操作${index + 1}成功:`, result.value); } else { - // console.log(`操作${index + 1}失败:`, result.reason - // .message); + console.error(`订阅消息操作${index + 1}失败:`, result.reason); } }); - // console.log("订阅消息成功"); + console.log("订阅消息完成,deviceId:", deviceId); resolve(); }).catch((ex) => { console.error("异常,ex=", ex); @@ -1547,9 +1551,19 @@ class BleHelper { } } else { //已连接过,直接订阅消息 // console.log("11111111"); - if (fIndex > -1 && f && !f.notifyState) { - - this.subScribe(deviceId, true); + if (fIndex > -1 && f) { + if (!f.notifyState) { + console.log("设备已连接但未订阅,开始订阅消息"); + return this.subScribe(deviceId, true).then(() => { + console.log("订阅消息完成"); + return Promise.resolve(true); + }).catch(err => { + console.error("订阅消息失败:", err); + return Promise.resolve(true); + }); + } else { + console.log("设备已连接且已订阅消息"); + } } return Promise.resolve(true); //已连接过的设备无需获取服务 } @@ -1698,8 +1712,32 @@ class BleHelper { if (this.data.platform == 'web') { return Promise.resolve("h5平台默认成功"); } - // console.log("deviceid=" + deviceid + ",writeServiceId=" + writeServiceId + ",wirteCharactId=" + - // wirteCharactId + ",timeout=" + ms) + + // 打印发送的蓝牙指令 + let bufferHex = ''; + if (buffer) { + let bytes = []; + // 处理不同类型的buffer(ArrayBuffer、Uint8Array等) + if (buffer instanceof ArrayBuffer) { + let dataView = new DataView(buffer); + for (let i = 0; i < buffer.byteLength; i++) { + bytes.push(dataView.getUint8(i)); + } + } else if (buffer.byteLength !== undefined) { + // 如果是 Uint8Array 或其他类型 + for (let i = 0; i < buffer.byteLength; i++) { + bytes.push(buffer[i] || 0); + } + } else if (Array.isArray(buffer)) { + bytes = buffer; + } + if (bytes.length > 0) { + bufferHex = bytes.map(b => '0x' + b.toString(16).padStart(2, '0').toUpperCase()).join(' '); + } + } + console.log("准备发送蓝牙指令 - deviceId:", deviceid, "writeServiceId:", writeServiceId, "writeCharactId:", wirteCharactId); + console.log("发送数据(Hex):", bufferHex || "(空数据)"); + console.log("发送数据(原始buffer长度):", buffer ? (buffer.byteLength || buffer.length || 0) : 0); if (ms === undefined) { ms = 50; } @@ -1743,13 +1781,14 @@ class BleHelper { serviceId: device.writeServiceId, characteristicId: device.wirteCharactId, value: buffer, + writeType: 'write', success: () => { - // console.log("发送数据成功"); + console.log("✓ 蓝牙指令发送成功 - deviceId:", device.deviceId); succ(); }, fail: (ex) => { ex = this.getError(ex); - console.error("发送数据失败", ex); + console.error("✗ 蓝牙指令发送失败 - deviceId:", device.deviceId, "错误:", ex); err(ex); } @@ -1787,10 +1826,10 @@ class BleHelper { } if (c.Linked) { - // console.log("蓝牙已连接,直接发送"); + console.log("蓝牙已连接,直接发送数据"); return sendBuffer(); } else { - // console.log("先连接蓝牙再发送"); + console.log("蓝牙未连接,先连接蓝牙再发送数据"); return new Promise((resolve, reject) => { let f = this.data.LinkedList.find((v) => { return v.deviceId == deviceid; diff --git a/utils/BleReceive.js b/utils/BleReceive.js index 3897d3f..5394ef8 100644 --- a/utils/BleReceive.js +++ b/utils/BleReceive.js @@ -44,24 +44,27 @@ class BleReceive { if (f && f.macAddress && f.device && f.device.id) { let handler = null; let keys = Object.keys(this.HandlerMap); + let devKey = f.device.detailPageUrl ? f.device.detailPageUrl.replace(/\//g, '').toLowerCase() : ''; + console.log("查找handler - detailPageUrl:", f.device.detailPageUrl, "转换后:", devKey); for (let index = 0; index < keys.length; index++) { - let key = keys[index].replaceAll('/', '').toLowerCase(); - let devKey = f.device.detailPageUrl ? f.device.detailPageUrl.replaceAll('/', '').toLowerCase() : ''; + let key = keys[index].replace(/\//g, '').toLowerCase(); if (key == devKey) { handler = this.HandlerMap[keys[index]]; + console.log("找到匹配的handler:", keys[index]); break; } } if (handler) { let data = handler(receive, f, path, recArr); + console.log("handler返回的数据:", data); return data; } else { - console.log("已收到消息,但无指定处理程序", receive); + console.log("已收到消息,但无指定处理程序, deviceUrl:", f.device.detailPageUrl, "可用handlers:", keys); } } else { - console.log("已收到该消息,但无法处理", receive); + console.log("已收到该消息,但无法处理", receive, "f:", f); } return receive; @@ -466,12 +469,13 @@ class BleReceive { formData.battary = batteryLevel; formData.statu = warn; formData.xuhang = lightingTime; - + + console.log("7305解析结果 - 电量:", batteryLevel, "续航:", lightingTime, "完整数据:", formData); let recCnt = recArr.find(v => { - return v.key.replaceAll('/', '').toLowerCase() === f.device.detailPageUrl.replaceAll( - '/', '').toLowerCase(); + return v.key.replace(/\//g, '').toLowerCase() === f.device.detailPageUrl.replace( + /\//g, '').toLowerCase(); }); if (!recCnt) { if (batteryLevel <= 20) { @@ -487,9 +491,11 @@ class BleReceive { this.setBleFormData(formData, f); return formData; } catch (error) { - console.log('数据解析错误:', error); + console.log('7305数据解析错误:', error); + return null; } } + return null; } Receive_4877(receive,f,path,recArr){