From 5c69e32c2d8c98e4ef50a538373f6b0ec5fd6603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=BE=AE=E4=B8=80=E7=AC=91?= <709648985@qq.com> Date: Wed, 26 Nov 2025 15:56:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D6155=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=8A=A0=E5=8F=B7=E6=B7=BB=E5=8A=A0=E8=AE=BE=E5=A4=87=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E7=9B=91=E5=90=AC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/common/addBLE/addEquip.vue | 269 ++++++++++++++++++------------- 1 file changed, 160 insertions(+), 109 deletions(-) diff --git a/pages/common/addBLE/addEquip.vue b/pages/common/addBLE/addEquip.vue index a9cac65..5c821e1 100644 --- a/pages/common/addBLE/addEquip.vue +++ b/pages/common/addBLE/addEquip.vue @@ -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';