0

我正在尝试写入一个发送二进制文件作为值的特性,这个值很大,它是 19215 字节,这不是问题,因为我使用以下方式协商 mtu:

           device.connect({ requestMTU: 260 }) 

我已将每个元素的文件分成 240 个字节,并且每次将元素编码为 base64,我使用函数 writeCharacteristicWithResponseForDevice() 来写入该元素,

问题是我使用循环成功写入了整个文件 19215 字节,每次写入一个元素,但是当我尝试读取特性时,我只能读取最后写入的元素

例子:

           this.manager.writeCharacteristicWithResponseForDevice(device.id,"1111", "40E1ED56-EC4A-4DC6-A6BD-30377F186B77", base64.encode(element1)

           this.manager.writeCharacteristicWithResponseForDevice(device.id,"1111", "40E1ED56-EC4A-4DC6-A6BD-30377F186B77", base64.encode(element2)) 

           this.manager.writeCharacteristicWithResponseForDevice(device.id,"1111", "40E1ED56-EC4A-4DC6-A6BD-30377F186B77", base64.encode(element3))

当我使用

          device.readCharacteristicForService("1111", "40E1ED56-EC4A-4DC6-A6BD-30377F186B77")

我得到一个值:

        console.log(base64.decode(characteristic.value) => element3

它应该是元素1+元素2+元素3

-

这是我的写作代码:

  WriteIncaracteristic() {

   // this.state.fileSize is calculated in another function
      const nbPackages = Math.floor(this.state.fileSize/240) + 1

      var fileArray = []

     for (let i = 0; i <= nbPackages; i++) {

      // this.state.fileContent is created in another function
      fileArray.push(this.state.fileContent.slice(0, 240))
      this.state.fileContent = this.Remove(this.state.fileContent, 0, 240)

     }

     for (let j = 0; j <= fileArray.length; j++) {

       if (fileArray[j]) {

       const device = this.state.myDevice

           this.manager.writeCharacteristicWithResponseForDevice(device.id,"1111","40E1ED56-EC4A-4DC6-A6BD-30377F186B77", base64.encode(fileArray[j]))

            .then((characteristic) => {
              console.log(base64.decode(characteristic.value));

              return 
            }) .catch((error) => {
              this.createTAlert("writing", error.message  )
            });
     }}

  }

这是读取特征的功能:

     ReadCaracteristic() {

          const device = this.state.myDevice

          device.readCharacteristicForService("1111", "40E1ED56-EC4A-4DC6-A6BD-30377F186B77")
          .then((characteristic) => {

               console.log(base64.decode(characteristic.value));
               console.log(characteristic );

            return 
           }) .catch((error) => {
               // Handle errors
               this.createTAlert("Reading", error.message  )
           });

}

任何人都可以通过提供一个有关如何发送大包文件的工作示例来提供帮助,因为问题可能出在编写时,该函数可能会删除旧值。

谢谢

4

0 回答 0