This commit is contained in:
fengerli
2025-11-26 15:57:54 +08:00

View File

@ -165,15 +165,19 @@
},
onUnload() {
ble.StopSearch();
ble.removeAllCallback(pagePath);
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 = [];
ble = bleTool.getBleTool();
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,107 +255,114 @@
}, pagePath);
//蓝牙连接已恢复的回调
ble.addRecoveryCallback(res => {
// --- Start: Logic for Pairing Mode Only ---
if (these.device) {
console.log("进入配对模式,启用连接恢复和验证逻辑。");
//蓝牙连接已恢复的回调
ble.addRecoveryCallback(res => {
if (these.Status.isPageHidden) {
return;
}
// hideLoading(these);
these.EquipMents.find(function(v, ind) {
if (v.deviceId == res.deviceId) {
these.PairEquip.push(v);
return true;
}
return false;
});
if (these.device) {
clearInterval(this.Status.intval);
showLoading(these, {
text: '蓝牙连接已恢复,正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(res.deviceId);
}, 0);
} else {
hideLoading(these);
}
}, pagePath);
//收到设备的消息回调
ble.addReceiveCallback((receivData, f, path, arr) => {
if (these.Status.isPageHidden) {
return;
}
if (f.macAddress && these.device) {
clearInterval(this.Status.intval);
this.Status.intval = null;
this.Status.time = null;
showLoading(these, {
text: '正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(f.deviceId);
}, 0);
}
}, pagePath);
}
// --- End: Logic for Pairing Mode Only ---
//搜索到新设备的回调 (Always active)
ble.addDeviceFound((arr) => {
console.log("--- 收到原始扫描数据 ---", JSON.stringify(arr));
if (these.Status.isPageHidden) {
return;
}
// hideLoading(these);
these.EquipMents.find(function(v, ind) {
if (v.deviceId == res.deviceId) {
these.PairEquip.push(v);
return true;
}
return false;
});
if (these.device) {
clearInterval(this.Status.intval);
showLoading(these, {
text: '蓝牙连接已恢复,正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(res.deviceId);
}, 0);
} else {
hideLoading(these);
}
}, pagePath);
//搜索到新设备的回调
ble.addDeviceFound((arr) => {
if (these.Status.isPageHidden) {
if (!arr || !arr.devices) {
return;
}
arr = arr.devices;
for (var i = 0; i < arr.length; i++) {
console.log(`本次扫描批次发现 ${arr.length} 个设备`);
arr[i].linkStatu = false;
if (!arr[i].name) {
continue;
}
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 == arr[i].deviceId) {
these.$set(these.EquipMents[index], 'RSSI', arr[i].RSSI);
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("发现新设备,", arr[i]);
console.log("+++ 发现新设备,准备添加到列表:", JSON.stringify(device));
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;
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;
}
}
arr[i].name = arr[i].name.replace('JQZM-', '');
these.EquipMents.push(arr[i]);
if (device.name) {
device.name = device.name.replace('JQZM-', '');
}
these.EquipMents.push(device);
}
}
// console.log("EquipMents=", these.EquipMents)
}, pagePath);
//收到设备的消息回调
ble.addReceiveCallback((receivData, f, path, arr) => {
if (these.Status.isPageHidden) {
return;
}
if (f.macAddress && these.device) {
clearInterval(this.Status.intval);
this.Status.intval = null;
this.Status.time = null;
showLoading(these, {
text: '正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(f.deviceId);
}, 0);
}
}, pagePath);
}
if (search) {
StartSubsrib();
}
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);
if (err.code === 10001) {
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';