197 lines
5.8 KiB
JavaScript
197 lines
5.8 KiB
JavaScript
/**
|
|
* @description 蓝牙数据持久化
|
|
*/
|
|
import { defineStore } from 'pinia'
|
|
|
|
// 定时器
|
|
let timer
|
|
|
|
export const useBluetoothStore = defineStore('ble', {
|
|
state() {
|
|
return {
|
|
/*
|
|
* 蓝牙状态
|
|
* -1 未知(初始状态)
|
|
* 0 正常
|
|
* 1 系统蓝牙未打开
|
|
* 2 小程序蓝牙功能被禁用
|
|
* 3 系统蓝牙未打开且小程序蓝牙功能被禁用
|
|
*/
|
|
bluetoothStatus: -1,
|
|
// 设备列表
|
|
deviceList: []
|
|
}
|
|
},
|
|
actions: {
|
|
// 初始化蓝牙模块并监听蓝牙状态变化
|
|
initBluetooth() {
|
|
const that = this
|
|
// 初始化蓝牙模块
|
|
return new Promise(resolve => {
|
|
uni.openBluetoothAdapter({
|
|
success() {
|
|
that.bluetoothStatus = 0
|
|
console.log('蓝牙初始化成功')
|
|
resolve()
|
|
},
|
|
fail(err) {
|
|
console.log('蓝牙初始化失败', err)
|
|
// 系统蓝牙未打开
|
|
if(err.errCode === 10001) {
|
|
if(that.bluetoothStatus === 2) {
|
|
that.bluetoothStatus = 3
|
|
} else {
|
|
that.bluetoothStatus = 1
|
|
}
|
|
}
|
|
// 小程序蓝牙功能被禁用
|
|
if(err.errno === 103) {
|
|
if(that.bluetoothStatus === 1) {
|
|
that.bluetoothStatus = 3
|
|
} else {
|
|
that.bluetoothStatus = 2
|
|
}
|
|
}
|
|
resolve()
|
|
}
|
|
})
|
|
})
|
|
},
|
|
// 监听蓝牙状态变化
|
|
onBluetoothState() {
|
|
const that = this
|
|
uni.onBluetoothAdapterStateChange((res) => {
|
|
console.log('蓝牙状态改变', res)
|
|
if(that.bluetoothStatus === 3 && res.available) {
|
|
that.bluetoothStatus = 2
|
|
} else if (that.bluetoothStatus === 2 && !res.available) {
|
|
that.bluetoothStatus = 3
|
|
} else if (that.bluetoothStatus === 1 && res.available) {
|
|
that.bluetoothStatus = 0
|
|
} else if (that.bluetoothStatus === 0 && !res.available){
|
|
that.bluetoothStatus = 1
|
|
} else if (that.bluetoothStatus === -1) {
|
|
if(res.available) {
|
|
that.bluetoothStatus = 0
|
|
} else {
|
|
that.bluetoothStatus = 1
|
|
}
|
|
}
|
|
})
|
|
},
|
|
// 开始搜索蓝牙设备
|
|
getBluetoothDevices() {
|
|
const that = this
|
|
if(this.bluetoothStatus !== 0) {
|
|
console.log('搜索未执行', this.bluetoothStatus)
|
|
this.getBluetoothStatus()
|
|
return
|
|
}
|
|
uni.startBluetoothDevicesDiscovery({
|
|
success: function (res) {
|
|
setTimeout(() => {
|
|
that.searchBluetoothDevices()
|
|
}, 300)
|
|
},
|
|
fail: async function (res) {
|
|
console.log('开始搜索失败', res)
|
|
if(res.errCode === 10000) {
|
|
// 重新初始化蓝牙适配器
|
|
await that.initBluetooth()
|
|
that.getBluetoothDevices()
|
|
}
|
|
}
|
|
})
|
|
},
|
|
// 定时查询蓝牙设备
|
|
searchBluetoothDevices() {
|
|
const that = this
|
|
if(this.bluetoothStatus !== 0) {
|
|
console.log('搜索未执行', this.bluetoothStatus)
|
|
this.getBluetoothStatus()
|
|
return
|
|
}
|
|
timer = setInterval(() => {
|
|
uni.getBluetoothDevices({
|
|
success(res) {
|
|
const deviceList = res.devices
|
|
that.deviceList = []
|
|
for(let i = 0; i < deviceList.length; i++) {
|
|
if(deviceList[i]?.advertisServiceUUIDs) {
|
|
const uuid = deviceList[i]?.advertisServiceUUIDs[0]
|
|
if(uuid && uuid.slice(2,8)==='758824' && uuid.slice(30,32)==='00') {
|
|
that.deviceList.push(deviceList[i])
|
|
}
|
|
}
|
|
}
|
|
console.log('设备列表', that.deviceList)
|
|
},
|
|
fail: async function (err) {
|
|
console.log('获取设备列表失败', err)
|
|
if(res.errCode === 10000) {
|
|
// 重新初始化蓝牙适配器
|
|
await that.initBluetooth()
|
|
}
|
|
}
|
|
})
|
|
}, 1000)
|
|
},
|
|
// 停止搜索蓝牙设备
|
|
stopGetBluetoothDevices() {
|
|
clearInterval(timer)
|
|
uni.stopBluetoothDevicesDiscovery()
|
|
},
|
|
// 蓝牙状态提示
|
|
getBluetoothStatus() {
|
|
if(this.bluetoothStatus === 1) {
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: '蓝牙尚未打开,请先打开蓝牙',
|
|
showCancel: false,
|
|
confirmText: '确定',
|
|
})
|
|
} else if(this.bluetoothStatus === 2 || this.bluetoothStatus === 3) {
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: '小程序蓝牙功能被禁用,请打开小程序蓝牙权限',
|
|
showCancel: false,
|
|
confirmText: '去设置',
|
|
success(res) {
|
|
if(res.confirm) {
|
|
uni.openSetting()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
},
|
|
// 检查小程序设置
|
|
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 {
|
|
that.bluetoothStatus = 2
|
|
}
|
|
} 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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|