基础库版本:2.24.1
微信版本:8.0.22
手机型号:MI 9
安卓版本:9
描述:
writeBLECharacteristicValue 写入接口调用后无任何回调。success,fail,complete 都没有回调。onBLECharacteristicValueChange 也没有触发
但数据似乎写入进去了。通过 readBLECharacteristicValue 读取到的 特征value 为我写入进去的值。虽然我不是要我写入的值,而是要通过写入 让设备反馈给我数据。
readBLECharacteristicValue 有回调 success ,fail,complete,onBLECharacteristicValueChange 都能触发。
真机调试截图:
部分代码:
getBLEDeviceServices(deviceId) {
wx.getBLEDeviceServices({
deviceId,
success: (res) => {
for (let i = 0; i < res.services.length; i++) {
if (res.services[i].isPrimary) {
console.log(res.services[i])
}
}
// this.getBLEDeviceCharacteristics(deviceId, res.services[4].uuid)
this.getBLEDeviceCharacteristics(deviceId, res.services[2].uuid)
this.getBLEDeviceCharacteristics(deviceId, res.services[3].uuid)
this.getBLEDeviceCharacteristics(deviceId, res.services[4].uuid)
this.getBLEDeviceCharacteristics(deviceId, res.services[5].uuid)
}
})
},
getBLEDeviceCharacteristics(deviceId, serviceId) {
wx.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success: (res) => {
console.log('getBLEDeviceCharacteristics success', res.characteristics)
for (let i = 0; i < res.characteristics.length; i++) {
let item = res.characteristics[i]
console.log(item)
if (item.properties.read) {
wx.readBLECharacteristicValue({
deviceId,
serviceId,
characteristicId: item.uuid,
success: (res) => {
console.log(res)
console.log('读取成功')
}
})
}
if (item.properties.write) {
this.setData({
canWrite: true
})
this._deviceId = deviceId
this._serviceId = serviceId
this._characteristicId = item.uuid
let data = 0
if (serviceId === '0000FFF0-0000-1000-8000-00805F9B34FB') {
data = 0xff
}
if (serviceId === '0000FFE0-0000-1000-8000-00805F9B34FB') {
data = 0x11
}
if (serviceId === '0000FFB1-0000-1000-8000-00805F9B34FB') {
data = 0x14
}
this.writeBLECharacteristicValue(data)
}
if (item.properties.notify || item.properties.indicate) {
wx.notifyBLECharacteristicValueChange({
deviceId,
serviceId,
characteristicId: item.uuid,
state: true,
})
}
}
},
fail(res) {
console.error('getBLEDeviceCharacteristics', res)
}
})
// 操作之前先监听,保证第一时间获取数据
wx.onBLECharacteristicValueChange((characteristic) => {
console.log('接受')
console.log(characteristic)
const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId)
const data = {}
if (idx === -1) {
data[`chs[${this.data.chs.length}]`] = {
uuid: characteristic.characteristicId,
value: ab2hex(characteristic.value)
}
} else {
data[`chs[${idx}]`] = {
uuid: characteristic.characteristicId,
value: ab2hex(characteristic.value)
}
}
// data[`chs[${this.data.chs.length}]`] = {
// uuid: characteristic.characteristicId,
// value: ab2hex(characteristic.value)
// }
this.setData(data)
})
},
writeBLECharacteristicValue(data) {
// 向蓝牙设备发送一个0x00的16进制数据
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
data = data || 0x10
dataView.setUint8(0, data)
console.log('写入数据')
console.log(buffer)
wx.writeBLECharacteristicValue({
deviceId: this._deviceId,
serviceId: this._serviceId,
characteristicId: this._characteristicId,
writeType: 'write',
value: buffer,
success: () => {
console.log('成功!')
},
fail: () => {
console.log('失败!')
},
complete: () => {
console.log('结束!')
}
})
},
你好,麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点
楼主解决了吗 我的也是这样
哎