问题
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
来源:https://stackoverflow.com/questions/61386613/large-file-sending-by-using-react-native-ble-plx