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