diff --git a/pages/7305/BJQ7305.vue b/pages/7305/BJQ7305.vue index 9ff3fa7..320f69d 100644 --- a/pages/7305/BJQ7305.vue +++ b/pages/7305/BJQ7305.vue @@ -240,7 +240,7 @@ mode: '', bleStatu: '' }, - inteval: 200, + inteval: 500, device: { id: "", deviceName: "", @@ -479,6 +479,12 @@ 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({ @@ -561,92 +567,85 @@ // 分包发送图片数据 var sendImagePackets = function(packetData) { return new Promise((resolve, reject) => { - - // 总数据包数 - const totalPackets = 3; - let currentPacket = 1; - - - - // 发送单个数据包 - const sendNextPacket = () => { - if (currentPacket > totalPackets) { - setTimeout(() => { - hideLoading(these); - these.Status.BottomMenu.show = false; - - these.showPop({ - message: "上传成功", - iconUrl: "/static/images/6155/DeviceDetail/uploadSuccess.png", - borderColor: '#BBE600', - buttonBgColor: '#BBE600' - }); - - resolve(); - }, 0) - - return; + try { + // 1. 构建一个包含完整头、数据和尾的完整数据包 + const header = [0xFA, 0x09, 0x04, 0x00]; + const footer = [0xFF]; + const imageData = packetData.map(hex => parseInt(hex, 16)); + + // 确保图像数据为1024字节,不足则补0 + while (imageData.length < 1024) { + imageData.push(0x00); } + + const fullPacket = new Uint8Array([...header, ...imageData.slice(0, 1024), ...footer]); + const fullBuffer = fullPacket.buffer; + // 2. 将完整数据包切片成20字节的小块进行发送 + const chunkSize = 20; + const numChunks = Math.ceil(fullBuffer.byteLength / chunkSize); + let chunkIndex = 0; - - let start = 0; - let bufferSize = 343; //总共1029字节3包,正好每包343字节 - //FA 09 04 00 1024字节 FF - var buffer = new ArrayBuffer(bufferSize); - var dataView = new DataView(buffer); - if (currentPacket == 1) { - dataView.setUint8(0, 0xFA); // 帧头 - dataView.setUint8(1, 0x09); // 帧头 - dataView.setUint8(2, 0x04); // 帧头 - dataView.setUint8(3, 0x00); // 帧头 - - - for (let i = 0; i < packetData.length; i++) { - dataView.setUint8(4 + i, parseInt(packetData[i], 16)); + const sendNextChunk = () => { + if (chunkIndex >= numChunks) { + setTimeout(() => { + hideLoading(these); + these.Status.BottomMenu.show = false; + these.showPop({ + message: "上传成功", + iconUrl: "/static/images/6155/DeviceDetail/uploadSuccess.png", + borderColor: '#BBE600', + buttonBgColor: '#BBE600' + }); + resolve(); + }, 0); + return; } - } - if (currentPacket == totalPackets) { - dataView.setUint8(bufferSize - 1, 0xFF); - } - //发送数据包 - ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId, - 30) - .then(() => { + const start = chunkIndex * chunkSize; + const end = Math.min(start + chunkSize, fullBuffer.byteLength); + const chunk = fullBuffer.slice(start, end); + + const hexArray = Array.from(new Uint8Array(chunk)).map(b => b.toString(16).padStart(2, '0')); + console.log(`发送图片数据块 ${chunkIndex + 1}/${numChunks}:`, hexArray.join(' ')); - - updateLoading(these, { - text: "正在发送" + currentPacket + "/" + totalPackets - - }) - currentPacket++; - - setTimeout(sendNextPacket, 100); - }).catch(err => { - if (err.code == 10007) { - setTimeout(sendNextPacket, 100); - return; - } - - console.log("发送数据包失败了", err); - - these.Status.BottomMenu.show = false; - these.showPop({ - message: "发送数据包失败了" + err.msg, - iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png", - borderColor: "#e034344d", - buttonBgColor: "#E03434", - }); - - hideLoading(these); - reject(err); + updateLoading(these, { + text: "正在发送 " + (chunkIndex + 1) + "/" + numChunks }); + + ble.sendData(f.deviceId, chunk, f.writeServiceId, f.wirteCharactId, 100) + .then(() => { + chunkIndex++; + setTimeout(sendNextChunk, 30); // 每个小包之间延时30ms + }) + .catch(err => { + if (err.code == 10007 || err.code == -1) { + setTimeout(sendNextChunk, 30); + return; + } + + console.log("发送数据包失败了", err); + + these.Status.BottomMenu.show = false; + these.showPop({ + message: "发送数据包失败了: " + err.msg, + iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png", + borderColor: "#e034344d", + buttonBgColor: "#E03434", + }); + + hideLoading(these); + reject(err); + }); + }; + + sendNextChunk(); + + } catch (ex) { + console.error("构建图片数据包失败", ex); + hideLoading(these); + reject(ex); } - // 开始发送数据 - sendNextPacket(); - - }); } @@ -1050,77 +1049,60 @@ var promise = new Promise((resolve, reject) => { try { + // 1. 构建一个包含完整包头、数据和包尾的261字节数据包 + const bufferSize = 261; + let fullBuffer = new ArrayBuffer(bufferSize); + let dataView = new DataView(fullBuffer); - let packetSize = rgbdata.length; //每包均分的数量 - let mode = rgbdata.length % packetSize; //最后一包的数量 - let cnt = - 1; // parseInt(rgbdata.length / packetSize) + (mode > 0 ? 1 :0); //总包数量 - let curr = 1; //当前包序号 + dataView.setUint8(0, 0xFA); + dataView.setUint8(1, type); + dataView.setUint8(2, 0x01); + dataView.setUint8(3, 0x00); - let sendNext = () => { + for (let i = 0; i < rgbdata.length; i++) { + dataView.setUint8(4 + i, rgbdata[i]); + } + // 用0填充剩余部分,直到最后一个字节 + for (let i = 4 + rgbdata.length; i < bufferSize - 1; i++) { + dataView.setUint8(i, 0x00); + } - if (curr > cnt) { + dataView.setUint8(bufferSize - 1, 0xFF); + + // 2. 将完整数据包切片成20字节的小块进行发送 + const chunkSize = 20; + const numChunks = Math.ceil(fullBuffer.byteLength / chunkSize); + let chunkIndex = 0; + + let sendNextChunk = () => { + if (chunkIndex >= numChunks) { resolve(); return; } - let bufferSize = 261; + const start = chunkIndex * chunkSize; + const end = Math.min(start + chunkSize, fullBuffer.byteLength); + const chunk = fullBuffer.slice(start, end); + + const hexArray = Array.from(new Uint8Array(chunk)).map(b => b.toString(16).padStart(2, '0')); + console.log(`发送数据块 ${chunkIndex + 1}/${numChunks}:`, hexArray.join(' ')); - console.log("bufferSize=", bufferSize) - - let buffer = new ArrayBuffer(bufferSize); - let dataView = new DataView(buffer); - let startIndex = (curr - 1) * packetSize; - - let endIndex = Math.min(startIndex + packetSize, rgbdata - .length); - if (startIndex > endIndex) { - - return; - } - - let packetData = rgbdata.slice(startIndex, - endIndex); //取一片数据发送 - console.log("packetData.length=", packetData.length); - let start = 0; - if (curr == 1) { - dataView.setUint8(0, 0xFA); - dataView.setUint8(1, type); - dataView.setUint8(2, 0x01); - dataView.setUint8(3, 0x00); - - - start = 4; - } - - for (let i = 0; i < packetData.length; i++) { - dataView.setUint8(start + i, packetData[i]); - } - - dataView.setUint8(bufferSize - 1, 0xFF); - - let inteval = parseInt(this.inteval ? this.inteval : 50); - console.log("inteval=", inteval) - ble.sendData(f.deviceId, buffer, f.writeServiceId, f - .wirteCharactId, 100).then(() => { - - curr++; - setTimeout(sendNext, inteval); + ble.sendData(f.deviceId, chunk, f.writeServiceId, f.wirteCharactId, 100).then(() => { + chunkIndex++; + setTimeout(sendNextChunk, 30); // 每个小包之间延时30ms }).catch(err => { - if (err.code == '10007') { - setTimeout(sendNext, inteval); + setTimeout(sendNextChunk, 30); } else { reject(err); } }); - } - sendNext(); + sendNextChunk(); + } catch (ex) { console.log("ex=", ex); - reject(ex); } }); @@ -1164,7 +1146,7 @@ // console.log("222222"); } catch (ex) { flag = false; - console.log("33333"); + console.log("发送数据包出现异常", ex); break; } } diff --git a/utils/BleHelper.js b/utils/BleHelper.js index d218e38..06ef47c 100644 --- a/utils/BleHelper.js +++ b/utils/BleHelper.js @@ -1222,7 +1222,7 @@ class BleHelper { } setTimeout(function() { startgetService(id); - }, 100); + }, 1000); } }, fail: (ex) => { @@ -1410,11 +1410,16 @@ class BleHelper { // .LinkedList); // 处理 MTU 设置 - if (plus.os.name === 'Android') { - this.setMtu(deviceId); - } + if (plus.os.name === 'Android') { + this.setMtu(deviceId).then(() => { + resolve(true); + }).catch(() => { + resolve(true); //mtu设置失败也算成功 + }) + } else { resolve(true); - + } + }).catch((ex) => { reject(this.getError(ex)); }); diff --git a/utils/BleReceive.js b/utils/BleReceive.js index 8364126..940733e 100644 --- a/utils/BleReceive.js +++ b/utils/BleReceive.js @@ -50,7 +50,7 @@ class BleReceive { ReceiveData(receive,f,path,recArr) { if(f && f.macAddress && f.device && f.device.id){ let data={}; - if(f.device.detailPageUrl=='/pages/6155/deviceDetail'){ + if(f.device.detailPageUrl=='/pages/6155/deviceDetail' || f.device.detailPageUrl=='/pages/7305/BJQ7305'){ // console.log("该设备是6155"); data= this.Receive_6155(receive,f,path,recArr); }