小程序接收蓝牙返回的参数wx.onBLECharacteristicValueChange
监听低功耗蓝牙设备的特征值变化事件。必须先启用
notifyBLECharacteristicValueChange
接口才能接收到设备推送的 notification。
参数
function callback
低功耗蓝牙设备的特征值变化事件的回调函数
参数
Object res
//监听设备特征值变化
getonBLECharacteristicValueChange(){
console.log('监听设备特征值变化')
//授权之后即可监听特征值变化
wx.onBLECharacteristicValueChange((onNotityChangeRes)=>{
console.log('特征值改变:: ', onNotityChangeRes)
let characteristicValue = this.buf2hex(onNotityChangeRes.value)
console.log('更新后的特征值(16进制格式)',characteristicValue)
let buffer = this.readData(onNotityChangeRes.value)
console.log("更新后的buffer", buffer)
if(buffer){
this.changeBuffer(buffer,onNotityChangeRes.value)
}
})
},
readData(buffer){
let dateView = new DataView(buffer);
let hexArr = buf2hex(buffer);
// console.log("hexArr - ", hexArr);
},
buf2hex(){
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit){
return ('00' + bit.toString(16)).slice(-2)
})
return hexArr.join('');
},