From 403ee37a094b5e56bab2d902f4dab967f06c5a0a Mon Sep 17 00:00:00 2001 From: fanpeng <438123081@qq.com> Date: Mon, 9 Jun 2025 17:37:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E8=93=9D=E7=89=99?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=9A=84=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 1 - pages/addDevice/searchDevice.vue | 2 +- stores/bluetooth.js | 141 +++++++++++++++++++------------ 3 files changed, 89 insertions(+), 55 deletions(-) diff --git a/App.vue b/App.vue index 3c515c3..2bfcb22 100644 --- a/App.vue +++ b/App.vue @@ -49,7 +49,6 @@ this.onBluetoothState() // 检查蓝牙权限 const checkResult = await this.checkSetting() - console.log(checkResult) if (checkResult === true) { this.initAndListenBluetooth(false) } diff --git a/pages/addDevice/searchDevice.vue b/pages/addDevice/searchDevice.vue index 50274ba..e2519db 100644 --- a/pages/addDevice/searchDevice.vue +++ b/pages/addDevice/searchDevice.vue @@ -130,8 +130,8 @@ nowTime: this.serverTimestamp, localTime: timestamp }) + uni.hideLoading() if (code !== 0) { - uni.hideLoading() uni.showToast({ title: '连接失败,请靠近设备并保持设备处于唤醒状态', icon: 'none' diff --git a/stores/bluetooth.js b/stores/bluetooth.js index dad8566..6e53f20 100644 --- a/stores/bluetooth.js +++ b/stores/bluetooth.js @@ -190,7 +190,9 @@ export const useBluetoothStore = defineStore('ble', { }, // 关闭全部蓝牙监听并关闭蓝牙模拟 closeAllBluetooth() { + // #ifdef MP uni.offBluetoothAdapterStateChange() + // #endif uni.closeBluetoothAdapter({ success(res) { console.log('关闭蓝牙模块', res) @@ -858,7 +860,7 @@ export const useBluetoothStore = defineStore('ble', { } } } - console.log('设备列表', that.deviceList) + // console.log('设备列表', that.deviceList) }, async fail(res) { console.log('获取设备列表失败', res) @@ -943,6 +945,9 @@ export const useBluetoothStore = defineStore('ble', { } }) // #endif + // #ifdef APP-PLUS + resolve(true) + // #endif resolve(false) }) }, @@ -962,63 +967,74 @@ export const useBluetoothStore = defineStore('ble', { success(res) { console.log('连接成功', res) // 获取设备服务 - uni.getBLEDeviceServices({ - deviceId: that.currentLockInfo.deviceId, - success(res) { - let serviceId - for (let i = 0; i < res.services.length; i++) { - if (res.services[i].uuid.indexOf('FFF0') !== -1) { - serviceId = res.services[i].uuid - } - } - // 获取设备对应服务的特征值 - uni.getBLEDeviceCharacteristics({ - deviceId: that.currentLockInfo.deviceId, - serviceId, - success(res) { - let notifyCharacteristicId - let writeCharacteristicId - for (let i = 0; i < res.characteristics.length; i++) { - const characteristic = res.characteristics[i] - if (characteristic.properties.notify) { - notifyCharacteristicId = characteristic.uuid - } - if (characteristic.properties.write) { - writeCharacteristicId = characteristic.uuid - } + const executeBluetoothOperation = () => { + uni.getBLEDeviceServices({ + deviceId: that.currentLockInfo.deviceId, + success(res) { + let serviceId + for (let i = 0; i < res.services.length; i++) { + if (res.services[i].uuid.indexOf('FFF0') !== -1) { + serviceId = res.services[i].uuid } - that.updateCurrentLockInfo({ - ...that.currentLockInfo, - serviceId, - notifyCharacteristicId, - writeCharacteristicId - }) - that.notifyBluetoothCharacteristicValueChange() - resolve(true) - }, - fail(res) { - if (res.errCode === 10006) { - uni.showToast({ - title: '连接失败,请靠近设备并保持设备处于唤醒状态', - icon: 'none' + } + + // 获取设备对应服务的特征值 + uni.getBLEDeviceCharacteristics({ + deviceId: that.currentLockInfo.deviceId, + serviceId, + success(res) { + let notifyCharacteristicId + let writeCharacteristicId + for (let i = 0; i < res.characteristics.length; i++) { + const characteristic = res.characteristics[i] + if (characteristic.properties.notify) { + notifyCharacteristicId = characteristic.uuid + } + if (characteristic.properties.write) { + writeCharacteristicId = characteristic.uuid + } + } + that.updateCurrentLockInfo({ + ...that.currentLockInfo, + serviceId, + notifyCharacteristicId, + writeCharacteristicId }) + that.notifyBluetoothCharacteristicValueChange() + resolve(true) + }, + fail(res) { + if (res.errCode === 10006) { + uni.showToast({ + title: '连接失败,请靠近设备并保持设备处于唤醒状态', + icon: 'none' + }) + } + console.log('获取设备特征值失败', res) + resolve(false) } - console.log('获取设备特征值失败', res) - resolve(false) - } - }) - }, - fail(res) { - if (res.errCode === 10006) { - uni.showToast({ - title: '连接失败,请靠近设备并保持设备处于唤醒状态', - icon: 'none' }) + }, + fail(res) { + if (res.errCode === 10006) { + uni.showToast({ + title: '连接失败,请靠近设备并保持设备处于唤醒状态', + icon: 'none' + }) + } + console.log('获取设备服务失败', res) + resolve(false) } - console.log('获取设备服务失败', res) - resolve(false) - } - }) + }) + } + + // #ifdef APP-PLUS + setTimeout(executeBluetoothOperation, 800) + // #endif + + // #ifdef MP + executeBluetoothOperation() + // #endif }, async fail(res) { console.log('连接失败', res) @@ -1412,6 +1428,24 @@ export const useBluetoothStore = defineStore('ble', { i === count - 1 ? binaryData.length : (i + 1) * 20 ) + // #ifdef APP-PLUS + setTimeout( + () => { + uni.writeBLECharacteristicValue({ + deviceId: that.currentLockInfo.deviceId, + serviceId: that.currentLockInfo.serviceId, + characteristicId: that.currentLockInfo.writeCharacteristicId, + value: writeData.buffer, + fail(res) { + console.log('写入失败', res) + } + }) + }, + (i + 1) * 200 + ) + // #endif + + // #ifdef MP uni.writeBLECharacteristicValue({ deviceId: that.currentLockInfo.deviceId, serviceId: that.currentLockInfo.serviceId, @@ -1421,6 +1455,7 @@ export const useBluetoothStore = defineStore('ble', { console.log('写入失败', res) } }) + // #endif } } },