基础库 1.1.0 开始支持,低版本需做兼容处理
监听低功耗蓝牙设备的特征值变化。必须先启用notify
接口才能接收到设备推送的notification。
CALLBACK参数说明:
参数 |
类型 |
说明 |
deviceId |
String |
蓝牙设备 id,参考 device 对象 |
serviceId |
String |
特征值所属服务 uuid |
characteristicId |
String |
特征值 uuid |
value |
ArrayBuffer |
特征值最新的值 (注意:vConsole 无法打印出 ArrayBuffer 类型数据) |
示例代码:
function ab2hex(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('');
}
wx.onBLECharacteristicValueChange(function(res) {
console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
console.log(ab2hext(res.value))
})