解决安卓数据包问题,拆成小包发送,增加电量,续航时间,充电检测

This commit is contained in:
微微一笑
2025-10-23 15:28:47 +08:00
parent 2d3b896243
commit 8487a4303a
3 changed files with 126 additions and 139 deletions

View File

@ -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,71 +567,60 @@
// 分包发送图片数据
var sendImagePackets = function(packetData) {
return new Promise((resolve, reject) => {
try {
// 1. 构建一个包含完整头、数据和尾的完整数据包
const header = [0xFA, 0x09, 0x04, 0x00];
const footer = [0xFF];
const imageData = packetData.map(hex => parseInt(hex, 16));
// 总数据包数
const totalPackets = 3;
let currentPacket = 1;
// 确保图像数据为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;
// 发送单个数据包
const sendNextPacket = () => {
if (currentPacket > totalPackets) {
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)
}, 0);
return;
}
const start = chunkIndex * chunkSize;
const end = Math.min(start + chunkSize, fullBuffer.byteLength);
const chunk = fullBuffer.slice(start, end);
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));
}
}
if (currentPacket == totalPackets) {
dataView.setUint8(bufferSize - 1, 0xFF);
}
//发送数据包
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId,
30)
.then(() => {
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
text: "正在发送 " + (chunkIndex + 1) + "/" + numChunks
});
ble.sendData(f.deviceId, chunk, f.writeServiceId, f.wirteCharactId, 100)
.then(() => {
chunkIndex++;
setTimeout(sendNextChunk, 30); // 每个小包之间延时30ms
})
currentPacket++;
setTimeout(sendNextPacket, 100);
}).catch(err => {
if (err.code == 10007) {
setTimeout(sendNextPacket, 100);
.catch(err => {
if (err.code == 10007 || err.code == -1) {
setTimeout(sendNextChunk, 30);
return;
}
@ -633,7 +628,7 @@
these.Status.BottomMenu.show = false;
these.showPop({
message: "发送数据包失败了" + err.msg,
message: "发送数据包失败了: " + err.msg,
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
borderColor: "#e034344d",
buttonBgColor: "#E03434",
@ -642,11 +637,15 @@
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; //当前包序号
let sendNext = () => {
if (curr > cnt) {
resolve();
return;
}
let bufferSize = 261;
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 < rgbdata.length; i++) {
dataView.setUint8(4 + i, rgbdata[i]);
}
for (let i = 0; i < packetData.length; i++) {
dataView.setUint8(start + i, packetData[i]);
// 用0填充剩余部分直到最后一个字节
for (let i = 4 + rgbdata.length; i < bufferSize - 1; i++) {
dataView.setUint8(i, 0x00);
}
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(() => {
// 2. 将完整数据包切片成20字节的小块进行发送
const chunkSize = 20;
const numChunks = Math.ceil(fullBuffer.byteLength / chunkSize);
let chunkIndex = 0;
curr++;
setTimeout(sendNext, inteval);
let sendNextChunk = () => {
if (chunkIndex >= numChunks) {
resolve();
return;
}
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(' '));
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;
}
}

View File

@ -1222,7 +1222,7 @@ class BleHelper {
}
setTimeout(function() {
startgetService(id);
}, 100);
}, 1000);
}
},
fail: (ex) => {
@ -1411,9 +1411,14 @@ class BleHelper {
// 处理 MTU 设置
if (plus.os.name === 'Android') {
this.setMtu(deviceId);
}
this.setMtu(deviceId).then(() => {
resolve(true);
}).catch(() => {
resolve(true); //mtu设置失败也算成功
})
} else {
resolve(true);
}
}).catch((ex) => {
reject(this.getError(ex));

View File

@ -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);
}