1. 修复安卓手机蓝牙搜索需要定位权限的问题

2. 删除以前的蓝牙操作demo
This commit is contained in:
范鹏 2024-08-29 16:18:49 +08:00
parent 5129d3624a
commit 86c398b754
3 changed files with 17 additions and 208 deletions

View File

@ -21,9 +21,6 @@
{
"path": "pages/mine/mine"
},
{
"path": "pages/index/index"
},
{
"path": "pages/userInfo/userInfo",
"style": {

View File

@ -1,205 +0,0 @@
<template>
<view class="content">
<view v-if="!showConnect">
<button class="button" v-if="showAdd" @click="getList">添加设备</button>
<button class="button" v-else @click="stopGetList">停止搜索</button>
<view class="device-list" v-for="(item, index) in deviceList" :key="item.deviceId" @click="connect(item)">{{item.name}}</view>
</view>
<view v-else>
<button class="button" @click="bindUser">添加用户</button>
<button class="button" @click="openDoorOperate">开门</button>
<button class="button" @click="closeDoorOperate">关门</button>
<button class="button" @click="reset">恢复出厂设置</button>
<button class="button" @click="getLockStatusResult">获取锁状态</button>
<button class="button" @click="resetPassword">重置锁密码</button>
<button class="button" @click="setPassword">设置密码</button>
<button class="button" @click="deletePassword">删除密码</button>
<view>名称{{currentLockInfo.name}}</view>
<view>设备Id{{currentLockInfo.deviceId}}</view>
</view>
</view>
</template>
<script>
import { useBluetoothStore } from '@/stores/bluetooth'
import { mapState, mapActions } from 'pinia'
export default {
data() {
return {
showAdd: true,
showConnect: false,
keyId: '0',
authUid: '294',
onlineToken: '0'
}
},
computed: {
...mapState(useBluetoothStore, ['deviceList', 'currentLockInfo'])
},
onLoad () {},
methods: {
...mapActions(useBluetoothStore, ['getBluetoothDevices', 'stopGetBluetoothDevices', 'updateCurrentLockInfo',
'connectBluetoothDevice', 'getPublicKey', 'getCommKey', 'getLockStatus', 'addLockUser', 'timestampToArray',
'openDoor', 'resetDevice', 'resetLockPassword', 'setLockPassword']),
async deletePassword() {
const timestamp = parseInt(new Date().getTime() / 1000)
const data = await this.setLockPassword({
keyId: this.keyId,
uid: this.authUid,
pwdNo: 2,
operate: 2,
isAdmin: 1,
pwd: '000000',
userCountLimit: 0,
startTime: timestamp,
endTime: timestamp
})
console.log('设置密码返回', data)
},
async setPassword() {
const timestamp = parseInt(new Date().getTime() / 1000)
const endTimestamp = timestamp + 3600 * 24 * 365
const data = await this.setLockPassword({
keyId: this.keyId,
uid: this.authUid,
pwdNo: 1,
operate: 0,
isAdmin: 1,
pwd: '000000',
userCountLimit: 0xffff,
startTime: timestamp,
endTime: endTimestamp
})
console.log('设置密码返回', data)
},
async resetPassword() {
const { code } = await this.resetLockPassword({
uid: this.authUid,
keyId: this.keyId
})
console.log('重置密码返回', code)
},
async openDoorOperate() {
const { code } = await this.openDoor({
name: this.currentLockInfo.name,
uid: this.authUid,
openMode: 0,
openTime: parseInt(new Date().getTime() / 1000),
onlineToken: this.onlineToken
})
console.log('开门返回', code)
},
async closeDoorOperate() {
const { code } = await this.openDoor({
name: this.currentLockInfo.name,
uid: this.authUid,
openMode: 32,
openTime: parseInt(new Date().getTime() / 1000),
onlineToken: this.onlineToken
})
console.log('关门返回', code)
},
async reset() {
const { code } = await this.resetDevice({
name: this.currentLockInfo.name,
authUid: this.authUid,
keyId: this.keyId
})
console.log('恢复出厂设置返回', code)
},
getList() {
this.getBluetoothDevices()
this.showAdd = false
},
stopGetList() {
this.stopGetBluetoothDevices()
this.showAdd = true
},
async connect(item) {
this.updateCurrentLockInfo({
name: item.name,
deviceId: item.deviceId
})
const result = await this.connectBluetoothDevice()
if(result) {
this.showConnect = true
this.stopGetBluetoothDevices()
}
},
async bindUser() {
const { code } = await this.getPublicKey(this.currentLockInfo.name)
console.log('获取公钥返回', code)
if(code !== 0) {
return
}
const { code: getCommKeyCode } = await this.getCommKey(this.currentLockInfo.name, this.keyId, this.authUid,
parseInt(new Date().getTime() / 1000))
console.log('获取私钥返回', getCommKeyCode)
if(getCommKeyCode !== 0) {
return
}
const timestamp = parseInt(new Date().getTime() / 1000)
const { code: addUserCode } = await this.addLockUser({
name: this.currentLockInfo.name,
keyId: this.keyId,
authUid: this.authUid,
uid: this.authUid,
publicKey: this.currentLockInfo.publicKey,
commKey: this.currentLockInfo.commKey,
openMode: 1,
keyType: 1,
startDate: timestamp,
expireDate: 0xffffffff,
useCountLimit: 0xffff,
isRound: 0,
weekRound: 0,
startHour: 0,
startMin: 0,
endHour: 0,
endMin: 0,
role: 0xff,
password: (Math.floor(Math.random() * 900000) + 100000).toString()
})
console.log('添加用户返回', addUserCode)
},
getLockStatusResult() {
const timestamp = parseInt(new Date().getTime() / 1000)
this.getLockStatus({
name: this.currentLockInfo.name,
uid: this.authUid,
nowTime: timestamp,
localTime: timestamp
})
}
}
}
</script>
<style lang="scss" scoped>
.content {
padding-top: 160rpx;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.button {
margin-top: 40rpx;
width: 400rpx;
height: 108rpx;
line-height: 108rpx;
background: #3F536E;
color: #ffffff !important;
}
.device-list {
margin-top: 10rpx;
height: 100rpx;
width: 500rpx;
line-height: 100rpx;
text-align: center;
border: #dd524d solid 2rpx;
}
</style>

View File

@ -373,6 +373,19 @@ export const useBluetoothStore = defineStore('ble', {
},
fail: async function (res) {
console.log('开始搜索失败', res)
if(res.errno === 1509008) {
uni.showModal({
title: '提示',
content: '安卓手机蓝牙功能需要定位权限,请前往设置开启微信的定位权限后再试',
showCancel: false,
confirmText: '重试',
success(res) {
if (res.confirm) {
that.getBluetoothDevices()
}
}
})
}
if(res.errCode === 10000) {
// 重新初始化蓝牙适配器
await that.initBluetooth()
@ -550,6 +563,10 @@ export const useBluetoothStore = defineStore('ble', {
})
},
fail(res) {
if(res.errno === 1509007) {
resolve(true)
return
}
uni.showToast({
title: '连接失败,请靠近设备并保持设备处于唤醒状态',
icon: 'none'