feat: 完成蓝牙操作的适配
This commit is contained in:
parent
9dfd051415
commit
403ee37a09
1
App.vue
1
App.vue
@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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'
|
||||||
|
|||||||
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user