This commit is contained in:
aihe 2024-05-13 02:02:08 +08:00
parent 4fe62d1f08
commit 8ebe91b021

View File

@ -34,7 +34,7 @@
<view v-if="curTab == 0">
<view class="pb-10">蓝牙设备列表</view>
<template v-for="(item, index) in devices">
<view @click="handleSelectDeviceId(item)" v-if="item.name.length>0"
<view @click="handleAutoSelectAllData(item)" v-if="item.name.length>0"
:key="index" :class="`list-item ${item.deviceId == deviceId?'list-item-active':''}`"
>
<u-row justify="between">
@ -114,13 +114,13 @@ export default {
},
watch: {
deviceId(val){
updateBlueSessionInfo()
this.updateBlueSessionInfo()
},
serviceId(val){
updateBlueSessionInfo()
this.updateBlueSessionInfo()
},
characteristicId(val){
updateBlueSessionInfo()
this.updateBlueSessionInfo()
}
},
props:{
@ -152,28 +152,23 @@ export default {
this.stopDiscoveryPrinter()
// this.closeBluetoothAdapter()
},
methods: {
methods: {
updateBlueSessionInfo() {
let { deviceId, serviceId, characteristicId } = this
setSessionBluetoothInfo({ deviceId, serviceId, characteristicId })
},
// 自动连接历史蓝牙配置
async autoConnect() {
let conSta = await this.checkConnectStat()
// 自动连接
if(!conSta) {
this.discoveryPrinter()
let { characteristicId } = getSessionBluetoothInfo()
let conSta = false
// 判断是否有历史连接
if(characteristicId) {
conSta = await this.connect()
}
return true
},
// 检查蓝牙 缓存连接状态 【获取不到characteristics说明连接已经过期】
async checkConnectStat(){
let bl = false
try{
await this.getBLEDeviceCharacteristics()
bl = true
}catch(e){
// 连接不成功,打开蓝牙配置页面
if(!conSta) {
this.showConnectPage = true
this.discoveryPrinter()
}
return bl
},
@ -286,10 +281,10 @@ export default {
//获取蓝牙设备所有服务(service)。
setTimeout(async()=> {
await _this.getBLEDeviceServices()
resolve()
resolve(true)
}, 2000)
} else {
resolve()
resolve(false)
showToast(`连接设备失败:${e.errMsg}`)
}
}
@ -345,6 +340,43 @@ export default {
})
})
},
// 根据选择的deviceId自动匹配 serviceId, characteristicId
async handleAutoSelectAllData(item) {
console.log("============测试==1111111")
const _this = this
this.deviceId = item.deviceId
this.serviceId = ''
this.serverList = []
this.characteristics = []
this.characteristicId = ''
await this.connect()
if(this.serverList?.length > 0) {
for(let i=0; i<_this.serverList?.length; i++) {
let _serviceId = _this.serverList[i].uuid
let charRes = await new Promise((resolve, reject) => {
uni.getBLEDeviceCharacteristics({
deviceId: _this.deviceId,
serviceId: _serviceId,
success:(res)=>{
let charItem = res.characteristics.filter(t=> {
let { notify, indicate, write } = t.properties || {}
return (notify || indicate) && write
})[0]
resolve(charItem)
},
fail:(res)=>{
resolve()
}
})
})
if(charRes) {
_this.serviceId = _serviceId
_this.characteristicId = charRes.uuid
break
}
}
}
},
getInstance() {
return this
}