1. 修改蓝牙初始化位置

This commit is contained in:
范鹏 2024-08-22 16:37:15 +08:00
parent 7663c1cecd
commit 921f9607a3
8 changed files with 184 additions and 50 deletions

33
App.vue
View File

@ -3,8 +3,7 @@
import { useUserStore } from '@/stores/user'
import { useBasicStore } from '@/stores/basic'
import { mapState, mapActions } from 'pinia'
import { getUserInfoRequest } from '@/api/user'
let vm
let firstCheckSetting = true
export default {
globalData: {
@ -16,28 +15,26 @@
computed: {
...mapState(useBluetoothStore, ['bluetoothStatus']),
},
onLaunch: function() {
onLaunch: async function() {
//
this.updateMiniProgram()
// //
// this.initBluetooth()
// //
// this.onBluetoothState()
// //
// this.onBluetoothConnectStatus()
// //
// this.onBluetoothCharacteristicValueChange()
this.getDeviceInfo()
this.getButtonInfo()
//
const checkResult = await this.checkSetting()
console.log(checkResult)
if(checkResult === true) {
this.initAndListenBluetooth()
}
},
onShow: function() {
this.checkSetting()
onShow() {
if(firstCheckSetting) {
firstCheckSetting = false
} else {
this.checkSetting()
}
},
methods: {
...mapActions(useBluetoothStore, ['initBluetooth', 'onBluetoothState', 'updateBluetoothStatus', 'checkSetting',
'onBluetoothConnectStatus', 'onBluetoothCharacteristicValueChange']),
...mapActions(useBasicStore, ['getDeviceInfo', 'getButtonInfo']),
'onBluetoothConnectStatus', 'onBluetoothCharacteristicValueChange', 'updateInitBluetooth', 'initAndListenBluetooth']),
...mapActions(useUserStore, ['updateLoginStatus']),
//
updateMiniProgram() {

View File

@ -100,6 +100,24 @@
"navigationBarBackgroundColor": "#63b8af",
"navigationStyle": "default"
}
},
{
"path": "pages/lockDetail/lockDetail",
"style": {
"navigationBarTitleText": "星星锁",
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#63b8af",
"navigationStyle": "default"
}
},
{
"path": "pages/searchDevice/searchDevice",
"style": {
"navigationBarTitleText": "搜索",
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#63b8af",
"navigationStyle": "default"
}
}
],
"globalStyle": {

View File

@ -12,7 +12,7 @@
</view>
<view class="lock-list" v-if="!penging">
<view v-if="lockList.length === 0 && search.searchStr === '' && !focus">
<image src="/static/images/icon_add.png" mode="aspectFill" class="button-add-big"></image>
<image src="/static/images/icon_add.png" mode="aspectFill" class="button-add-big" @click="toSearchDevice"></image>
<view class="text">填加锁时手机必须在锁旁边</view>
</view>
<view v-else>
@ -21,7 +21,7 @@
<view class="group-name-text">{{group.groupName}}</view>
<view class="group-name-line"></view>
</view>
<view class="lock" v-for="lock in group.lockList" :key="lock.lockId">
<view class="lock" v-for="lock in group.lockList" :key="lock.lockId" @click="toLockDeatil(lock)">
<view class="lock-top">
<image class="lock-image-lock" src="/static/images/icon_lock.png"></image>
<view class="lock-top-right">
@ -46,7 +46,7 @@
</view>
</scroll-view>
<image v-if="lockList.length !== 0" src="/static/images/icon_add.png" mode="aspectFill"
class="button-add"></image>
class="button-add" @click="toSearchDevice"></image>
</view>
<view v-else>
<view class="button-login" @click="login">登录</view>
@ -60,6 +60,8 @@
import { getUserInfoRequest } from '@/api/user'
import { useUserStore } from '@/stores/user'
import { useLockStore } from '@/stores/lock'
import { useBluetoothStore } from '@/stores/bluetooth'
import { useBasicStore } from '@/stores/basic'
import { mapState, mapActions } from 'pinia'
export default {
@ -72,18 +74,18 @@
},
refresherTriggered: false,
focus: false,
penging: false
penging: true
}
},
computed: {
...mapState(useUserStore, ['userInfo', 'isLogin']),
...mapState(useLockStore, ['lockList', 'lockTotal'])
...mapState(useLockStore, ['lockList', 'lockTotal']),
...mapState(useBluetoothStore, ['bluetoothStatus', 'isInitBluetooth']),
},
async onLoad() {
uni.showLoading({
title: '加载中'
})
this.penging = true
const token = uni.getStorageSync('token')
if(token) {
this.updateLoginStatus(true)
@ -99,6 +101,8 @@
timeFormat,
...mapActions(useUserStore, ['updateUserInfo', 'updateLoginStatus', 'login']),
...mapActions(useLockStore, ['getLockList', 'getRole', 'getTimeLimit']),
...mapActions(useBluetoothStore, ['getBluetoothStatus', 'initAndListenBluetooth', 'updateCurrentLockInfo', 'checkSetting']),
...mapActions(useBasicStore, ['routeJump']),
async nextPage() {
if(this.lockList.length < this.lockTotal) {
this.search.pageNo++
@ -131,6 +135,41 @@
},
getBlur() {
this.focus = false
},
async toSearchDevice() {
if(this.bluetoothStatus !== 0) {
this.getBluetoothStatus()
return
}
let result = true
if(!this.isInitBluetooth) {
result = await this.initAndListenBluetooth()
}
if(result) {
this.routeJump({
name: 'searchDevice'
})
} else {
this.checkSetting()
}
},
async toLockDeatil(lock) {
if(this.bluetoothStatus !== 0) {
this.getBluetoothStatus()
return
}
let result = true
if(!this.isInitBluetooth) {
result = await this.initAndListenBluetooth()
}
if(result) {
this.updateCurrentLockInfo(lock)
this.routeJump({
name: 'lockDetail'
})
} else {
this.checkSetting()
}
}
}
}

View File

@ -0,0 +1,17 @@
<template>
<view>
</view>
</template>
<script>
export default {
data () {
return {}
}
}
</script>
<style lang="scss">
</style>

View File

@ -0,0 +1,17 @@
<template>
<view>
</view>
</template>
<script>
export default {
data () {
return {}
}
}
</script>
<style lang="scss">
</style>

View File

@ -51,6 +51,16 @@ const pages = [
name: 'webview',
path: '/pages/webview/webview',
tabBar: false
},
{
name: 'lockDetail',
path: '/pages/lockDetail/lockDetail',
tabBar: false
},
{
name: 'searchDevice',
path: '/pages/searchDevice/searchDevice',
tabBar: false
}
]

View File

@ -56,11 +56,36 @@ export const useBluetoothStore = defineStore('ble', {
// 当前锁信息
currentLockInfo: {},
// 消息序号
messageCount: 1
messageCount: 1,
// 是否初始化蓝牙
isInitBluetooth: false
}
},
actions: {
// 初始化蓝牙模块并监听蓝牙状态变化
// 初始化并监听
async initAndListenBluetooth() {
// 初始化蓝牙
const initResult = await this.initBluetooth()
if (initResult) {
// 更新蓝牙初始化状态
this.updateInitBluetooth(true)
// 监听蓝牙开关状态
this.onBluetoothState()
// 监听蓝牙连接状态
this.onBluetoothConnectStatus()
// 监听设备特征值变化
this.onBluetoothCharacteristicValueChange()
return true
} else {
return false
}
},
// 更新是否初始化蓝牙字段
updateInitBluetooth(value) {
this.isInitBluetooth = value
},
// 初始化蓝牙模块
initBluetooth() {
const that = this
// 初始化蓝牙模块
@ -69,7 +94,7 @@ export const useBluetoothStore = defineStore('ble', {
success() {
that.bluetoothStatus = 0
console.log('蓝牙初始化成功')
resolve()
resolve(true)
},
fail(err) {
console.log('蓝牙初始化失败', err)
@ -89,7 +114,12 @@ export const useBluetoothStore = defineStore('ble', {
that.bluetoothStatus = 2
}
}
resolve()
// 蓝牙已经初始化
if(err.errMsg === 'openBluetoothAdapter:fail already opened') {
resolve(true)
return
}
resolve(false)
}
})
})
@ -385,29 +415,35 @@ export const useBluetoothStore = defineStore('ble', {
// 检查小程序设置
checkSetting() {
const that = this
uni.getSetting({
async success(res) {
const bluetooth = res.authSetting['scope.bluetooth']
if(that.bluetoothStatus === -1) {
if(bluetooth) {
that.bluetoothStatus = 0
} else {
return new Promise((resolve) => {
uni.getSetting({
async success(res) {
const bluetooth = res.authSetting['scope.bluetooth']
if(that.bluetoothStatus === -1) {
if(bluetooth !== false) {
that.bluetoothStatus = 0
} else {
that.bluetoothStatus = 2
}
} else if(that.bluetoothStatus === 0 && bluetooth === false) {
that.bluetoothStatus = 2
} else if(that.bluetoothStatus === 1 && bluetooth === false) {
that.bluetoothStatus = 3
} else if(that.bluetoothStatus === 2 && bluetooth !== false) {
that.bluetoothStatus = 0
await that.initBluetooth()
} else if(that.bluetoothStatus === 3 && bluetooth !== false) {
that.bluetoothStatus = 1
await that.initBluetooth()
that.onBluetoothState()
}
} else if(that.bluetoothStatus === 0 && !bluetooth) {
that.bluetoothStatus = 2
} else if(that.bluetoothStatus === 1 && !bluetooth) {
that.bluetoothStatus = 3
} else if(that.bluetoothStatus === 2 && bluetooth) {
that.bluetoothStatus = 0
await that.initBluetooth()
} else if(that.bluetoothStatus === 3 && bluetooth) {
that.bluetoothStatus = 1
await that.initBluetooth()
that.onBluetoothState()
console.log('蓝牙权限', bluetooth, that.bluetoothStatus)
resolve(bluetooth)
},
fail() {
resolve(false)
}
console.log('蓝牙权限', bluetooth, that.bluetoothStatus)
}
})
})
},
// 连接蓝牙设备+获取设备服务+获取设备特征值

View File

@ -23,15 +23,15 @@ export const useUserStore = defineStore('user', {
},
async login() {
uni.setStorageSync('token', '3021|MZv7iEf0NwjCPSGx4QWs37zOjeVN3GrSJ2v7D56L7db1fcc5')
this.isLogin = true
const { code, data } = await getUserInfoRequest()
useLockStore().getLockList({
await useLockStore().getLockList({
pageNo: 1,
pageSize: 50
})
if(code === 0) {
this.updateUserInfo(data)
}
this.isLogin = true
return this.isLogin
}
}