合并线上代码

This commit is contained in:
liub
2025-11-07 12:16:10 +08:00
parent 2493bb7113
commit 623a47466a
41 changed files with 5293 additions and 119 deletions

View File

@ -754,30 +754,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) {
@ -1033,7 +1038,7 @@ class BleHelper {
//订阅消息
subScribe(deviceId, state) {
// console.log("开始订阅消息", state);
console.log("开始订阅消息", deviceId, state);
return new Promise((resolve, reject) => {
setTimeout(() => {
@ -1154,13 +1159,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);
@ -1553,9 +1557,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); //已连接过的设备无需获取服务
}
@ -1703,8 +1717,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 = [];
// 处理不同类型的bufferArrayBuffer、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;
}
@ -1748,13 +1786,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);
}
@ -1792,10 +1831,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;