018A初步完成
This commit is contained in:
@ -651,7 +651,7 @@ class BleHelper {
|
||||
BleReceive() {
|
||||
uni.onBLECharacteristicValueChange((receive) => {
|
||||
//订阅消息
|
||||
// console.log("收到订阅消息", receive);
|
||||
// console.log("收到订阅消息", receive);
|
||||
let f = this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == receive.deviceId;
|
||||
})
|
||||
@ -768,6 +768,7 @@ class BleHelper {
|
||||
} catch (convertException) {
|
||||
if (str && (str.trim().startsWith('{') || str.trim().startsWith('['))) {
|
||||
console.error("JSON解析失败(可能是格式错误的数据)", convertException);
|
||||
console.error("错误的数据", str);
|
||||
}
|
||||
}
|
||||
|
||||
@ -810,6 +811,7 @@ class BleHelper {
|
||||
console.error(
|
||||
"订阅消息出现异常",
|
||||
err);
|
||||
console.error("异常数据:", str);
|
||||
}
|
||||
|
||||
}
|
||||
@ -976,7 +978,10 @@ class BleHelper {
|
||||
if (this.cfg.recoveryCallback.length > 0) {
|
||||
this.cfg.recoveryCallback.forEach(c => {
|
||||
try {
|
||||
c.callback({ deviceId: res.deviceId, connected: true });
|
||||
c.callback({
|
||||
deviceId: res.deviceId,
|
||||
connected: true
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("执行蓝牙恢复连接的回调异常", err);
|
||||
}
|
||||
@ -998,15 +1003,15 @@ class BleHelper {
|
||||
// console.log("发现新设备",item.name+" "+item.RSSI);
|
||||
// }
|
||||
|
||||
let f = serviceDic.find(v => {
|
||||
return item.advertisServiceUUIDs
|
||||
.includes(v.serviceId);
|
||||
});
|
||||
if (f) {
|
||||
let f = serviceDic.find(v => {
|
||||
return item.advertisServiceUUIDs
|
||||
.includes(v.serviceId);
|
||||
});
|
||||
if (f) {
|
||||
|
||||
// console.log("发现目标设备:", item);
|
||||
arr.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (arr.length === 0) {
|
||||
@ -1140,7 +1145,7 @@ class BleHelper {
|
||||
//停止搜索
|
||||
StopSearch() {
|
||||
if (this.data.platform == 'web' || !this.data.discovering) {
|
||||
console.error("web平台或当前没有搜索,直接返回",this.data.discovering);
|
||||
console.error("web平台或当前没有搜索,直接返回", this.data.discovering);
|
||||
return Promise.resolve();
|
||||
}
|
||||
let p1 = new Promise((resolve, reject) => {
|
||||
@ -1575,9 +1580,11 @@ class BleHelper {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
if (this.data.connectingDevices[deviceId]) {
|
||||
console.error("连接任务进行中,本次连接被阻断")
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
this.data.connectingDevices = true;
|
||||
|
||||
this.data.connectingDevices[deviceId] = true;
|
||||
|
||||
if (maxRetries === undefined) {
|
||||
maxRetries = 0; // 最大重试次数
|
||||
@ -1600,35 +1607,37 @@ class BleHelper {
|
||||
return false;
|
||||
});
|
||||
////console.log("findex=" + fIndex);
|
||||
|
||||
|
||||
var these = this;
|
||||
|
||||
|
||||
//连接设备
|
||||
var linkDevice = () => {
|
||||
// 添加重试次数限制
|
||||
|
||||
let retryCount = 0;
|
||||
|
||||
|
||||
const connect = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
if (fIndex > -1 && f?.Linked) {
|
||||
// console.log("当前已连接,跳过其他步骤");
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!this.data.available) {
|
||||
reject(this.getError({
|
||||
code: 10001
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
// console.log("正在连接" + deviceId);
|
||||
|
||||
console.log("正在连接" + deviceId);
|
||||
uni.createBLEConnection({
|
||||
deviceId: deviceId,
|
||||
timeout: 20000,
|
||||
timeout: 10000,
|
||||
success: (info) => {
|
||||
//释放连接锁
|
||||
|
||||
@ -1728,7 +1737,7 @@ class BleHelper {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.OpenBlue().then((res) => {
|
||||
////console.log("2222222");
|
||||
console.log("2222222");
|
||||
return linkDevice(deviceId);
|
||||
}).then((res) => {
|
||||
|
||||
@ -1772,6 +1781,7 @@ class BleHelper {
|
||||
}, 500);
|
||||
}).catch((ex) => {
|
||||
console.error("出现异常", ex);
|
||||
delete this.data.connectingDevices[deviceId];
|
||||
reject(ex);
|
||||
});
|
||||
});
|
||||
@ -1869,6 +1879,7 @@ class BleHelper {
|
||||
}
|
||||
//向蓝牙设备发送一个字符串的ASCII码
|
||||
sendString(deviceid, str, writeServiceId, wirteCharactId, ms, iosIsChuck, chunkSize) {
|
||||
debugger;
|
||||
if (str && typeof(str) == 'object') {
|
||||
str = JSON.stringify(str);
|
||||
}
|
||||
@ -1913,6 +1924,7 @@ class BleHelper {
|
||||
}
|
||||
//向蓝牙设备发送数据,如果没连接将自动连接后再发
|
||||
sendData(deviceid, buffer, writeServiceId, wirteCharactId, ms, iosIsChuck, chunkSize) {
|
||||
debugger;
|
||||
if (this.data.platform == 'web') {
|
||||
return Promise.resolve("h5平台默认成功");
|
||||
}
|
||||
@ -1995,7 +2007,9 @@ class BleHelper {
|
||||
},
|
||||
fail: (ex) => {
|
||||
ex = this.getError(ex);
|
||||
console.error("✗ 蓝牙指令发送失败 - deviceId:",device.deviceId, "错误:", ex);
|
||||
console.error(
|
||||
"✗ 蓝牙指令发送失败 - deviceId:",
|
||||
device.deviceId, "错误:", ex);
|
||||
|
||||
_err(ex);
|
||||
}
|
||||
@ -2057,7 +2071,7 @@ class BleHelper {
|
||||
);
|
||||
},
|
||||
20
|
||||
); // 20ms延迟,可根据需要调整
|
||||
); // 20ms延迟,可根据需要调整
|
||||
});
|
||||
}
|
||||
_resolve(result);
|
||||
@ -2078,7 +2092,7 @@ class BleHelper {
|
||||
);
|
||||
},
|
||||
20
|
||||
); // 20ms延迟,可根据需要调整
|
||||
); // 20ms延迟,可根据需要调整
|
||||
});
|
||||
}
|
||||
_resolve(ex);
|
||||
|
||||
Reference in New Issue
Block a user