修复一个Uniapp蓝牙的坑,App首页蓝牙状态功能添加
This commit is contained in:
@ -513,58 +513,71 @@ class BleHelper {
|
||||
return bytes.map(byte => byte.toString(16).padStart(2,
|
||||
'0'));
|
||||
}
|
||||
|
||||
let adapterStateTime=null;
|
||||
uni.onBluetoothAdapterStateChange((state) => {
|
||||
console.log('蓝牙状态发生变化:' + JSON.stringify(state));
|
||||
this.data.discovering = state.discovering;
|
||||
|
||||
if (this.data.available !== state.available) {
|
||||
this.data.available = state.available;
|
||||
clearTimeout(adapterStateTime);
|
||||
setTimeout(()=>{
|
||||
|
||||
if (this.data.available && this.data
|
||||
.isOpenBlue) { //蓝牙状态再次可用,重连所有设备
|
||||
|
||||
if (this.cfg.stateRecoveryCallback.length > 0) {
|
||||
this.cfg.stateRecoveryCallback.forEach(
|
||||
c => {
|
||||
try {
|
||||
c.callback();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"蓝牙适配器已恢复,但回调函数发生错误",
|
||||
error);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if(this.data.discovering === state.discovering && this.data.available===state.available){
|
||||
console.error("专业给Uniapp填坑,适配器状态错误");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!state.available) { //蓝牙状态不可用了,将所有设备标记为断开连接
|
||||
|
||||
this.data.LinkedList.filter((v) => {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
return true;
|
||||
});
|
||||
let keys = Object.keys(bleDeviceStates)
|
||||
keys.filter(v => {
|
||||
bleDeviceStates[v] = false;
|
||||
});
|
||||
|
||||
this.cfg.stateBreakCallback.forEach(f => {
|
||||
try {
|
||||
f.callback();
|
||||
} catch (error) {
|
||||
console.error("蓝牙状态不可用了,执行回调异常",
|
||||
error)
|
||||
console.log('蓝牙模块状态发生变化:' + JSON.stringify(state));
|
||||
this.data.discovering = state.discovering;
|
||||
|
||||
if (this.data.available !== state.available) {
|
||||
this.data.available = state.available;
|
||||
|
||||
if (this.data.available && this.data
|
||||
.isOpenBlue) { //蓝牙适配器再次可用
|
||||
|
||||
if (this.cfg.stateRecoveryCallback.length > 0) {//执行适配器恢复的回调
|
||||
console.log('蓝牙状态再次可用,执行恢复连接的回调');
|
||||
this.cfg.stateRecoveryCallback.forEach(
|
||||
c => {
|
||||
try {
|
||||
c.callback();
|
||||
console.log("执行恢复连接的回调成功");
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"蓝牙适配器已恢复,但回调函数发生错误",
|
||||
error);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.updateCache();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!state.available) { //蓝牙状态不可用了,将所有设备标记为断开连接
|
||||
console.log("蓝牙模块不可用了,将所有设备标记为断开连接");
|
||||
this.data.LinkedList.filter((v) => {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
|
||||
return true;
|
||||
});
|
||||
// let keys = Object.keys(bleDeviceStates)
|
||||
// keys.filter(v => {
|
||||
// bleDeviceStates[v] = false;
|
||||
// });
|
||||
|
||||
this.cfg.stateBreakCallback.forEach(f => {
|
||||
try {
|
||||
f.callback();
|
||||
console.log("执行断开连接的回调成功");
|
||||
} catch (error) {
|
||||
console.error("蓝牙状态不可用了,执行回调异常",
|
||||
error)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.updateCache();
|
||||
}
|
||||
},500);
|
||||
|
||||
});
|
||||
|
||||
|
||||
@ -594,8 +607,8 @@ class BleHelper {
|
||||
return false;
|
||||
});
|
||||
this.updateCache();
|
||||
if (f && f.device && f.device.id) {
|
||||
console.log("尝试5次恢复连接,", f
|
||||
if (f && f.device && f.device.id && this.data.available) {
|
||||
console.log("蓝牙状态可用,尝试5次恢复连接,", f
|
||||
.deviceId);
|
||||
this.LinkBlue(res.deviceId, f
|
||||
.writeServiceId, f
|
||||
@ -609,6 +622,7 @@ class BleHelper {
|
||||
(c) => {
|
||||
try {
|
||||
c.callback(res);
|
||||
console.log("执行蓝牙断开连接的回调成功");
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"执行蓝牙断开连接的回调出现异常,",
|
||||
@ -618,20 +632,7 @@ class BleHelper {
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log("蓝牙连接已恢复,", res);
|
||||
if (this.cfg.recoveryCallback.length >
|
||||
0) {
|
||||
this.cfg.recoveryCallback.forEach((
|
||||
c) => {
|
||||
try {
|
||||
c.callback(res);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"执行蓝牙恢复连接的回调出现异常,",
|
||||
error)
|
||||
}
|
||||
});
|
||||
}
|
||||
console.log("蓝牙连接已恢复",res);
|
||||
}
|
||||
|
||||
}, 0);
|
||||
@ -1501,6 +1502,20 @@ class BleHelper {
|
||||
this.updateCache();
|
||||
// console.log("LinkedList=", this.data
|
||||
// .LinkedList);
|
||||
|
||||
//执行连接成功的回调
|
||||
if (this.cfg.recoveryCallback.length >0) {
|
||||
this.cfg.recoveryCallback.forEach((
|
||||
c) => {
|
||||
try {
|
||||
c.callback({deviceId:deviceId,connected:true});
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"执行蓝牙恢复连接的回调出现异常,",
|
||||
error)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 处理 MTU 设置
|
||||
if (plus.os.name === 'Android') {
|
||||
@ -1830,7 +1845,7 @@ class BleHelper {
|
||||
Promise.race([timeOut(ms), promise]).then(resolve).catch((ex) => {
|
||||
// console.error("ex=", ex);
|
||||
if (ex.code == -1) {
|
||||
// console.error('专业给Uniapp填坑,发送消息永无回调');
|
||||
console.error('专业给Uniapp填坑,发送消息永无回调');
|
||||
resolve(ex);
|
||||
} else {
|
||||
reject(ex);
|
||||
|
||||
Reference in New Issue
Block a user