收藏
回答

利用wx.onBLECharacteristicValueChange监听的值来做应答但是只能接收?

  /*蓝牙升级通讯协议(小程序与单片机的应答)

------------------------------------------------------------------------------------------------------------------

* 五、开始下载后小程序自动重复发送每一组数据,直到收到单片机应答信息后,开始连续下载;

* 六、单片机每收到一组数据,计算校验和,如果正确则应答接收正确,否则应答接收错误;

 * 七、接收正确应答:2B 4F 4B CF 75

 * 八、接收错误应答:2D 45 52 52 00 A4

  * 九、全部下载结束后停止

------------------------------------------------------------------------------------------------------------------

就是我这边只能监听变化的值,却不能通过获取硬件发送过来这个值进行发送,求大佬们怎么解决这是我的部分代码

 wx.onBLECharacteristicValueChange(function (res{
              console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`99999999999999999999)
              console.log(ab2hex(res.value)) // ArrayBuffer转16进制字符串
              console.log(res.value)
              that.setData({
                valueChange: valueChangeData + "|" + ab2hex(res.value), //前端显示蓝牙接收数据
              })
              valueChangeData = valueChangeData + '|' + ab2hex(res.value);
              console.log("valueChangeData", valueChangeData)
              var a = [];
              a.push(valueChangeData)
              console.log(a, "A")
              var b = (a.toString()).split('|')
              console.log(b)
              valueChangeDataArray = b//定义的全局变量
              // that.testSend1()
              // that.writePackageData1()


              var success = [437975207117];
              var successArrayBuffer = new Uint8Array(success).buffer;
              console.log(successArrayBuffer)
              var fail = [454552520164]
              var failArrayBuffer = new Uint8Array(fail).buffer;
              console.log(failArrayBuffer)
              if (ab2hex(res.value) == ab2hex(successArrayBuffer)) {
                console.log("接收正确应答")
               // that.testSend1()
                //that.package20Write()
              } else if (ab2hex(res.value) == ab2hex(failArrayBuffer)) {
                console.log("接收错误应答,重新回调")
               // that.testSend2()
              }
            })
          },


testSend1() {
    this.writeRealData1(deviceIdConnected, serviceIdConnected, characteristicsIdConnected, sendFile)
  },
writeRealData1(deviceId, serviceId, characteristicId, packageData) {
    const arrays = packageData;
    console.log(arrays, "所有的1k")
    var valueChange = valueChangeDataArray; //监听的数组
    var index;
    if (valueChange instanceof Array) {
      valueChange.splice(01);
      if (valueChange.length % 57 == 0) {
        index = valueChange.length / 57
      } else {
        index = parseInt(valueChange.length / 57)
      }
    } else if (valueChange == undefined) {
      index = 0
    }
    console.log(valueChange)
    console.log(index, "长度")
    var success = "2b4f4bcf75" //监听成功的值
    var fail = "2d4552520a4" //监听失败的值
    const data = arrays[index] || null,
      self = this
    console.log("第", index, "包数据", data)
    self.writePackageData1(deviceId, serviceId, characteristicId, data)
  },
  writePackageData1(deviceId, serviceId, characteristicId, Data) {
    /** hex2int(hex)字符串转16进制 */
    /**concatenate(resultConstructor, ...arrays)合并arraybuffer */

    /////////////////校检和
    //////////////////////
    function CRC(array{
      var len = array.length,
        sum = 0;
      for (let i = 0; i < len; i++) {
        sum += array[i]
      }
      return sum.toString(16)
    }
    ///////////////////////
    ////////////////////////
    var valueChange = valueChangeDataArray; //监听的数组
    console.log(valueChange)
    var valueChangeleng;
    if (valueChange instanceof Array) {
      valueChange.splice(01);
      valueChangeleng = valueChange.length - parseInt(valueChange.length / 57) * 57
    } else if (valueChange == undefined) {
      valueChangeleng = 0
    }
    const array = Data;
    console.log(array, "array")
    var arr = []
    for (let i = 0; i < array.byteLength; i += 18) {
      arr.push(array.slice(i, i + 18))


    }
    var uintBuffer = new Uint8Array(arr[valueChangeleng])
    console.log("uintBuffer", uintBuffer)
    var uintBuffers = concatenate(Uint8ArrayUint8Array.of(valueChangeleng), uintBuffer)
    console.log("uintBuffers", uintBuffers)
    var array1 = Array.from(uintBuffers)
    //console.log(CRC.ToCRC16(array1),"校检和")
    console.log(CRC(array1), "校检和")
    array1.push(hex2int(CRC(array1).slice(13)))
    var arrayBuffer = new Uint8Array(array1).buffer;
    console.log("arrayBuffer", arrayBuffer)
    const data = arrayBuffer || null,
      self = this
    console.log("第", valueChangeleng, "次""发送的数据为", data)
    var success = "2b4f4bcf75" //监听成功的值
    var fail = "2d4552520a4" //监听失败的值
    if (valueChangeleng == 0) {
      wx.writeBLECharacteristicValue({
        deviceId,
        serviceId,
        characteristicId,
        value: data,
        successfunction () {
          console.log("我是第一个数据包")
        }
      })
    } else if (data && data instanceof ArrayBuffer && valueChange[valueChangeleng] == success) {
      wx.writeBLECharacteristicValue({
        deviceId,
        serviceId,
        characteristicId,
        value: data,
        successfunction () {
          console.log("success,成功")
          if (valueChangeleng < arr.length) {
            sleep(20)
            self.writePackageData1(deviceId, serviceId, characteristicId, Data)
          }
        }
      })
    } else if (data && data instanceof ArrayBuffer && valueChange[valueChangeleng] == fail) {
      wx.writeBLECharacteristicValue({
        deviceId,
        serviceId,
        characteristicId,
        value: data,
        successfunction () {
          console.log("fail,失败")
          self.writePackageData1(deviceId, serviceId, characteristicId, Data, valueChangeleng - 1)


        }
      })
    }
  },
最后一次编辑于  2021-01-19
回答关注问题邀请回答
收藏

3 个回答

  • 辉
    2021-07-09

    同,只能监听到,我想获取到发送来的值进行判断,但获取不到,大佬最终如何解决这个问题的

    2021-07-09
    有用
    回复
  • 😶
    😶
    2021-01-19

    你好,请提供一下可以复现问题的代码片段,按照时间顺序 用文字 描述清楚流程,具体哪一步不符预期可以贴下console截图

    2021-01-19
    有用
    回复 10
    查看更多(5)
  • ShowFaker
    ShowFaker
    2021-01-19

    哥们你这个高端...

    2021-01-19
    有用
    回复 1
    • 自省
      自省
      2021-01-19
      帮我@下官方大佬来回答 谢谢啦
      2021-01-19
      回复
登录 后发表内容
问题标签