完成小程序非低功耗模块的蓝牙封装
This commit is contained in:
parent
ac5c800bac
commit
8c44939bf7
15
App.vue
15
App.vue
@ -1,16 +1,21 @@
|
||||
<script>
|
||||
import { useBluetoothStore } from '@/stores/bluetooth'
|
||||
import { mapState, mapActions } from 'pinia'
|
||||
|
||||
export default {
|
||||
computed: {
|
||||
...mapState(useBluetoothStore, ['bluetoothStatus'])
|
||||
},
|
||||
onLaunch: function() {
|
||||
console.log('App Launch')
|
||||
this.updateMiniProgram()
|
||||
this.initBluetooth()
|
||||
this.onBluetoothState()
|
||||
},
|
||||
onShow: function() {
|
||||
console.log('App Show')
|
||||
},
|
||||
onHide: function() {
|
||||
console.log('App Hide')
|
||||
this.checkSetting()
|
||||
},
|
||||
methods: {
|
||||
...mapActions(useBluetoothStore, ['initBluetooth', 'onBluetoothState', 'updateBluetoothStatus', 'checkSetting']),
|
||||
// 强制升级
|
||||
updateMiniProgram() {
|
||||
const updateManager = uni.getUpdateManager()
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name" : "wx-starlock",
|
||||
"appid" : "",
|
||||
"appid" : "__UNI__933D519",
|
||||
"description" : "",
|
||||
"versionName" : "1.0.0",
|
||||
"versionCode" : "100",
|
||||
@ -50,9 +50,10 @@
|
||||
"quickapp" : {},
|
||||
/* 小程序特有相关 */
|
||||
"mp-weixin" : {
|
||||
"appid" : "",
|
||||
"appid" : "wx9829a39e65550757",
|
||||
"setting" : {
|
||||
"urlCheck" : false
|
||||
"urlCheck" : true,
|
||||
"minified" : true
|
||||
},
|
||||
"usingComponents" : true
|
||||
},
|
||||
|
||||
15
pages.json
15
pages.json
@ -9,23 +9,18 @@
|
||||
},
|
||||
"pages": [
|
||||
{
|
||||
"path": "pages/demo/demo",
|
||||
"style": {
|
||||
"navigationBarTitleText": "uni-app"
|
||||
}
|
||||
"path": "pages/index/index"
|
||||
},
|
||||
{
|
||||
"path": "pages/index/index",
|
||||
"style": {
|
||||
"navigationBarTitleText": "uni-app"
|
||||
}
|
||||
"path": "pages/demo/demo"
|
||||
}
|
||||
],
|
||||
"globalStyle": {
|
||||
"navigationBarTextStyle": "black",
|
||||
"navigationBarTitleText": "uni-app",
|
||||
"navigationBarTitleText": "星星锁",
|
||||
"navigationBarBackgroundColor": "#F8F8F8",
|
||||
"backgroundColor": "#F8F8F8"
|
||||
"backgroundColor": "#F8F8F8",
|
||||
"navigationStyle": "custom"
|
||||
},
|
||||
"uniIdRouter": {}
|
||||
}
|
||||
|
||||
@ -1,68 +1,45 @@
|
||||
<template>
|
||||
<view class="content">
|
||||
<image class="logo" src="/static/logo.png"></image>
|
||||
<view class="text-area">
|
||||
<text class="title">{{title}}{{userInfo.name}}</text>
|
||||
</view>
|
||||
<up-button type="primary" :plain="true" text="镂空"></up-button>
|
||||
<up-button class="button" type="primary" text="添加设备" @click="getList"></up-button>
|
||||
<up-button class="button" type="error" text="停止搜索" @click="stopGetList"></up-button>
|
||||
<view v-for="(item, index) in deviceList" :key="item.deviceId">{{item.name}}</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { useUserStore } from '../../stores/user.js'
|
||||
import { useBluetoothStore } from '@/stores/bluetooth'
|
||||
import { mapState, mapActions } from 'pinia'
|
||||
import { rgbToHex } from 'uview-plus'
|
||||
import { login } from '../../api/user.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
title: 'Hello'
|
||||
}
|
||||
return {}
|
||||
},
|
||||
computed: {
|
||||
// 允许读取this.userInfo
|
||||
...mapState(useUserStore, ['userInfo'])
|
||||
},
|
||||
async onLoad () {
|
||||
this.update()
|
||||
const data = await login({ code: '123' })
|
||||
console.log(data)
|
||||
console.log(rgbToHex('rgb(13, 145, 20)'))
|
||||
console.log(111, uni.$u.config.v)
|
||||
...mapState(useBluetoothStore, ['deviceList'])
|
||||
},
|
||||
async onLoad () {},
|
||||
methods: {
|
||||
// 允许调用this.updateUserInfo()
|
||||
...mapActions(useUserStore, ['updateUserInfo']),
|
||||
update() {
|
||||
this.updateUserInfo({ name: '123' })
|
||||
console.log(111, this.userInfo)
|
||||
...mapActions(useBluetoothStore, ['getBluetoothDevices', 'stopGetBluetoothDevices']),
|
||||
getList() {
|
||||
this.getBluetoothDevices()
|
||||
},
|
||||
stopGetList() {
|
||||
this.stopGetBluetoothDevices()
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
<style lang="scss" scoped>
|
||||
.content {
|
||||
padding-top: 200rpx;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 200rpx;
|
||||
width: 200rpx;
|
||||
margin: 200rpx auto 50rpx;
|
||||
.button {
|
||||
width: 400rpx;
|
||||
}
|
||||
|
||||
.text-area {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 36rpx;
|
||||
color: #8f8f94;
|
||||
}
|
||||
</style>
|
||||
|
||||
203
stores/bluetooth.js
Normal file
203
stores/bluetooth.js
Normal file
@ -0,0 +1,203 @@
|
||||
/**
|
||||
* @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) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '小程序蓝牙功能被禁用,请打开小程序蓝牙权限',
|
||||
showCancel: false,
|
||||
confirmText: '去设置',
|
||||
success(res) {
|
||||
if(res.confirm) {
|
||||
uni.openSetting()
|
||||
}
|
||||
}
|
||||
})
|
||||
} else if(this.bluetoothStatus === 3) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '蓝牙尚未打开且小程序蓝牙功能被禁用,请先打开蓝牙并打开小程序蓝牙权限',
|
||||
showCancel: false,
|
||||
confirmText: '确定',
|
||||
})
|
||||
}
|
||||
},
|
||||
// 检查小程序设置
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user