蓝牙添加连接、断开、可用、不可用控制
This commit is contained in:
@ -17,13 +17,13 @@ const serviceDic = [ //合作供应商的蓝牙主服务
|
||||
class BleHelper {
|
||||
constructor() {
|
||||
this.StorageKey = "linkedDevices";
|
||||
|
||||
|
||||
recei = receivTool.getBleReceive();
|
||||
|
||||
this.init();
|
||||
}
|
||||
init() {
|
||||
|
||||
|
||||
var store = uni.getStorageInfoSync();
|
||||
var f = store.keys.includes(this.StorageKey);
|
||||
var linkedDevices = [];
|
||||
@ -39,7 +39,7 @@ class BleHelper {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
this.OpenBlue().then(() => {
|
||||
@ -54,14 +54,15 @@ class BleHelper {
|
||||
searchList: [], //已搜索到的设备列表,
|
||||
isSubscribe: false, //是否开启了订阅
|
||||
LinkedList: linkedDevices //已连接的设备列表
|
||||
|
||||
|
||||
}
|
||||
this.cfg = {
|
||||
onDeviceFound: [], //发现新设备的事件
|
||||
receivDataCallback: [], //接收到数据的事件
|
||||
bleDisposeCallback: [], //蓝牙断开连接的事件
|
||||
recoveryCallback: [], //蓝牙连接恢复的事件
|
||||
stateRecoveryCallback: [] //蓝牙适配器恢复可用事件
|
||||
stateRecoveryCallback: [], //蓝牙适配器恢复可用事件
|
||||
stateBreakCallback: [] //蓝牙适配器不可用事件
|
||||
}
|
||||
// this.addReceiveCallback((a, b, c) => {
|
||||
// recei.ReceiveData(a, b, c);
|
||||
@ -128,13 +129,13 @@ class BleHelper {
|
||||
}
|
||||
//从缓存中删除某个设备,bleId蓝牙id,deviceId数据库中的设备id
|
||||
DropDevice(bleId, deviceId) {
|
||||
|
||||
|
||||
let flag = false;
|
||||
for (var i = 0; i < this.data.LinkedList.length; i++) {
|
||||
let item = this.data.LinkedList[i];
|
||||
if (bleId && item.device) {
|
||||
if (item.device.id == bleId) {
|
||||
// console.log("找到要删除的设备", item);
|
||||
if (deviceId && item.device) {
|
||||
if (item.device.id == deviceId) {
|
||||
console.log("找到要删除的设备", item);
|
||||
this.data.LinkedList.splice(i, 1);
|
||||
this.disconnectDevice(item.deviceId);
|
||||
flag = true;
|
||||
@ -142,16 +143,16 @@ class BleHelper {
|
||||
}
|
||||
|
||||
} else {
|
||||
if (deviceId && item.deviceId == deviceId) {
|
||||
// console.log("找到要删除的设备1,", item)
|
||||
if (bleId && item.deviceId == bleId) {
|
||||
console.log("找到要删除的设备1,", item)
|
||||
this.data.LinkedList.splice(i, 1);
|
||||
this.disconnectDevice(item.deviceId);
|
||||
flag = true;
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flag) {
|
||||
if (flag) {
|
||||
this.updateCache();
|
||||
}
|
||||
|
||||
@ -159,9 +160,9 @@ class BleHelper {
|
||||
|
||||
//更新缓存
|
||||
updateCache() {
|
||||
|
||||
|
||||
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
|
||||
|
||||
|
||||
}
|
||||
//连接所有已连接过的设备
|
||||
linkAllDevices() {
|
||||
@ -248,60 +249,80 @@ class BleHelper {
|
||||
}
|
||||
|
||||
|
||||
//设置蓝牙适配器恢复可用的回调
|
||||
addstateRecoveryCallback(callback, currKey) {
|
||||
addCallback(callback, currKey, 'stateRecoveryCallback');
|
||||
}
|
||||
|
||||
//移除蓝牙适配器恢复的回调
|
||||
removestateRecoveryCallback(currKey) {
|
||||
removeCallback(currKey, 'stateRecoveryCallback');
|
||||
}
|
||||
|
||||
|
||||
|
||||
//设置蓝牙恢复连接的回调
|
||||
addRecoveryCallback(callback, currKey) {
|
||||
addCallback(callback, currKey, 'recoveryCallback');
|
||||
this.addCallback(callback, currKey, 'recoveryCallback');
|
||||
}
|
||||
|
||||
//移除蓝牙恢复连接的回调
|
||||
removeRecoveryCallback(currKey) {
|
||||
removeCallback(currKey, 'recoveryCallback');
|
||||
this.removeCallback(currKey, 'recoveryCallback');
|
||||
}
|
||||
|
||||
//设置蓝牙断开连接的回调
|
||||
addDisposeCallback(callback, currKey) {
|
||||
addCallback(callback, currKey, 'bleDisposeCallback');
|
||||
this.addCallback(callback, currKey, 'bleDisposeCallback');
|
||||
}
|
||||
|
||||
//移除蓝牙断开连接的回调
|
||||
removeDisposeCallback(currKey) {
|
||||
removeCallback(currKey, 'bleDisposeCallback');
|
||||
this.removeCallback(currKey, 'bleDisposeCallback');
|
||||
}
|
||||
|
||||
|
||||
//设置发现新设备的回调
|
||||
addDeviceFound(callback, currKey) {
|
||||
addCallback(callback, currKey, 'onDeviceFound');
|
||||
this.addCallback(callback, currKey, 'onDeviceFound');
|
||||
}
|
||||
|
||||
//移除发现新设备的回调
|
||||
removeDeviceFound(currKey) {
|
||||
removeCallback(currKey, 'onDeviceFound');
|
||||
this.removeCallback(currKey, 'onDeviceFound');
|
||||
}
|
||||
|
||||
//添加接收到数据的回调
|
||||
addReceiveCallback(callback, currKey) {
|
||||
addCallback(callback, currKey, 'receivDataCallback');
|
||||
this.addCallback(callback, currKey, 'receivDataCallback');
|
||||
}
|
||||
|
||||
|
||||
|
||||
//设置接收到数据的回调
|
||||
//移除接收到数据的回调
|
||||
removeReceiveCallback(currKey) {
|
||||
removeCallback(currKey, 'receivDataCallback');
|
||||
this.removeCallback(currKey, 'receivDataCallback');
|
||||
}
|
||||
|
||||
//添加蓝牙不可用的回调
|
||||
addStateBreakCallback(callback, currKey) {
|
||||
this.addCallback(callback, currKey, 'stateBreakCallback');
|
||||
}
|
||||
|
||||
//移除蓝牙不可用的回调
|
||||
removeStateBreakCallback(currKey) {
|
||||
this.removeCallback(currKey, 'stateBreakCallback');
|
||||
}
|
||||
|
||||
|
||||
//设置蓝牙适配器恢复可用的回调
|
||||
addStateRecoveryCallback(callback, currKey) {
|
||||
this.addCallback(callback, currKey, 'stateRecoveryCallback');
|
||||
}
|
||||
|
||||
//移除蓝牙适配器恢复的回调
|
||||
removeStateRecoveryCallback(currKey) {
|
||||
this.removeCallback(currKey, 'stateRecoveryCallback');
|
||||
}
|
||||
|
||||
//清除所有事件回调
|
||||
removeAllCallback(currKey) {
|
||||
this.removeDeviceFound(currKey)
|
||||
this.removeDisposeCallback(currKey);
|
||||
this.removeReceiveCallback(currKey);
|
||||
this.removeRecoveryCallback(currKey);
|
||||
this.removeStateRecoveryCallback(currKey);
|
||||
this.removeStateBreakCallback(currKey);
|
||||
}
|
||||
|
||||
|
||||
getError(ex) {
|
||||
@ -461,6 +482,9 @@ class BleHelper {
|
||||
console.log("开始订阅各类变化消息");
|
||||
this.data.isSubscribe = true;
|
||||
|
||||
// 保存每个设备的连接状态
|
||||
let bleDeviceStates = {};
|
||||
|
||||
|
||||
var bytesToHexString = (bytes) => {
|
||||
return bytes.map(byte => byte.toString(16).padStart(2,
|
||||
@ -468,21 +492,24 @@ class BleHelper {
|
||||
}
|
||||
|
||||
uni.onBluetoothAdapterStateChange((state) => {
|
||||
console.log('蓝牙状态发生变化:' + JSON.stringify(state));
|
||||
// console.log('蓝牙状态发生变化:' + JSON.stringify(state));
|
||||
if (this.data.available !== state.available) {
|
||||
this.data.available = state.available;
|
||||
this.data.discovering = state.discovering;
|
||||
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.cfg.stateRecoveryCallback.length > 0) {
|
||||
this.cfg.stateRecoveryCallback.forEach(
|
||||
c => {
|
||||
try {
|
||||
c.callback();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"蓝牙适配器已恢复,但回调函数发生错误",
|
||||
error);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@ -490,60 +517,99 @@ class BleHelper {
|
||||
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.updateCache();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
uni.onBLEConnectionStateChange((res) => {
|
||||
|
||||
if (!res.connected) {
|
||||
|
||||
console.error("蓝牙已断开", res);
|
||||
let f = this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == res.deviceId) {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
this.updateCache();
|
||||
if (f.device && f.device.id) {
|
||||
console.log("尝试5次恢复连接,", f.deviceId);
|
||||
this.LinkBlue(res.deviceId, f.writeServiceId, f
|
||||
.wirteCharactId, f.notifyCharactId, 5);
|
||||
}
|
||||
|
||||
if (this.cfg.bleDisposeCallback.length > 0) {
|
||||
this.cfg.bleDisposeCallback.forEach((c)=>{
|
||||
try {
|
||||
c.callback();
|
||||
} catch (error) {
|
||||
console.error("执行蓝牙断开连接的回调出现异常,", error)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log("蓝牙连接已恢复,", res);
|
||||
if (this.cfg.recoveryCallback.length > 0) {
|
||||
this.cfg.recoveryCallback.forEach((c)=>{
|
||||
try {
|
||||
c.callback();
|
||||
} catch (error) {
|
||||
console.error("执行蓝牙恢复连接的回调出现异常,", error)
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
console.log("蓝牙连接状态变化了", res);
|
||||
// 检查状态是否真的发生了变化
|
||||
if (bleDeviceStates[res.deviceId] === res.connected) {
|
||||
console.error('专业给Uniapp填坑,连接状态');
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新状态记录
|
||||
bleDeviceStates[res.deviceId] = res.connected;
|
||||
|
||||
setTimeout(() => {
|
||||
if (!res.connected) {
|
||||
console.error("蓝牙已断开", res);
|
||||
let f = this.data.LinkedList.find((
|
||||
v) => {
|
||||
if (v.deviceId == res
|
||||
.deviceId) {
|
||||
v.Linked = false;
|
||||
v.notifyState = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
this.updateCache();
|
||||
if (f && f.device && f.device.id) {
|
||||
console.log("尝试5次恢复连接,", f
|
||||
.deviceId);
|
||||
this.LinkBlue(res.deviceId, f
|
||||
.writeServiceId, f
|
||||
.wirteCharactId, f
|
||||
.notifyCharactId, 5);
|
||||
}
|
||||
|
||||
if (this.cfg.bleDisposeCallback.length >
|
||||
0) {
|
||||
this.cfg.bleDisposeCallback.forEach(
|
||||
(c) => {
|
||||
try {
|
||||
c.callback(res);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"执行蓝牙断开连接的回调出现异常,",
|
||||
error)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log("蓝牙连接已恢复,", res);
|
||||
if (this.cfg.recoveryCallback.length >
|
||||
0) {
|
||||
this.cfg.recoveryCallback.forEach((
|
||||
c) => {
|
||||
try {
|
||||
c.callback(res);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"执行蓝牙恢复连接的回调出现异常,",
|
||||
error)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}, 0);
|
||||
});
|
||||
|
||||
uni.onBluetoothDeviceFound((res) => {
|
||||
@ -556,11 +622,14 @@ class BleHelper {
|
||||
.includes(v.serviceId);
|
||||
});
|
||||
if (f) {
|
||||
|
||||
console.log("发现新设备:", item);
|
||||
arr.push(item);
|
||||
}
|
||||
|
||||
}
|
||||
if (arr.length === 0) {
|
||||
// console.error("发现了设备,但不是想要的设备", res);
|
||||
return;
|
||||
}
|
||||
res.devices = arr;
|
||||
@ -569,7 +638,13 @@ class BleHelper {
|
||||
if (this.cfg.onDeviceFound) {
|
||||
if (this.cfg.onDeviceFound.length > 0) {
|
||||
this.cfg.onDeviceFound.forEach((found) => {
|
||||
found.callback(res);
|
||||
try {
|
||||
found.callback(res);
|
||||
} catch (error) {
|
||||
console.error("处理发现设备的回调出现异常,",
|
||||
error);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@ -745,12 +820,12 @@ class BleHelper {
|
||||
uni.closeBluetoothAdapter({
|
||||
success: () => {
|
||||
// console.log("蓝牙模块已关闭");
|
||||
this.data.isOpenBlue = false;
|
||||
|
||||
},
|
||||
fail: (ex) => {
|
||||
console.error('无法关闭蓝牙模块:', ex);
|
||||
ex = this.getError(ex);
|
||||
////console.log(msg);
|
||||
|
||||
},
|
||||
complete: () => {
|
||||
resolve();
|
||||
@ -842,13 +917,14 @@ class BleHelper {
|
||||
err({
|
||||
code: -1
|
||||
});
|
||||
}, 50);
|
||||
}, 100);
|
||||
});
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
Promise.race([p1, p2]).then(resolve).catch(ex => {
|
||||
if (ex.code == -1) {
|
||||
console.error('专业给Uniapp填坑,停止搜索');
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
@ -960,6 +1036,7 @@ class BleHelper {
|
||||
Promise.race([p1, p2]).then(succ).catch(ex => {
|
||||
// console.error("异常了:",ex);
|
||||
if (ex.code == -1) {
|
||||
console.error('专业给Uniapp填坑,订阅成功');
|
||||
succ();
|
||||
return;
|
||||
}
|
||||
@ -1342,7 +1419,7 @@ class BleHelper {
|
||||
},
|
||||
fail: () => {
|
||||
console.error(
|
||||
"mtu设置失败");
|
||||
"mtu设置失败");
|
||||
resolve(
|
||||
true
|
||||
); // MTU设置失败不影响连接成功
|
||||
@ -1358,7 +1435,7 @@ class BleHelper {
|
||||
fail: (ex) => {
|
||||
ex = this.getError(ex);
|
||||
console.error("蓝牙" + deviceId + "连接失败" + JSON.stringify(
|
||||
ex));
|
||||
ex));
|
||||
|
||||
// 连接超时后自动重试
|
||||
if (ex.code === 10012 && retryCount < maxRetries) {
|
||||
@ -1441,12 +1518,12 @@ class BleHelper {
|
||||
uni.closeBLEConnection({
|
||||
deviceId: id,
|
||||
success: (res) => {
|
||||
console.log("蓝牙连接已断开:" + id);
|
||||
console.log("用户主动断开了蓝牙:" + id);
|
||||
this.subScribe(id, false);
|
||||
resolve();
|
||||
},
|
||||
fail: (ex) => {
|
||||
|
||||
console.error("无法断开蓝牙连接", ex);
|
||||
reject(this.getError(ex));
|
||||
}
|
||||
});
|
||||
@ -1570,6 +1647,7 @@ class BleHelper {
|
||||
Promise.race([timeOut(ms), promise]).then(resolve).catch((ex) => {
|
||||
// console.error("ex=", ex);
|
||||
if (ex.code == -1) {
|
||||
console.error('专业给Uniapp填坑,发送成功');
|
||||
resolve(ex);
|
||||
} else {
|
||||
reject(ex);
|
||||
|
||||
Reference in New Issue
Block a user