wx.notifyBLECharacteristicValueChanged设置后没有效果,求大神指点!
现象:
蓝牙连接成功后,设备上传消息给小程序,小程序无法自动读取并提示,需要手动执行wx.readBLECharacteristicValue才会在wx.onBLECharacteristicValueChange方法中获得消息。手动执行存在每次点击都会读取Characteristic中的值,无法判断这个值是否已读。
设置步骤如下:
onLoad: function () {
wx.onBLECharacteristicValueChange(function(res) {//获得消息解码并输出
console.log('value:',"characteristic "+res.characteristicId+" as changed, now is "+res.value);
//console.log('value:',"value= "+res.value[0]+" "+res.value[1]);
let buffer = res.value
let dataView = new DataView(buffer)
console.log('value:',dataView.getUint8(0))
})
}
updateBTDevs: function(dev) {
console.log(JSON.stringify(dev));
var that = this;
if(this.btdevshash==null && dev["name"]=='Xinda'){
this.btdevshash = dev;
wx.createBLEConnection({ //连接蓝牙设备
deviceId: that.btdevshash["deviceId"],
success: function (res) {
console.log(res);
setTimeout(function () {
wx.notifyBLECharacteristicValueChanged({ //连接后设置监听
deviceId: that.btdevshash["deviceId"],
serviceId: "0000180f-0000-1000-8000-00805f9b34fb",
characteristicId: "00002a19-0000-1000-8000-00805f9b34fb",
state: true,
success: function (res) {
console.log('bind:',res);
},
fail: function (res) {
console.log("bind failed", res)
}
})
}.bind(this), 3000)
}
})
}
},
设置过程中,此Characteristic的属性notify可以修改,并成功修改为true,但是notify功能不工作。
于是增加按钮执行readBLECharacteristicValue做测试,每次点击按钮,可以在onBLECharacteristicValueChange函数中获得最新值(读取后再次读取还是这个值)
代码如下:
bindHelp: function() {
wx.readBLECharacteristicValue({
deviceId: this.btdevshash[“deviceId”],
serviceId: “0000180f-0000-1000-8000-00805f9b34fb”,
characteristicId: “00002a19-0000-1000-8000-00805f9b34fb”,
success: function (res) {
console.log(‘readBLECharacteristicValue:’, res)
}
})
},