1

I'm trying to write in a characteristic that sends a binary file as the value, this value is large, it's 19215 bytes which is not a problem because i negociated the mtu using :

           device.connect({ requestMTU: 260 }) 

i've divided the file into 240 bytes for each element and each time encode the element to base64 and i use the function writeCharacteristicWithResponseForDevice() in order to write that element,

the issue is i successfully wrote the whole file 19215 bytes using a loop to write each time an element, but while i tried to read the characteristic i can only read the last written element

Example:

           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))

when i read the Characteristic using

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

i get as a value :

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

it should be element1+ element2 + element3

-

here is my code for writing :

  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  )
            });
     }}

  }

here is the function to read characteristic:

     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  )
           });

}

Can anybody help please by providing a working example in how to send large package files, because the problem can be while writing, the function erases the older value maybe.

Thanks

raddaoui
  • 95
  • 13
  • Hi, is your reading the characteristics code works? i Just need help in reading the data from characteristics using the service UUID. when i print the device.service this is what i'm getting Services and characteristics discovered: {"_40": 0, "_55": null, "_65": 0, "_72": null} How can i extract the serviceUUID from the device.service and then characteristics UUID from service object. Please help. Thanks – Madhu Jul 22 '20 at 10:46
  • @Madhu actually for the serviceuuid is coded in the server side so i am just using it while reading my characterestics, i don't search for it using code cause i tried and i didn't find how, so i asked for the service uuid and i just initiate it in my code and it works just fine this way – raddaoui Aug 17 '20 at 13:59

0 Answers0