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 } }