feat: 完成蓝牙操作的适配

This commit is contained in:
fanpeng 2025-06-09 17:37:43 +08:00
parent 9dfd051415
commit 403ee37a09
3 changed files with 89 additions and 55 deletions

View File

@ -49,7 +49,6 @@
this.onBluetoothState()
//
const checkResult = await this.checkSetting()
console.log(checkResult)
if (checkResult === true) {
this.initAndListenBluetooth(false)
}

View File

@ -130,8 +130,8 @@
nowTime: this.serverTimestamp,
localTime: timestamp
})
uni.hideLoading()
if (code !== 0) {
uni.hideLoading()
uni.showToast({
title: '连接失败,请靠近设备并保持设备处于唤醒状态',
icon: 'none'

View File

@ -190,7 +190,9 @@ export const useBluetoothStore = defineStore('ble', {
},
// 关闭全部蓝牙监听并关闭蓝牙模拟
closeAllBluetooth() {
// #ifdef MP
uni.offBluetoothAdapterStateChange()
// #endif
uni.closeBluetoothAdapter({
success(res) {
console.log('关闭蓝牙模块', res)
@ -858,7 +860,7 @@ export const useBluetoothStore = defineStore('ble', {
}
}
}
console.log('设备列表', that.deviceList)
// console.log('设备列表', that.deviceList)
},
async fail(res) {
console.log('获取设备列表失败', res)
@ -943,6 +945,9 @@ export const useBluetoothStore = defineStore('ble', {
}
})
// #endif
// #ifdef APP-PLUS
resolve(true)
// #endif
resolve(false)
})
},
@ -962,63 +967,74 @@ export const useBluetoothStore = defineStore('ble', {
success(res) {
console.log('连接成功', res)
// 获取设备服务
uni.getBLEDeviceServices({
deviceId: that.currentLockInfo.deviceId,
success(res) {
let serviceId
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].uuid.indexOf('FFF0') !== -1) {
serviceId = res.services[i].uuid
}
}
// 获取设备对应服务的特征值
uni.getBLEDeviceCharacteristics({
deviceId: that.currentLockInfo.deviceId,
serviceId,
success(res) {
let notifyCharacteristicId
let writeCharacteristicId
for (let i = 0; i < res.characteristics.length; i++) {
const characteristic = res.characteristics[i]
if (characteristic.properties.notify) {
notifyCharacteristicId = characteristic.uuid
}
if (characteristic.properties.write) {
writeCharacteristicId = characteristic.uuid
}
const executeBluetoothOperation = () => {
uni.getBLEDeviceServices({
deviceId: that.currentLockInfo.deviceId,
success(res) {
let serviceId
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].uuid.indexOf('FFF0') !== -1) {
serviceId = res.services[i].uuid
}
that.updateCurrentLockInfo({
...that.currentLockInfo,
serviceId,
notifyCharacteristicId,
writeCharacteristicId
})
that.notifyBluetoothCharacteristicValueChange()
resolve(true)
},
fail(res) {
if (res.errCode === 10006) {
uni.showToast({
title: '连接失败,请靠近设备并保持设备处于唤醒状态',
icon: 'none'
}
// 获取设备对应服务的特征值
uni.getBLEDeviceCharacteristics({
deviceId: that.currentLockInfo.deviceId,
serviceId,
success(res) {
let notifyCharacteristicId
let writeCharacteristicId
for (let i = 0; i < res.characteristics.length; i++) {
const characteristic = res.characteristics[i]
if (characteristic.properties.notify) {
notifyCharacteristicId = characteristic.uuid
}
if (characteristic.properties.write) {
writeCharacteristicId = characteristic.uuid
}
}
that.updateCurrentLockInfo({
...that.currentLockInfo,
serviceId,
notifyCharacteristicId,
writeCharacteristicId
})
that.notifyBluetoothCharacteristicValueChange()
resolve(true)
},
fail(res) {
if (res.errCode === 10006) {
uni.showToast({
title: '连接失败,请靠近设备并保持设备处于唤醒状态',
icon: 'none'
})
}
console.log('获取设备特征值失败', res)
resolve(false)
}
console.log('获取设备特征值失败', res)
resolve(false)
}
})
},
fail(res) {
if (res.errCode === 10006) {
uni.showToast({
title: '连接失败,请靠近设备并保持设备处于唤醒状态',
icon: 'none'
})
},
fail(res) {
if (res.errCode === 10006) {
uni.showToast({
title: '连接失败,请靠近设备并保持设备处于唤醒状态',
icon: 'none'
})
}
console.log('获取设备服务失败', res)
resolve(false)
}
console.log('获取设备服务失败', res)
resolve(false)
}
})
})
}
// #ifdef APP-PLUS
setTimeout(executeBluetoothOperation, 800)
// #endif
// #ifdef MP
executeBluetoothOperation()
// #endif
},
async fail(res) {
console.log('连接失败', res)
@ -1412,6 +1428,24 @@ export const useBluetoothStore = defineStore('ble', {
i === count - 1 ? binaryData.length : (i + 1) * 20
)
// #ifdef APP-PLUS
setTimeout(
() => {
uni.writeBLECharacteristicValue({
deviceId: that.currentLockInfo.deviceId,
serviceId: that.currentLockInfo.serviceId,
characteristicId: that.currentLockInfo.writeCharacteristicId,
value: writeData.buffer,
fail(res) {
console.log('写入失败', res)
}
})
},
(i + 1) * 200
)
// #endif
// #ifdef MP
uni.writeBLECharacteristicValue({
deviceId: that.currentLockInfo.deviceId,
serviceId: that.currentLockInfo.serviceId,
@ -1421,6 +1455,7 @@ export const useBluetoothStore = defineStore('ble', {
console.log('写入失败', res)
}
})
// #endif
}
}
},