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() this.onBluetoothState()
// //
const checkResult = await this.checkSetting() const checkResult = await this.checkSetting()
console.log(checkResult)
if (checkResult === true) { if (checkResult === true) {
this.initAndListenBluetooth(false) this.initAndListenBluetooth(false)
} }

View File

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

View File

@ -190,7 +190,9 @@ export const useBluetoothStore = defineStore('ble', {
}, },
// 关闭全部蓝牙监听并关闭蓝牙模拟 // 关闭全部蓝牙监听并关闭蓝牙模拟
closeAllBluetooth() { closeAllBluetooth() {
// #ifdef MP
uni.offBluetoothAdapterStateChange() uni.offBluetoothAdapterStateChange()
// #endif
uni.closeBluetoothAdapter({ uni.closeBluetoothAdapter({
success(res) { success(res) {
console.log('关闭蓝牙模块', res) console.log('关闭蓝牙模块', res)
@ -858,7 +860,7 @@ export const useBluetoothStore = defineStore('ble', {
} }
} }
} }
console.log('设备列表', that.deviceList) // console.log('设备列表', that.deviceList)
}, },
async fail(res) { async fail(res) {
console.log('获取设备列表失败', res) console.log('获取设备列表失败', res)
@ -943,6 +945,9 @@ export const useBluetoothStore = defineStore('ble', {
} }
}) })
// #endif // #endif
// #ifdef APP-PLUS
resolve(true)
// #endif
resolve(false) resolve(false)
}) })
}, },
@ -962,6 +967,7 @@ export const useBluetoothStore = defineStore('ble', {
success(res) { success(res) {
console.log('连接成功', res) console.log('连接成功', res)
// 获取设备服务 // 获取设备服务
const executeBluetoothOperation = () => {
uni.getBLEDeviceServices({ uni.getBLEDeviceServices({
deviceId: that.currentLockInfo.deviceId, deviceId: that.currentLockInfo.deviceId,
success(res) { success(res) {
@ -971,6 +977,7 @@ export const useBluetoothStore = defineStore('ble', {
serviceId = res.services[i].uuid serviceId = res.services[i].uuid
} }
} }
// 获取设备对应服务的特征值 // 获取设备对应服务的特征值
uni.getBLEDeviceCharacteristics({ uni.getBLEDeviceCharacteristics({
deviceId: that.currentLockInfo.deviceId, deviceId: that.currentLockInfo.deviceId,
@ -1019,6 +1026,15 @@ export const useBluetoothStore = defineStore('ble', {
resolve(false) resolve(false)
} }
}) })
}
// #ifdef APP-PLUS
setTimeout(executeBluetoothOperation, 800)
// #endif
// #ifdef MP
executeBluetoothOperation()
// #endif
}, },
async fail(res) { async fail(res) {
console.log('连接失败', res) console.log('连接失败', res)
@ -1412,6 +1428,9 @@ export const useBluetoothStore = defineStore('ble', {
i === count - 1 ? binaryData.length : (i + 1) * 20 i === count - 1 ? binaryData.length : (i + 1) * 20
) )
// #ifdef APP-PLUS
setTimeout(
() => {
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.currentLockInfo.deviceId, deviceId: that.currentLockInfo.deviceId,
serviceId: that.currentLockInfo.serviceId, serviceId: that.currentLockInfo.serviceId,
@ -1421,6 +1440,22 @@ export const useBluetoothStore = defineStore('ble', {
console.log('写入失败', res) console.log('写入失败', res)
} }
}) })
},
(i + 1) * 200
)
// #endif
// #ifdef MP
uni.writeBLECharacteristicValue({
deviceId: that.currentLockInfo.deviceId,
serviceId: that.currentLockInfo.serviceId,
characteristicId: that.currentLockInfo.writeCharacteristicId,
value: writeData.buffer,
fail(res) {
console.log('写入失败', res)
}
})
// #endif
} }
} }
}, },