How to save a binary file after i received from BLE?

My application is receiving a remote binary file via BLE and save it. I use "AsciiConversion" extension block to encode received byte then to "file" extension block to write to a file, when i check the result of the file, only the value between 0x00 ~ 0x7F can be saved correctly, the value greater then 0x80 will be replace to 2 bytes value. As below,
the original contents are
0x99 0xcc 0xaa 0x55 0x55 0x53 0x45 0x52 ....
send via BLE, the AI2 app received the data and save to a file becomes
0xc2 0x99 0xc3 0x8c 0xc2 0xaa 0x55 0x55 0x53 0x45 0x52 ...

My block as below

How to fix it? Or any other extension block to use?

If your application receives bytes and wants to store them, why do you need to convert those bytes? What format are you converting the bytes to? Why don't you just write them down directly?

The problem is an extension that converts something but we don't know what and why.

Try removing the extension block and writing the bytes to the file directly.

This is part from protocol, i need to parse the data contents and compare the checksum, after all pass, point the start to the assigned position, and find the data length in the protocol, pick all the data in the protocol then write to a file. These are data from an IOT device.

I don't know, we can't see most of the blocks. You should ask the author of this extension.

The same blocks, but not use ASCiiconversion, the data save to the file will show as the following (all in ASCII text form)
it means 153 204 170 85 85 83 69 82 ...
the same meanings as original file
0x99 0xcc 0xaa 0x55 0x55 0x53 0x45 0x52 ....
but all in ASCII text, that is why i call ASCiiconversion. It seems either asciiconversion block can not conver the value greater than 0x7f, or FILE block can not save the value greater than 0x7f. Is there any other way to solve it?

What block will you use to receive data? It seems that this converter converts characters to utf8.

And I still don't understand why convert bytes to write. If you want your data in hexadecimal, use a math block that converts decimal to hexadecimal.

Hi willychen I'm having a similar issue, it seems the in the built file blocks only allow writing text to a file.

1 Like

In fact, the file component writes text. So you're on the right track if you want to convert the bytes to ascii characters and then save them. The problem here is the file component that use utf8. Pure ascii ends at byte 127, later different encodings use different techniques. UTF8 encodes with two bytes.

The question is what do you want to do next with the saved file? Will you export it further or process it only in your app?

The solution would be another extension that can save a text file in ANSI encoding.

If you can influence the authors of the communicating programs, maybe convince them to trade data using a text based format like JSON.

Storage is cheap, and programs like MS Word and Excel have evolved to abandon proprietary file formats.

The bin file can be different usages, one is OTA firmware update. The protocol between IOT devices can exchange data and do firmware update.

Putaning whether this bin will be used only by your app or for general use. Because if only through your app, the format can be any. If it's for the general then you'll have to do something with the data record.

The problem solved. I found an extension block named "AI2 Byte Array Extension" can meet everything I need.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.