蓝牙开始、结束搜索添加状态判定

This commit is contained in:
liub
2025-10-30 11:10:57 +08:00
parent bd56ca997b
commit c39cbcb34d
11 changed files with 104 additions and 100 deletions

View File

@ -511,9 +511,11 @@ class BleHelper {
uni.onBluetoothAdapterStateChange((state) => {
// console.log('蓝牙状态发生变化:' + JSON.stringify(state));
this.data.discovering = state.discovering;
if (this.data.available !== state.available) {
this.data.available = state.available;
this.data.discovering = state.discovering;
if (this.data.available && this.data
.isOpenBlue) { //蓝牙状态再次可用,重连所有设备
@ -870,10 +872,7 @@ class BleHelper {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
this.data.available = false;
this.data.discovering = false;
this.data.isOpenBlue = false;
this.data.searchList = [];
this.StopSearch();
@ -882,15 +881,19 @@ class BleHelper {
uni.closeBluetoothAdapter({
success: () => {
// console.log("蓝牙模块已关闭");
this.data.available = false;
this.data.discovering = false;
this.data.isOpenBlue = false;
this.data.searchList = [];
resolve();
},
fail: (ex) => {
console.error('无法关闭蓝牙模块:', ex);
ex = this.getError(ex);
reject(ex);
},
complete: () => {
resolve();
}
});
@ -963,7 +966,8 @@ class BleHelper {
//停止搜索
StopSearch() {
if (this.data.platform == 'web') {
if (this.data.platform == 'web' || !this.data.discovering) {
console.error("web平台或当前没有搜索,直接返回");
return Promise.resolve();
}
let p1 = new Promise((resolve, reject) => {
@ -1481,7 +1485,9 @@ class BleHelper {
// 处理 MTU 设置
if (plus.os.name === 'Android') {
this.setMtu(deviceId);
this.setMtu(deviceId).catch(ex=>{
console.error("mtu设置失败=",ex);
});
}
resolve(true);
@ -1579,12 +1585,11 @@ class BleHelper {
_succ(mtuRes);
},
fail: (ex) => {
console.error(
"mtu设置失败", ex);
ex = this.getError(ex);
_err(
ex
); // MTU设置失败不影响连接成功
);
},
complete() {
console.log("设置mtu完毕");