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 v-if="curTab == 0">
<view class="pb-10">蓝牙设备列表</view> <view class="pb-10">蓝牙设备列表</view>
<template v-for="(item, index) in devices"> <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':''}`" :key="index" :class="`list-item ${item.deviceId == deviceId?'list-item-active':''}`"
> >
<u-row justify="between"> <u-row justify="between">
@ -114,13 +114,13 @@ export default {
}, },
watch: { watch: {
deviceId(val){ deviceId(val){
updateBlueSessionInfo() this.updateBlueSessionInfo()
}, },
serviceId(val){ serviceId(val){
updateBlueSessionInfo() this.updateBlueSessionInfo()
}, },
characteristicId(val){ characteristicId(val){
updateBlueSessionInfo() this.updateBlueSessionInfo()
} }
}, },
props:{ props:{
@ -152,28 +152,23 @@ export default {
this.stopDiscoveryPrinter() this.stopDiscoveryPrinter()
// this.closeBluetoothAdapter() // this.closeBluetoothAdapter()
}, },
methods: { methods: {
updateBlueSessionInfo() { updateBlueSessionInfo() {
let { deviceId, serviceId, characteristicId } = this let { deviceId, serviceId, characteristicId } = this
setSessionBluetoothInfo({ deviceId, serviceId, characteristicId }) setSessionBluetoothInfo({ deviceId, serviceId, characteristicId })
}, },
// 自动连接历史蓝牙配置 // 自动连接历史蓝牙配置
async autoConnect() { async autoConnect() {
let conSta = await this.checkConnectStat() let { characteristicId } = getSessionBluetoothInfo()
// 自动连接 let conSta = false
if(!conSta) { // 判断是否有历史连接
this.discoveryPrinter() if(characteristicId) {
conSta = await this.connect()
} }
return true // 连接不成功,打开蓝牙配置页面
}, if(!conSta) {
this.showConnectPage = true
// 检查蓝牙 缓存连接状态 【获取不到characteristics说明连接已经过期】 this.discoveryPrinter()
async checkConnectStat(){
let bl = false
try{
await this.getBLEDeviceCharacteristics()
bl = true
}catch(e){
} }
return bl return bl
}, },
@ -286,10 +281,10 @@ export default {
//获取蓝牙设备所有服务(service)。 //获取蓝牙设备所有服务(service)。
setTimeout(async()=> { setTimeout(async()=> {
await _this.getBLEDeviceServices() await _this.getBLEDeviceServices()
resolve() resolve(true)
}, 2000) }, 2000)
} else { } else {
resolve() resolve(false)
showToast(`连接设备失败:${e.errMsg}`) 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() { getInstance() {
return this return this
} }