修复6155使用加号添加设备没有监听的问题
This commit is contained in:
@ -165,15 +165,19 @@
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
if (ble) {
|
||||
ble.StopSearch();
|
||||
ble.removeAllCallback(pagePath);
|
||||
|
||||
}
|
||||
},
|
||||
onLoad(option) {
|
||||
let search = option.search;
|
||||
these = this;
|
||||
eventChannel = this.getOpenerEventChannel();
|
||||
const systemInfo = uni.getSystemInfoSync();
|
||||
|
||||
ble = bleTool.getBleTool(); // Ensure ble is initialized
|
||||
|
||||
if (systemInfo.uniPlatform == 'web') {
|
||||
|
||||
|
||||
@ -216,7 +220,9 @@
|
||||
}
|
||||
let StartSubsrib = () => {
|
||||
these.EquipMents = [];
|
||||
if (!ble) {
|
||||
ble = bleTool.getBleTool();
|
||||
}
|
||||
//蓝牙不可用的回调
|
||||
ble.addStateBreakCallback(res => {
|
||||
if (these.Status.isPageHidden) {
|
||||
@ -233,19 +239,6 @@
|
||||
these.showOpenSetting();
|
||||
|
||||
}, pagePath);
|
||||
//蓝牙再次可用的回调
|
||||
ble.addStateRecoveryCallback(res => {
|
||||
if (these.Status.isPageHidden) {
|
||||
return;
|
||||
}
|
||||
uni.showToast({
|
||||
icon: 'success',
|
||||
title: '蓝牙恢复可用'
|
||||
});
|
||||
these.Status.BottomMenu.show = false;
|
||||
these.EquipMents = [];
|
||||
these.refreshBleList();
|
||||
}, pagePath);
|
||||
|
||||
//蓝牙断开连接的回调
|
||||
ble.addDisposeCallback(res => {
|
||||
@ -262,6 +255,10 @@
|
||||
|
||||
|
||||
}, pagePath);
|
||||
|
||||
// --- Start: Logic for Pairing Mode Only ---
|
||||
if (these.device) {
|
||||
console.log("进入配对模式,启用连接恢复和验证逻辑。");
|
||||
//蓝牙连接已恢复的回调
|
||||
ble.addRecoveryCallback(res => {
|
||||
if (these.Status.isPageHidden) {
|
||||
@ -290,46 +287,7 @@
|
||||
|
||||
|
||||
}, pagePath);
|
||||
//搜索到新设备的回调
|
||||
ble.addDeviceFound((arr) => {
|
||||
if (these.Status.isPageHidden) {
|
||||
return;
|
||||
}
|
||||
arr = arr.devices;
|
||||
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
|
||||
arr[i].linkStatu = false;
|
||||
if (!arr[i].name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let f = these.EquipMents.find((v, index) => {
|
||||
if (v.deviceId == arr[i].deviceId) {
|
||||
|
||||
these.$set(these.EquipMents[index], 'RSSI', arr[i].RSSI);
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if (!f) {
|
||||
console.log("发现新设备,", arr[i]);
|
||||
|
||||
if (these.device && these.device.bluetoothName) {
|
||||
if (these.device.bluetoothName === arr[i].name || arr[i].name.indexOf(these
|
||||
.device.bluetoothName) > -1 || these.device.bluetoothName.indexOf(arr[
|
||||
i].name) > -1) {
|
||||
arr[i].isTarget = true;
|
||||
}
|
||||
}
|
||||
arr[i].name = arr[i].name.replace('JQZM-', '');
|
||||
these.EquipMents.push(arr[i]);
|
||||
}
|
||||
}
|
||||
// console.log("EquipMents=", these.EquipMents)
|
||||
}, pagePath);
|
||||
//收到设备的消息回调
|
||||
ble.addReceiveCallback((receivData, f, path, arr) => {
|
||||
if (these.Status.isPageHidden) {
|
||||
@ -351,18 +309,60 @@
|
||||
|
||||
|
||||
}, pagePath);
|
||||
}
|
||||
// --- End: Logic for Pairing Mode Only ---
|
||||
|
||||
//搜索到新设备的回调 (Always active)
|
||||
ble.addDeviceFound((arr) => {
|
||||
console.log("--- 收到原始扫描数据 ---", JSON.stringify(arr));
|
||||
if (these.Status.isPageHidden) {
|
||||
return;
|
||||
}
|
||||
if (!arr || !arr.devices) {
|
||||
return;
|
||||
}
|
||||
arr = arr.devices;
|
||||
|
||||
console.log(`本次扫描批次发现 ${arr.length} 个设备`);
|
||||
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
let device = arr[i];
|
||||
device.linkStatu = false;
|
||||
|
||||
let f = these.EquipMents.find((v, index) => {
|
||||
if (v.deviceId == device.deviceId) {
|
||||
console.log(`更新设备信号: ${device.name || device.deviceId}, RSSI: ${device.RSSI}`);
|
||||
these.$set(these.EquipMents[index], 'RSSI', device.RSSI);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if (!f) {
|
||||
console.log("+++ 发现新设备,准备添加到列表:", JSON.stringify(device));
|
||||
|
||||
if (these.device && these.device.bluetoothName && device.name) {
|
||||
if (these.device.bluetoothName === device.name || (device.name && device.name.indexOf(these
|
||||
.device.bluetoothName) > -1) || (device.name && this.device.bluetoothName.indexOf(
|
||||
device.name) > -1)) {
|
||||
device.isTarget = true;
|
||||
}
|
||||
}
|
||||
if (device.name) {
|
||||
device.name = device.name.replace('JQZM-', '');
|
||||
}
|
||||
these.EquipMents.push(device);
|
||||
}
|
||||
}
|
||||
}, pagePath);
|
||||
}
|
||||
|
||||
if (search) {
|
||||
StartSubsrib();
|
||||
}
|
||||
|
||||
eventChannel.on('detailData', function(rec) {
|
||||
console.log("接收到父页面的参数:", rec);
|
||||
these.device = rec.data;
|
||||
StartSubsrib();
|
||||
|
||||
these.refreshBleList();
|
||||
});
|
||||
|
||||
@ -373,41 +373,92 @@
|
||||
this.refreshBleList();
|
||||
},
|
||||
methods: {
|
||||
refreshBleList() {
|
||||
if (!ble) {
|
||||
checkAndRequestLocationPermission() {
|
||||
return new Promise((resolve) => {
|
||||
|
||||
if (uni.getSystemInfoSync().platform !== 'android') {
|
||||
return resolve(true);
|
||||
}
|
||||
|
||||
plus.android.requestPermissions(
|
||||
['android.permission.ACCESS_FINE_LOCATION'],
|
||||
(result) => {
|
||||
if (result.granted.length > 0) {
|
||||
console.log('定位权限已授予');
|
||||
resolve(true);
|
||||
} else {
|
||||
console.warn('定位权限被拒绝');
|
||||
uni.showModal({
|
||||
title: '权限提醒',
|
||||
content: '为了正常扫描蓝牙设备,需要您开启定位权限',
|
||||
showCancel: false,
|
||||
success: () => {
|
||||
uni.openSetting(); // 引导用户去设置页
|
||||
}
|
||||
});
|
||||
resolve(false);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
console.error('请求定位权限失败:', error);
|
||||
uni.showToast({
|
||||
title: '权限请求异常',
|
||||
icon: 'none'
|
||||
});
|
||||
resolve(false);
|
||||
}
|
||||
);
|
||||
|
||||
resolve(true);
|
||||
});
|
||||
},
|
||||
async refreshBleList() {
|
||||
|
||||
const hasPermission = await this.checkAndRequestLocationPermission();
|
||||
if (!hasPermission) {
|
||||
console.log("缺少定位权限,已中止蓝牙扫描。");
|
||||
return;
|
||||
}
|
||||
let promis = [];
|
||||
for (let index = 0; index < this.PairEquip.length; index++) {
|
||||
let item = this.PairEquip[index];
|
||||
promis.push(ble.disconnectDevice(item.deviceId));
|
||||
// #endif
|
||||
|
||||
if (!ble) {
|
||||
ble = bleTool.getBleTool();
|
||||
if (!ble) {
|
||||
console.error("BLE helper not initialized!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Promise.allSettled(promis).finally(() => {
|
||||
ble.StopSearch().finally(res => {
|
||||
console.log("停止搜索成功");
|
||||
ble.StopSearch().finally(() => {
|
||||
|
||||
let disconnectPromises = [];
|
||||
if (ble.data && ble.data.LinkedList) {
|
||||
ble.data.LinkedList.forEach(device => {
|
||||
console.log(`Requesting disconnect for ${device.deviceId}`);
|
||||
disconnectPromises.push(ble.disconnectDevice(device.deviceId));
|
||||
});
|
||||
}
|
||||
|
||||
Promise.allSettled(disconnectPromises).finally(() => {
|
||||
|
||||
these.EquipMents = [];
|
||||
these.PairEquip = [];
|
||||
|
||||
ble.StartSearch().then(result => {
|
||||
console.log("开始搜索成功");
|
||||
console.log("Fresh scan started successfully.");
|
||||
}).catch(err => {
|
||||
console.error("开始搜索失败,err=", err);
|
||||
console.error("Failed to start fresh scan:", err);
|
||||
if (err.code === 10001) {
|
||||
these.showOpenSetting();
|
||||
} else {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '出现异常:' + err.msg
|
||||
content: '开始搜索失败:' + err.msg
|
||||
});
|
||||
}
|
||||
});
|
||||
}).catch(ex => {
|
||||
console.error("ex=", ex);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
},
|
||||
isItemLink: function(item, index) {
|
||||
let src = '/static/images/BLEAdd/noLink.png';
|
||||
|
||||
Reference in New Issue
Block a user