This commit is contained in:
aihe 2024-06-23 11:58:06 +08:00
parent bcd4de21cb
commit d24fd1c0fb

View File

@ -1,7 +1,7 @@
<template>
<tpx-layout>
<template v-slot:header>
<view class="blcok-white" style="margin-top: 0;">
<view class="blcok-white" style="margin-top: 0;padding-top: 0;padding-bottom: 0;">
<view >
<u-row custom-style="gap: 10rpx;">
<u-button @click="discoveryPrinter">
@ -12,27 +12,26 @@
<slot name="button"></slot>
</u-row>
</view>
<view class="">
<view class="pt-10 pb-10">连接信息</view>
<u-steps activeColor="#969799" :current="curTab" direction="column" dot="true">
<u-steps-item v-for="(item, index) in conInfos">
<template v-slot:title>
<view @click="changeTab(index)" :class="`${index == curTab ? 'clr-prm' : 'clr-sub'} pl-20`">{{item.title}}</view>
</template>
<template v-slot:desc>
<view @click="changeTab(index)" class="mt-10 pl-20">{{item.desc}}</view>
</template>
</u-steps-item>
</u-steps>
<view class="blcok-grey-sm">
<u-row justify="between">
<view>
<text class="clr-sub pr-10">蓝牙设备</text>
<text class="clr-prm">{{deviceName || '-'}}</text>
</view>
<view class="clr-prm">{{isStateOn?'已连接':'未连接'}}</view>
</u-row>
<view class="mt-10">
<text class="clr-sub pr-10">蓝牙地址</text>
<text class="clr-prm">{{deviceId || '-'}}</text>
</view>
</view>
</view>
</template>
<template v-slot:body>
<view class="blcok-white">
<view v-if="curTab == 0">
<view class="pb-10">蓝牙设备列表</view>
<view class="blcok-white" style="margin-top: 0;">
<view >
<view class="com-section font-28 pb-20">蓝牙设备列表</view>
<template v-for="(item, index) in devices">
<view @click="handleSelectDeviceId(item)" v-if="item.name.length>0"
:key="index" :class="`list-item ${item.deviceId == deviceId?'list-item-active':''}`"
@ -45,6 +44,7 @@
</view>
</view>
</template>
<tpx-empty v-if="devices.length == 0" size="sm"></tpx-empty>
</view>
<slot name="content"></slot>
@ -61,12 +61,6 @@ import bluetoothTool from './nativeBlueTooth'
export default {
computed: {
conInfos() {
let ary = [
{ title: '蓝牙设备', desc: this.deviceId || '-' },
]
return ary
},
},
watch: {
deviceId(val){
@ -77,15 +71,15 @@ export default {
},
data () {
let { deviceId } = getSessionBluetoothInfo()
let { deviceId, deviceName } = getSessionBluetoothInfo()
return {
canvas_width: 240,
canvas_height: 240,
curTab: 0,
searchLoading: false,
isStateOn: false,
isStateOn: false, // 蓝牙连接状态
devices: [],
deviceId,
deviceName
}
},
async mounted () {
@ -99,17 +93,17 @@ export default {
},
methods: {
updateBlueSessionInfo() {
let { deviceId } = this
setSessionBluetoothInfo({ deviceId })
let { deviceId, deviceName } = this
setSessionBluetoothInfo({ deviceId, deviceName })
},
// 自动连接历史蓝牙配置
async autoConnect() {
console.log("autoConnect start===========")
let { deviceId, isStateOn } = this
let { deviceId } = this
let conSta = false
const _this = this
// 判断是否有历史连接
if(deviceId && isStateOn) {
if(deviceId) {
conSta = await this.connect(deviceId)
}
console.log("=========当前蓝牙连接状态=======", conSta)
@ -132,7 +126,11 @@ export default {
showToast(err.errMsg)
}
}
let isStateOn = false
// 唤起蓝牙搜索权限
await uniPromiseApi('startBluetoothDevicesDiscovery', { })
uniPromiseApi('stopBluetoothDevicesDiscovery')
// end 唤起蓝牙搜索权限
//#ifdef APP-PLUS
await new Promise((resolve)=> {
// 蓝牙
@ -157,7 +155,6 @@ export default {
});
})
//#endif
this.isStateOn = isStateOn
},
onDiscoverDevice(item = {}){
const _this = this
@ -183,6 +180,7 @@ export default {
// 停止搜寻附近的蓝牙外围设备
async stopDiscoveryPrinter () {
this.searchLoading = false
bluetoothTool.cancelDiscovery()
return uniPromiseApi('stopBluetoothDevicesDiscovery')
},
// 断开蓝牙
@ -192,7 +190,7 @@ export default {
},
async handleSelectDeviceId (item) {
this.deviceId = item.deviceId
// TODO
this.deviceName = item.name
//连接蓝牙
let bl = await this.connect(this.deviceId)
},
@ -202,7 +200,9 @@ export default {
console.log("连接蓝牙 设备=====", deviceId)
let bl = await new Promise((resolve)=> {
bluetoothTool.connDevice(deviceId,(result) => {
resolve(!!result)
let lb = !!result
_this.isStateOn = lb
resolve(lb)
})
})
hideLoading()