1
0
forked from dyf/APP

修改蓝牙发现逻辑自动连接过的不再被发现的问题,上架驳回问题处理

This commit is contained in:
liub
2025-09-26 13:50:29 +08:00
parent 21e468c79f
commit 3ac113de3f
6 changed files with 104 additions and 47 deletions

View File

@ -17,6 +17,7 @@ const serviceDic = [ //合作供应商的蓝牙主服务
class BleHelper {
constructor() {
this.StorageKey = "linkedDevices";
this.dropKey = "DeletedDevices";
recei = receivTool.getBleReceive();
this.init();
@ -24,23 +25,26 @@ class BleHelper {
init() {
let key = this.StorageKey;
var store = uni.getStorageInfoSync();
var f = store.keys.find(function(v) {
return v == key;
});
var f = store.keys.includes(key);
var linkedDevices = [];
if (f) {
linkedDevices = uni.getStorageSync(key);
}
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
console.log("111111", linkedDevices);
linkedDevices=linkedDevices.filter((v) => {
linkedDevices = linkedDevices.filter((v) => {
v.Linked = false;
v.notifyState = false;
return v.device && v.device.id;
return true;
});
}
var deletedEqs = [];
f = store.keys.includes(this.dropKey);
if (f) {
deletedEqs = uni.getStorageSync(this.dropKey);
}
setTimeout(() => {
this.OpenBlue().then(() => {
this.linkAllDevices();
@ -54,6 +58,7 @@ class BleHelper {
searchList: [], //已搜索到的设备列表,
isSubscribe: false, //是否开启了订阅
LinkedList: linkedDevices, //已连接的设备列表
deletedEqs: deletedEqs
}
this.cfg = {
onDeviceFound: [], //发现新设备的事件
@ -124,25 +129,47 @@ class BleHelper {
}
//从缓存中删除某个设备bleId蓝牙iddeviceId数据库中的设备id
DropDevice(bleId, deviceId) {
let flag = false;
let delItem=null;
if (!this.data.deletedEqs || !this.data.deletedEqs.length) {
this.data.deletedEqs = [];
}
for (var i = 0; i < this.data.LinkedList.length; i++) {
let item = this.data.LinkedList[i];
if (bleId) {
if (item.deviceId == bleId) {
if (item.device.id == bleId) {
console.log("找到要删除的设备", item);
this.data.LinkedList.splice(i, 1);
this.disconnectDevice(item.deviceId);
flag = true;
delItem=item;
break;
}
} else {
if (item.device && item.device.id) {
if (deviceId && item.deviceId == deviceId) {
console.log("找到要删除的设备1,", item)
this.data.LinkedList.splice(i, 1);
this.disconnectDevice(item.deviceId);
flag = true;
delItem=item;
break;
}
}
}
if (flag) {
this.data.deletedEqs=this.data.deletedEqs.filter(v => {
return v.deviceId != delItem.deviceId;
});
this.data.deletedEqs.push(delItem);
this.updateCache();
}
@ -152,24 +179,25 @@ class BleHelper {
updateCache() {
// console.log("this.StorageKey=", this.StorageKey)
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
uni.setStorageSync(this.dropKey, this.data.deletedEqs);
}
linkAllDevices() {
// console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
if (this.data.LinkedList && this.data.LinkedList.length > 0) {
let flag=false;
let flag = false;
for (var i = 0; i < this.data.LinkedList.length; i++) {
let device = this.data.LinkedList[i];
// console.log("自动连接绑定过的设备:" + device.deviceId);
if (device.macAddress && device.device && device.device.id) {
this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
.notifyCharactId);
flag=true;
flag = true;
}
}
if(!flag){
if (!flag) {
this.updateCache();
}
} else {
@ -449,7 +477,7 @@ class BleHelper {
this.data.discovering = state.discovering;
if (this.data.available && this.data
.isOpenBlue) { //蓝牙状态再次可用,重连所有设备
this.linkAllDevices();
// this.linkAllDevices();
}
}
@ -1346,7 +1374,7 @@ class BleHelper {
uni.closeBLEConnection({
deviceId: id,
success: (res) => {
////console.log("蓝牙连接已断开:" + id);
console.log("蓝牙连接已断开:" + id);
this.subScribe(id, false);
resolve();
},