优化蓝牙搜索页面的动画闪烁感,优化从设备页面去匹配蓝牙时给出目标设备的提示

This commit is contained in:
liub
2025-10-13 10:57:03 +08:00
parent e3a2624509
commit 8689781e17
7 changed files with 233 additions and 171 deletions

View File

@ -31,7 +31,7 @@ class BleHelper {
linkedDevices = uni.getStorageSync(key);
}
if (linkedDevices && linkedDevices.length && linkedDevices.length > 0) {
console.log("111111", linkedDevices);
// console.log("111111", linkedDevices);
linkedDevices = linkedDevices.filter((v) => {
v.Linked = false;
v.notifyState = false;
@ -138,9 +138,9 @@ class BleHelper {
for (var i = 0; i < this.data.LinkedList.length; i++) {
let item = this.data.LinkedList[i];
if (bleId) {
if (bleId && item.device) {
if (item.device.id == bleId) {
console.log("找到要删除的设备", item);
// console.log("找到要删除的设备", item);
this.data.LinkedList.splice(i, 1);
this.disconnectDevice(item.deviceId);
flag = true;
@ -152,7 +152,7 @@ class BleHelper {
} else {
if (deviceId && item.deviceId == deviceId) {
console.log("找到要删除的设备1,", item)
// console.log("找到要删除的设备1,", item)
this.data.LinkedList.splice(i, 1);
this.disconnectDevice(item.deviceId);
flag = true;
@ -201,7 +201,7 @@ class BleHelper {
this.updateCache();
}
} else {
console.log("蓝牙未连接过任何设备");
// console.log("蓝牙未连接过任何设备");
this.updateCache();
}
}
@ -407,7 +407,7 @@ class BleHelper {
return new Promise((resolve, reject) => {
if (this.data.available) {
console.log("蓝牙模块是可用状态");
// console.log("蓝牙模块是可用状态");
resolve({
available: this.data.available,
discovering: this.data.discovering
@ -415,7 +415,7 @@ class BleHelper {
return;
}
if (!this.data.isOpenBlue) {
console.log("蓝牙模块未打开");
console.error("蓝牙模块未打开");
resolve({
available: false,
discovering: false
@ -424,13 +424,13 @@ class BleHelper {
}
uni.getBluetoothAdapterState({
success: (info) => {
console.log("蓝牙状态获取成功,", info)
// console.log("蓝牙状态获取成功,", info)
this.data.available = info.available;
this.data.discovering = info.discovering;
resolve(info);
},
fail: (ex1) => {
console.log("蓝牙状态获取失败", ex1);
console.error("蓝牙状态获取失败", ex1);
let res1 = {
available: false,
discovering: false
@ -446,14 +446,14 @@ class BleHelper {
var init = () => {
return new Promise((resolve, reject) => {
if (this.data.isOpenBlue) {
console.log("蓝牙状态已可用,提前返回");
// console.log("蓝牙状态已可用,提前返回");
resolve(false);
return;
}
uni.openBluetoothAdapter({
success: (args) => {
console.log("蓝牙初始化成功:" + JSON.stringify(args));
// console.log("蓝牙初始化成功:" + JSON.stringify(args));
this.data.isOpenBlue = true;
this.data.available = true;
resolve(true);
@ -471,7 +471,7 @@ 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;
@ -678,7 +678,7 @@ class BleHelper {
},
fail: (ex2) => {
console.log("蓝牙模块启动失败", ex2);
console.error("蓝牙模块启动失败", ex2);
reject(this.getError(ex2));
}
});
@ -692,7 +692,7 @@ class BleHelper {
}
this.CheckBlue().then((res) => {
console.log("res=", res)
// console.log("res=", res)
return init();
}).then(resolve).catch((ex) => {
console.error("异常:", ex);
@ -716,10 +716,11 @@ class BleHelper {
uni.closeBluetoothAdapter({
success: () => {
console.log("蓝牙模块已关闭");
// console.log("蓝牙模块已关闭");
this.data.isOpenBlue = false;
},
fail: (ex) => {
console.error('无法关闭蓝牙模块:',ex);
ex = this.getError(ex);
////console.log(msg);
},
@ -755,7 +756,7 @@ class BleHelper {
},
fail: (err) => {
console.log(`搜索蓝牙设备失败:`, err);
console.error(`搜索蓝牙设备失败:`, err);
reject(this.getError(err));
}
@ -797,11 +798,11 @@ class BleHelper {
let p1= new Promise((resolve, reject) => {
uni.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log("停止搜索蓝牙设备成功");
// console.log("停止搜索蓝牙设备成功");
resolve();
},
fail: (ex) => {
console.log("无法停止蓝牙搜索");
console.error("无法停止蓝牙搜索");
reject(this.getError(ex));
}
@ -856,7 +857,7 @@ class BleHelper {
//订阅消息
subScribe(deviceId, state) {
console.log("开始订阅消息", state);
// console.log("开始订阅消息", state);
return new Promise((resolve, reject) => {
setTimeout(() => {
@ -898,9 +899,9 @@ class BleHelper {
state: state,
success: (res) => {
if (state) {
console.log("订阅消息成功", res);
// console.log("订阅消息成功", res);
} else {
console.log("取消订阅成功", res);
// console.log("取消订阅成功", res);
}
this.data.LinkedList.find((v) => {
@ -960,8 +961,8 @@ class BleHelper {
}
}
} else {
console.log("c=", c);
console.log("serv=", serv);
// console.log("c=", c);
// console.log("serv=", serv);
reject({
code: 403,
msg: "出现错误,找不到任何通知特征"
@ -991,7 +992,7 @@ class BleHelper {
// console.log("finally")
});
} else {
console.log("没有特征需要订阅");
console.error("没有特征需要订阅");
resolve();
}
@ -1016,7 +1017,7 @@ class BleHelper {
deviceId: id,
success: (res) => {
if (res.services && res.services.length > 0) {
console.log("获取到服务:" + JSON.stringify(res));
// console.log("获取到服务:" + JSON.stringify(res));
this.data.LinkedList.find((v) => {
if (v.deviceId == id) {
@ -1041,7 +1042,7 @@ class BleHelper {
});
if (se) {
console.log("合作供应商的", s)
// console.log("合作供应商的", s)
this.data.LinkedList.find((v) => {
if (v.deviceId == id) {
v.writeServiceId = s.serviceId;
@ -1060,7 +1061,7 @@ class BleHelper {
} else {
console.log("预设的蓝牙服务和特征中找不到");
console.error("预设的蓝牙服务和特征中找不到");
for (var i = 0; i < res.services.length; i++) {
let service = res.services[i];
promises.push(this.getFeatrus(id, service.uuid,
@ -1070,7 +1071,7 @@ class BleHelper {
if (promises.length == 0) {
console.log("未找到主服务");
console.error("未找到主服务");
reject({
msg: "未找到主服务",
code: -1
@ -1089,8 +1090,8 @@ class BleHelper {
})
.then((res) => {
console.log('设备连接成功,初始化完成', this.data
.LinkedList);
// console.log('设备连接成功,初始化完成', this.data
// .LinkedList);
resolve();
})
@ -1138,7 +1139,7 @@ class BleHelper {
serviceId: serviceId,
success: (res) => {
console.log("获取到特征:" + JSON.stringify(res));
// console.log("获取到特征:" + JSON.stringify(res));
// res.characteristics.forEach((v) => {
// v.serviceId = serviceId;
// });
@ -1277,12 +1278,12 @@ class BleHelper {
return;
}
console.log("正在连接" + deviceId);
// console.log("正在连接" + deviceId);
uni.createBLEConnection({
deviceId: deviceId,
timeout: 30000,
success: (info) => {
console.log("新连接成功", this.data.LinkedList);
// console.log("新连接成功", this.data.LinkedList);
this.getLinkBlue().then((arr) => {
let cr = arr.devices.find(c => {
if (c.deviceId == deviceId) {
@ -1299,8 +1300,8 @@ class BleHelper {
}
this.updateCache();
console.log("LinkedList=", this.data
.LinkedList);
// console.log("LinkedList=", this.data
// .LinkedList);
// 处理 MTU 设置
if (plus.os.name === 'Android') {
@ -1312,7 +1313,7 @@ class BleHelper {
resolve(true);
},
fail: () => {
console.log("mtu设置失败");
console.error("mtu设置失败");
resolve(
true
); // MTU设置失败不影响连接成功
@ -1327,7 +1328,7 @@ class BleHelper {
},
fail: (ex) => {
ex = this.getError(ex);
console.log("蓝牙" + deviceId + "连接失败" + JSON.stringify(ex));
console.error("蓝牙" + deviceId + "连接失败" + JSON.stringify(ex));
// 连接超时后自动重试
if (ex.code === 10012 && retryCount < maxRetries) {
@ -1517,7 +1518,7 @@ class BleHelper {
},
fail: (ex) => {
ex = this.getError(ex);
console.log("发送数据失败", ex);
console.error("发送数据失败", ex);
err(ex);
}