title: Taro.onBLECharacteristicValueChange(callback)

sidebar_label: onBLECharacteristicValueChange

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

参考文档

类型

  1. (callback: Callback) => void

参数

Callback

低功耗蓝牙设备的特征值变化事件的回调函数

  1. (result: CallbackResult) => void
参数 类型
result CallbackResult

CallbackResult

参数 类型 说明
characteristicId string 蓝牙特征值的 uuid
deviceId string 蓝牙设备 id
serviceId string 蓝牙特征值对应服务的 uuid
value ArrayBuffer 特征值最新的值

示例代码

  1. // ArrayBuffer转16进制字符串示例
  2. function ab2hex(buffer) {
  3. let hexArr = Array.prototype.map.call(
  4. new Uint8Array(buffer),
  5. function(bit) {
  6. return ('00' + bit.toString(16)).slice(-2)
  7. }
  8. )
  9. return hexArr.join('');
  10. }
  11. Taro.onBLECharacteristicValueChange(function (res) {
  12. console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
  13. console.log(ab2hex(res.value))
  14. })

API 支持度

API 微信小程序 H5 React Native
Taro.onBLECharacteristicValueChange ✔️