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
|
||||||
})
|
})
|
||||||
|
uni.hideLoading()
|
||||||
if (code !== 0) {
|
if (code !== 0) {
|
||||||
uni.hideLoading()
|
|
||||||
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,63 +967,74 @@ export const useBluetoothStore = defineStore('ble', {
|
|||||||
success(res) {
|
success(res) {
|
||||||
console.log('连接成功', res)
|
console.log('连接成功', res)
|
||||||
// 获取设备服务
|
// 获取设备服务
|
||||||
uni.getBLEDeviceServices({
|
const executeBluetoothOperation = () => {
|
||||||
deviceId: that.currentLockInfo.deviceId,
|
uni.getBLEDeviceServices({
|
||||||
success(res) {
|
deviceId: that.currentLockInfo.deviceId,
|
||||||
let serviceId
|
success(res) {
|
||||||
for (let i = 0; i < res.services.length; i++) {
|
let serviceId
|
||||||
if (res.services[i].uuid.indexOf('FFF0') !== -1) {
|
for (let i = 0; i < res.services.length; i++) {
|
||||||
serviceId = res.services[i].uuid
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
that.updateCurrentLockInfo({
|
}
|
||||||
...that.currentLockInfo,
|
|
||||||
serviceId,
|
// 获取设备对应服务的特征值
|
||||||
notifyCharacteristicId,
|
uni.getBLEDeviceCharacteristics({
|
||||||
writeCharacteristicId
|
deviceId: that.currentLockInfo.deviceId,
|
||||||
})
|
serviceId,
|
||||||
that.notifyBluetoothCharacteristicValueChange()
|
success(res) {
|
||||||
resolve(true)
|
let notifyCharacteristicId
|
||||||
},
|
let writeCharacteristicId
|
||||||
fail(res) {
|
for (let i = 0; i < res.characteristics.length; i++) {
|
||||||
if (res.errCode === 10006) {
|
const characteristic = res.characteristics[i]
|
||||||
uni.showToast({
|
if (characteristic.properties.notify) {
|
||||||
title: '连接失败,请靠近设备并保持设备处于唤醒状态',
|
notifyCharacteristicId = characteristic.uuid
|
||||||
icon: 'none'
|
}
|
||||||
|
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) {
|
async fail(res) {
|
||||||
console.log('连接失败', res)
|
console.log('连接失败', res)
|
||||||
@ -1412,6 +1428,24 @@ 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({
|
||||||
|
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({
|
uni.writeBLECharacteristicValue({
|
||||||
deviceId: that.currentLockInfo.deviceId,
|
deviceId: that.currentLockInfo.deviceId,
|
||||||
serviceId: that.currentLockInfo.serviceId,
|
serviceId: that.currentLockInfo.serviceId,
|
||||||
@ -1421,6 +1455,7 @@ export const useBluetoothStore = defineStore('ble', {
|
|||||||
console.log('写入失败', res)
|
console.log('写入失败', res)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// #endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user