注册

小程序开发wx.onBLECharacteristicValueChange,监听低功耗蓝牙设备的特征值变化

2017-12-11
导读:基础库 1.1.0 开始支持,低版本需做 兼容处理 监听低功耗蓝牙设备的特征值变化。必须先启用 notify 接口才能接收到设备推送的notification。 CALLBACK参数说明: 参数 类型 说明 deviceId Str...

基础库 1.1.0 开始支持,低版本需做兼容处理

监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification。

CALLBACK参数说明:

参数 类型 说明
deviceId String 蓝牙设备 id,参考 device 对象
serviceId String 特征值所属服务 uuid
characteristicId String 特征值 uuid
value ArrayBuffer 特征值最新的值 (注意:vConsole 无法打印出 ArrayBuffer 类型数据)

示例代码:

// ArrayBuffer转16进度字符串示例
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))
})
重磅推荐:小程序开店目录

第一部分:小商店是什么

第二部分:如何开通一个小商店

第三部分:如何登录小商店

第四部分:开店任务常见问题

第五部分:小商店可以卖什么

第六部分:HiShop小程序特色功能

第七部分:小程序直播

第八部分:小程序收货/物流

第九部分:小程序怎么结算

第十部分:小程序客服

第十一部分:电商创业

第十二部分:小程序游戏开发