解决安卓数据包问题,拆成小包发送,增加电量,续航时间,充电检测
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user