From 336df1bf662e45ce14772f9bf1f7220a73c3d5c0 Mon Sep 17 00:00:00 2001 From: aihe <961836564@qq.com> Date: Tue, 14 May 2024 01:28:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E8=87=AA=E5=8A=A8=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/tpx-bluetooth/tpx-bluetooth.vue | 32 ++++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/components/tpx-bluetooth/tpx-bluetooth.vue b/components/tpx-bluetooth/tpx-bluetooth.vue index ba714dc..eb252bd 100644 --- a/components/tpx-bluetooth/tpx-bluetooth.vue +++ b/components/tpx-bluetooth/tpx-bluetooth.vue @@ -159,18 +159,20 @@ export default { }, // 自动连接历史蓝牙配置 async autoConnect() { - let { characteristicId } = getSessionBluetoothInfo() + console.log("autoConnect start===========") + let { characteristicId, serviceId, deviceId } = this let conSta = false + const _this = this // 判断是否有历史连接 - if(characteristicId) { - let { res, err } = await this.connect() - conSta = !!res - } + if(deviceId) { + await this.handleAutoSelectAllData({ deviceId }) + conSta = !!_this.characteristicId // 判断是否有选中 characteristicId + } console.log("=========当前蓝牙连接状态=======", conSta) // 连接不成功,打开蓝牙配置页面 if(!conSta) { this.showConnectPage = true - this.discoveryPrinter() + // this.discoveryPrinter() } else { this.stopDiscoveryPrinter() } @@ -258,9 +260,14 @@ export default { showLoading('正在连接蓝牙') let { res, err } = await uniPromiseApi('createBLEConnection', { deviceId: _this.deviceId }) hideLoading() - if( res) { + if(res || err?.code == -1) { + // code: -1 已连接 + res = res || err + err = undefined + } + if(res) { showToast(`蓝牙连接成功`) - await promiseTimeout(1000) + await promiseTimeout(1500) // 获取蓝牙设备所有服务(service)。 await _this.getBLEDeviceServices() } @@ -277,7 +284,6 @@ export default { }) if(res) { _this.serverList = res.services - // _this.serviceId = res.services[0].uuid; console.log('serverId:', _this.serviceId) } if(err) { @@ -301,25 +307,27 @@ export default { }, // 根据选择的deviceId自动匹配 serviceId, characteristicId async handleAutoSelectAllData(item) { + console.log("handleAutoSelectAllData :item======", item) const _this = this await this.handleSelectDeviceId(item) if(this.serverList?.length > 0) { for(let i=0; i<_this.serverList?.length; i++) { let _serviceId = _this.serverList[i].uuid - let charApi = await uniPromiseApi('getBLEDeviceCharacteristics', { + let charPms = await uniPromiseApi('getBLEDeviceCharacteristics', { deviceId: _this.deviceId, serviceId: _serviceId, }) - let { res, err } = charApi + let { res, err } = charPms if(res) { let charItem = res.characteristics.filter(t=> { let { notify, indicate, write } = t.properties || {} + // return write return (notify || indicate) && write })[0] if(charItem) { _this.serviceId = _serviceId - _this.characteristicId = charRes.uuid + _this.characteristicId = charItem.uuid break } }