writeBLECharacteristicValue 写入问题
发布于 5 年前 作者 gangma 16712 次浏览 来自 问答
//向低功耗蓝牙设备特征值中写入二进制数据
  writeBLECharacteristicValue: function (e) {
    var that = this;
    
    wx.onBLECharacteristicValueChange(function (characteristic) {
      that.setData({
        msg_3: JSON.stringify(characteristic)
      })
    })
    
    // 向蓝牙设备发送一个0x00的16进制数据
    let buffer = new ArrayBuffer(4)
    let dataView = new DataView(buffer)
    
    dataView.setUint8(0, 0x0D)
    dataView.setUint8(1, 0x02)
    dataView.setUint8(2, 0x11)
    dataView.setUint8(3, 0x13)
    
    wx.writeBLECharacteristicValue({
      deviceId: that.data.deviceId,
      serviceId: that.data.uuid,
      characteristicId: that.data.characteristicId,
      value: buffer,
      success: function (res) {
        that.setData({
          msg_2: JSON.stringify(res)
        })
      }
    })
  },

请问 value: buffer, 这样写对吗?

显示是成功写入的,但 onBLECharacteristicValueChange 没有回调信息

2 回复

你这样写多的吗?

onBLECharacteristicValueChange  是需要以下情况才会有回调

  1. 对支持notify属性的特征值调用notifyBLECharacteristicValueChange接口,并且设备上该特征值执行了notify

  2. 对支持read属性的特征值调用readBLECharacteristicValue接口,并且设备上该特征值返回了数据

回到顶部