收藏
回答

低功耗蓝牙 writeBLECharacteristicValue接口 无任何回调?

基础库版本:2.24.1

微信版本:8.0.22

手机型号:MI 9

安卓版本:9

描述:

writeBLECharacteristicValue 写入接口调用后无任何回调。success,fail,complete 都没有回调。onBLECharacteristicValueChange 也没有触发

但数据似乎写入进去了。通过 readBLECharacteristicValue 读取到的 特征value 为我写入进去的值。虽然我不是要我写入的值,而是要通过写入 让设备反馈给我数据。

readBLECharacteristicValue 有回调 success ,fail,complete,onBLECharacteristicValueChange 都能触发。

真机调试截图:

部分代码:

    getBLEDeviceServices(deviceId) {    
        wx.getBLEDeviceServices({
              deviceId,
              success(res) => {
                for (let i = 0; i < res.services.length; i++) {
                  if (res.services[i].isPrimary) {
                    console.log(res.services[i])
                  }
                }
                // this.getBLEDeviceCharacteristics(deviceId, res.services[4].uuid)
            this.getBLEDeviceCharacteristics(deviceId, res.services[2].uuid)
        this.getBLEDeviceCharacteristics(deviceId, res.services[3].uuid)
        this.getBLEDeviceCharacteristics(deviceId, res.services[4].uuid)
        this.getBLEDeviceCharacteristics(deviceId, res.services[5].uuid)
      }
    })
  },
  getBLEDeviceCharacteristics(deviceId, serviceId) {
    wx.getBLEDeviceCharacteristics({
      deviceId,
      serviceId,
      success(res) => {
        console.log('getBLEDeviceCharacteristics success', res.characteristics)
        for (let i = 0; i < res.characteristics.length; i++) {
          let item = res.characteristics[i]
          console.log(item)
          if (item.properties.read) {
            wx.readBLECharacteristicValue({
              deviceId,
              serviceId,
              characteristicId: item.uuid,
              success(res) => {
                console.log(res)
                console.log('读取成功')
              }
            })
          }
          if (item.properties.write) {
            this.setData({
              canWritetrue
            })


            this._deviceId = deviceId
            this._serviceId = serviceId
            this._characteristicId = item.uuid
            let data = 0
            if (serviceId === '0000FFF0-0000-1000-8000-00805F9B34FB') {
              data = 0xff
            }
            if (serviceId === '0000FFE0-0000-1000-8000-00805F9B34FB') {
              data = 0x11
            }
            if (serviceId === '0000FFB1-0000-1000-8000-00805F9B34FB') {
              data = 0x14
            }
            this.writeBLECharacteristicValue(data)
          }
          if (item.properties.notify || item.properties.indicate) {
            wx.notifyBLECharacteristicValueChange({
              deviceId,
              serviceId,
              characteristicId: item.uuid,
              statetrue,
            })
          }
        }
      },
      fail(res) {
        console.error('getBLEDeviceCharacteristics', res)
      }
    })
    // 操作之前先监听,保证第一时间获取数据
    wx.onBLECharacteristicValueChange((characteristic) => {
      console.log('接受')
      console.log(characteristic)
      const idx = inArray(this.data.chs, 'uuid', characteristic.characteristicId)
      const data = {}
      if (idx === -1) {
        data[`chs[${this.data.chs.length}]`] = {
          uuid: characteristic.characteristicId,
          value: ab2hex(characteristic.value)
        }
      } else {
        data[`chs[${idx}]`] = {
          uuid: characteristic.characteristicId,
          value: ab2hex(characteristic.value)
        }
      }
      // data[`chs[${this.data.chs.length}]`] = {
      //   uuid: characteristic.characteristicId,
      //   value: ab2hex(characteristic.value)
      // }
      this.setData(data)
    })
  },
  writeBLECharacteristicValue(data) {
    // 向蓝牙设备发送一个0x00的16进制数据
    let buffer = new ArrayBuffer(1)
    let dataView = new DataView(buffer)
    data = data || 0x10
    dataView.setUint8(0, data)
    console.log('写入数据')
    console.log(buffer)


    wx.writeBLECharacteristicValue({
      deviceIdthis._deviceId,
      serviceIdthis._serviceId,
      characteristicIdthis._characteristicId,
      writeType'write',
      value: buffer,
      success() => {
        console.log('成功!')
      },
      fail() => {
        console.log('失败!')
      },
      complete() => {
        console.log('结束!')
      }
    })
  },
回答关注问题邀请回答
收藏

3 个回答

  • Demons
    Demons
    2022-05-16

    你好,麻烦在手机微信那里上传下日志: 我->设置->帮助与反馈右上角有个上报日志的入口,麻烦提供一下微信号,时间点

    2022-05-16
    有用
    回复 3
    • Demons
      Demons
      2022-05-16
      你好,请提供一下最近三天内的日志谢谢你
      2022-05-16
      回复
    • 陈晓枫~云闲
      陈晓枫~云闲
      2022-05-30
      我的程序也是这种情况
      2022-05-30
      回复
    • 王妙端
      王妙端
      2022-08-05回复Demons
      我的也是这种情况
      2022-08-05
      回复
  • 我好哇塞
    我好哇塞
    2022-12-19

    楼主解决了吗 我的也是这样

    2022-12-19
    有用
    回复
  • 野猪佩琪
    野猪佩琪
    2022-05-09

    2022-05-09
    有用
    回复
登录 后发表内容