1. 修复蓝牙开关监听bug

This commit is contained in:
范鹏 2024-09-14 10:26:06 +08:00
parent d3364ecec5
commit 0de95780e6
2 changed files with 23 additions and 22 deletions

View File

@ -37,6 +37,8 @@
onLaunch: async function() { onLaunch: async function() {
// //
this.updateMiniProgram() this.updateMiniProgram()
//
this.onBluetoothState()
// //
const checkResult = await this.checkSetting() const checkResult = await this.checkSetting()
console.log(checkResult) console.log(checkResult)

View File

@ -121,8 +121,6 @@ export const useBluetoothStore = defineStore('ble', {
if (initResult) { if (initResult) {
// 更新蓝牙初始化状态 // 更新蓝牙初始化状态
this.updateInitBluetooth(true) this.updateInitBluetooth(true)
// 监听蓝牙开关状态
this.onBluetoothState()
// 监听蓝牙连接状态 // 监听蓝牙连接状态
this.onBluetoothConnectStatus() this.onBluetoothConnectStatus()
// 监听设备特征值变化 // 监听设备特征值变化
@ -542,30 +540,33 @@ export const useBluetoothStore = defineStore('ble', {
uni.stopBluetoothDevicesDiscovery() uni.stopBluetoothDevicesDiscovery()
}, },
// 蓝牙状态提示 // 蓝牙状态提示
getBluetoothStatus() { async getBluetoothStatus () {
if(this.bluetoothStatus === 1) { if (this.bluetoothStatus === 1) {
uni.showModal({ const initResult = await this.initBluetooth()
title: '提示', if (!initResult) {
content: '蓝牙尚未打开,请先打开蓝牙', uni.showModal({
showCancel: false, title: '提示',
success(res) { content: '蓝牙尚未打开,请先打开蓝牙',
if (res.confirm) { showCancel: false,
wx.openSystemBluetoothSetting({ success (res) {
success (res) { if (res.confirm) {
console.log(res) wx.openSystemBluetoothSetting({
} success (res) {
}) console.log(res)
}
})
}
} }
} })
}) }
} else if(this.bluetoothStatus === 2 || this.bluetoothStatus === 3) { } else if (this.bluetoothStatus === 2 || this.bluetoothStatus === 3) {
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '小程序蓝牙功能被禁用,请打开小程序蓝牙权限', content: '小程序蓝牙功能被禁用,请打开小程序蓝牙权限',
showCancel: false, showCancel: false,
confirmText: '去设置', confirmText: '去设置',
success(res) { success (res) {
if(res.confirm) { if (res.confirm) {
uni.openSetting() uni.openSetting()
} }
} }
@ -594,8 +595,6 @@ export const useBluetoothStore = defineStore('ble', {
await that.initBluetooth() await that.initBluetooth()
} else if(that.bluetoothStatus === 3 && bluetooth !== false) { } else if(that.bluetoothStatus === 3 && bluetooth !== false) {
that.bluetoothStatus = 1 that.bluetoothStatus = 1
await that.initBluetooth()
that.onBluetoothState()
} }
console.log('蓝牙权限', bluetooth, that.bluetoothStatus) console.log('蓝牙权限', bluetooth, that.bluetoothStatus)
resolve(bluetooth) resolve(bluetooth)