Download binary file and send it via Bluetooth

Dear All,
I'm working on a method to download and send data from Android phone to a microcontroller connected via Bluetooth serial. I have no issues with the Bluetooth communication. What I do:

  1. Download a binary file using the Web component. I don't want to save the file to the phone's file system because it contains sensitive data. That's why I used the GotText() method instead of the GotFile() method.
  2. I save the contents to a global variable
  3. If the user clicks on the "update" button, I go through the content of the global variable, convert the characters to byte and send them one by one via the Bluetooth serial.

The first problem is with the download. The downloaded content is not the same that the content on the server. I also see that the length of the variable does not match with the file size on the server.

I figured maybe the method I'm using to download assumes plain text file but I have to download the file and send its content to the microcontroller as-is. Can anyone point me how to download a binary file to memory and use its content as a byte stream?

The original content of the file is in blue and what I could see on the screen of my phone is in white background. It's clear its kind of similar but not the same by far.
Please give me some tips what to use to keep the file intact.




Thank you.

Is this simply a formatting issue ? Set background colour to blue and text colour to white, font to monospace, in your label/textbox?

How are you viewing the original file on your server, which program, is the default blue and white ?

Hello,
maybe there is a little misunderstanding here. :slight_smile:

  1. The blue one is the original file in the server in a hex editor.
  2. The white one is the file downloaded and printed to my phone's screen, using the Web component. The file was either altered during the download or I use a wrong type of variable (e.g. I was thinking the variable uses UTF-8 while the original file is clearly a binary one) because if you look at the stream of the bytes it does not match.

It's definitely nothing to do with formatting and setting backgrounds. :slight_smile:

OK :wink:

can you share an example file, then we can test.

OK, I created a strip-down version of my project. It downloads an example Arduino binary file from my site. In the meantime I commented out the "asc" procedure I copied from the Internet (thanks to the kind person who originally created it) and replaced with a Webviewer component and now I use Javascript to convert characters to their ascii code. Note that I don't like to add another component (Webviewer) for this conversion but I thought I'd try with a different method, too.

Still I left the original function as-is (but disabled) as it might be useful.

I got a bit forward with the JS version as I can see clearly from the $FFFD values it must be some kind of UTF8 issue. However I can't go further because the Web1.GotText() provides the variable "responseContent" and I can't do anything to change it if it's an UTF-8 encoded text already.

I think we'd rather use an alternative method to retrieve the file as a stream of bytes (as the response type suggests, it's application/octet-stream). Maybe I should look for "download binary file with Javascript"?

Honestly, I'd highly prefer a built-in AppInventor2 solution and not using the Webviewer component for a very different thing that it's intended to.

Thank you.
download.aia

Why not generate a byte array server side, then either fetch that stream of bytes or download a text file with the byte array.

A linux command:

xxd -i Blink.bin > blinkbytes.txt

will output this to a file:

unsigned char Blink_bin[] = {
  0x0c, 0x94, 0x5c, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x0f, 0x01, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00,
  0x0c, 0x94, 0x6e, 0x00, 0x0c, 0x94, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x24, 0x00, 0x27, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00,
  0x28, 0x00, 0x2b, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08,
  0x10, 0x20, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x00, 0x00, 0x08,
  0x00, 0x02, 0x01, 0x00, 0x00, 0x03, 0x04, 0x07, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x11, 0x24, 0x1f, 0xbe, 0xcf, 0xef, 0xd8, 0xe0,
  0xde, 0xbf, 0xcd, 0xbf, 0x21, 0xe0, 0xa0, 0xe0, 0xb1, 0xe0, 0x01, 0xc0,
  0x1d, 0x92, 0xa9, 0x30, 0xb2, 0x07, 0xe1, 0xf7, 0x0e, 0x94, 0x59, 0x01,
  0x0c, 0x94, 0xe6, 0x01, 0x0c, 0x94, 0x00, 0x00, 0xe1, 0xeb, 0xf0, 0xe0,
  0x24, 0x91, 0xed, 0xe9, 0xf0, 0xe0, 0x94, 0x91, 0xe9, 0xe8, 0xf0, 0xe0,
  0xe4, 0x91, 0xee, 0x23, 0xc9, 0xf0, 0x22, 0x23, 0x39, 0xf0, 0x23, 0x30,
  0x01, 0xf1, 0xa8, 0xf4, 0x21, 0x30, 0x19, 0xf1, 0x22, 0x30, 0x29, 0xf1,
  0xf0, 0xe0, 0xee, 0x0f, 0xff, 0x1f, 0xee, 0x58, 0xff, 0x4f, 0xa5, 0x91,
  0xb4, 0x91, 0x2f, 0xb7, 0xf8, 0x94, 0xec, 0x91, 0x81, 0x11, 0x26, 0xc0,
  0x90, 0x95, 0x9e, 0x23, 0x9c, 0x93, 0x2f, 0xbf, 0x08, 0x95, 0x27, 0x30,
  0xa9, 0xf0, 0x28, 0x30, 0xc9, 0xf0, 0x24, 0x30, 0x49, 0xf7, 0x20, 0x91,
  0x80, 0x00, 0x2f, 0x7d, 0x03, 0xc0, 0x20, 0x91, 0x80, 0x00, 0x2f, 0x77,
  0x20, 0x93, 0x80, 0x00, 0xdf, 0xcf, 0x24, 0xb5, 0x2f, 0x77, 0x24, 0xbd,
  0xdb, 0xcf, 0x24, 0xb5, 0x2f, 0x7d, 0xfb, 0xcf, 0x20, 0x91, 0xb0, 0x00,
  0x2f, 0x77, 0x20, 0x93, 0xb0, 0x00, 0xd2, 0xcf, 0x20, 0x91, 0xb0, 0x00,
  0x2f, 0x7d, 0xf9, 0xcf, 0x9e, 0x2b, 0xda, 0xcf, 0x3f, 0xb7, 0xf8, 0x94,
  0x80, 0x91, 0x05, 0x01, 0x90, 0x91, 0x06, 0x01, 0xa0, 0x91, 0x07, 0x01,
  0xb0, 0x91, 0x08, 0x01, 0x26, 0xb5, 0xa8, 0x9b, 0x05, 0xc0, 0x2f, 0x3f,
  0x19, 0xf0, 0x01, 0x96, 0xa1, 0x1d, 0xb1, 0x1d, 0x3f, 0xbf, 0xba, 0x2f,
  0xa9, 0x2f, 0x98, 0x2f, 0x88, 0x27, 0xbc, 0x01, 0xcd, 0x01, 0x62, 0x0f,
  0x71, 0x1d, 0x81, 0x1d, 0x91, 0x1d, 0x42, 0xe0, 0x66, 0x0f, 0x77, 0x1f,
  0x88, 0x1f, 0x99, 0x1f, 0x4a, 0x95, 0xd1, 0xf7, 0x08, 0x95, 0x8f, 0x92,
  0x9f, 0x92, 0xaf, 0x92, 0xbf, 0x92, 0xcf, 0x92, 0xdf, 0x92, 0xef, 0x92,
  0xff, 0x92, 0x4b, 0x01, 0x5c, 0x01, 0x0e, 0x94, 0xb8, 0x00, 0x6b, 0x01,
  0x7c, 0x01, 0x0e, 0x94, 0xb8, 0x00, 0x6c, 0x19, 0x7d, 0x09, 0x8e, 0x09,
  0x9f, 0x09, 0x68, 0x3e, 0x73, 0x40, 0x81, 0x05, 0x91, 0x05, 0xa8, 0xf3,
  0x21, 0xe0, 0x82, 0x1a, 0x91, 0x08, 0xa1, 0x08, 0xb1, 0x08, 0x88, 0xee,
  0xc8, 0x0e, 0x83, 0xe0, 0xd8, 0x1e, 0xe1, 0x1c, 0xf1, 0x1c, 0x81, 0x14,
  0x91, 0x04, 0xa1, 0x04, 0xb1, 0x04, 0x29, 0xf7, 0xff, 0x90, 0xef, 0x90,
  0xdf, 0x90, 0xcf, 0x90, 0xbf, 0x90, 0xaf, 0x90, 0x9f, 0x90, 0x8f, 0x90,
  0x08, 0x95, 0x1f, 0x92, 0x0f, 0x92, 0x0f, 0xb6, 0x0f, 0x92, 0x11, 0x24,
  0x2f, 0x93, 0x3f, 0x93, 0x8f, 0x93, 0x9f, 0x93, 0xaf, 0x93, 0xbf, 0x93,
  0x80, 0x91, 0x01, 0x01, 0x90, 0x91, 0x02, 0x01, 0xa0, 0x91, 0x03, 0x01,
  0xb0, 0x91, 0x04, 0x01, 0x30, 0x91, 0x00, 0x01, 0x23, 0xe0, 0x23, 0x0f,
  0x2d, 0x37, 0x58, 0xf5, 0x01, 0x96, 0xa1, 0x1d, 0xb1, 0x1d, 0x20, 0x93,
  0x00, 0x01, 0x80, 0x93, 0x01, 0x01, 0x90, 0x93, 0x02, 0x01, 0xa0, 0x93,
  0x03, 0x01, 0xb0, 0x93, 0x04, 0x01, 0x80, 0x91, 0x05, 0x01, 0x90, 0x91,
  0x06, 0x01, 0xa0, 0x91, 0x07, 0x01, 0xb0, 0x91, 0x08, 0x01, 0x01, 0x96,
  0xa1, 0x1d, 0xb1, 0x1d, 0x80, 0x93, 0x05, 0x01, 0x90, 0x93, 0x06, 0x01,
  0xa0, 0x93, 0x07, 0x01, 0xb0, 0x93, 0x08, 0x01, 0xbf, 0x91, 0xaf, 0x91,
  0x9f, 0x91, 0x8f, 0x91, 0x3f, 0x91, 0x2f, 0x91, 0x0f, 0x90, 0x0f, 0xbe,
  0x0f, 0x90, 0x1f, 0x90, 0x18, 0x95, 0x26, 0xe8, 0x23, 0x0f, 0x02, 0x96,
  0xa1, 0x1d, 0xb1, 0x1d, 0xd2, 0xcf, 0x78, 0x94, 0x84, 0xb5, 0x82, 0x60,
  0x84, 0xbd, 0x84, 0xb5, 0x81, 0x60, 0x84, 0xbd, 0x85, 0xb5, 0x82, 0x60,
  0x85, 0xbd, 0x85, 0xb5, 0x81, 0x60, 0x85, 0xbd, 0x80, 0x91, 0x6e, 0x00,
  0x81, 0x60, 0x80, 0x93, 0x6e, 0x00, 0x10, 0x92, 0x81, 0x00, 0x80, 0x91,
  0x81, 0x00, 0x82, 0x60, 0x80, 0x93, 0x81, 0x00, 0x80, 0x91, 0x81, 0x00,
  0x81, 0x60, 0x80, 0x93, 0x81, 0x00, 0x80, 0x91, 0x80, 0x00, 0x81, 0x60,
  0x80, 0x93, 0x80, 0x00, 0x80, 0x91, 0xb1, 0x00, 0x84, 0x60, 0x80, 0x93,
  0xb1, 0x00, 0x80, 0x91, 0xb0, 0x00, 0x81, 0x60, 0x80, 0x93, 0xb0, 0x00,
  0x80, 0x91, 0x7a, 0x00, 0x84, 0x60, 0x80, 0x93, 0x7a, 0x00, 0x80, 0x91,
  0x7a, 0x00, 0x82, 0x60, 0x80, 0x93, 0x7a, 0x00, 0x80, 0x91, 0x7a, 0x00,
  0x81, 0x60, 0x80, 0x93, 0x7a, 0x00, 0x80, 0x91, 0x7a, 0x00, 0x80, 0x68,
  0x80, 0x93, 0x7a, 0x00, 0x10, 0x92, 0xc1, 0x00, 0xed, 0xe9, 0xf0, 0xe0,
  0x24, 0x91, 0xe9, 0xe8, 0xf0, 0xe0, 0x84, 0x91, 0x88, 0x23, 0x99, 0xf0,
  0x90, 0xe0, 0x88, 0x0f, 0x99, 0x1f, 0xfc, 0x01, 0xe8, 0x59, 0xff, 0x4f,
  0xa5, 0x91, 0xb4, 0x91, 0xfc, 0x01, 0xee, 0x58, 0xff, 0x4f, 0x85, 0x91,
  0x94, 0x91, 0x8f, 0xb7, 0xf8, 0x94, 0xec, 0x91, 0xe2, 0x2b, 0xec, 0x93,
  0x8f, 0xbf, 0x00, 0xe0, 0x10, 0xe0, 0x81, 0xe0, 0x0e, 0x94, 0x70, 0x00,
  0x68, 0xee, 0x73, 0xe0, 0x80, 0xe0, 0x90, 0xe0, 0x0e, 0x94, 0xdd, 0x00,
  0x80, 0xe0, 0x0e, 0x94, 0x70, 0x00, 0x68, 0xee, 0x73, 0xe0, 0x80, 0xe0,
  0x90, 0xe0, 0x0e, 0x94, 0xdd, 0x00, 0xc2, 0xe3, 0x81, 0xe0, 0x0e, 0x94,
  0x70, 0x00, 0x62, 0xe3, 0x70, 0xe0, 0x80, 0xe0, 0x90, 0xe0, 0x0e, 0x94,
  0xdd, 0x00, 0x80, 0xe0, 0x0e, 0x94, 0x70, 0x00, 0x62, 0xe3, 0x70, 0xe0,
  0x80, 0xe0, 0x90, 0xe0, 0x0e, 0x94, 0xdd, 0x00, 0xc1, 0x50, 0x61, 0xf7,
  0x01, 0x15, 0x11, 0x05, 0xb1, 0xf2, 0x0e, 0x94, 0x00, 0x00, 0xd3, 0xcf,
  0xf8, 0x94, 0xff, 0xcf
};
unsigned int Blink_bin_len = 976;

which you can then download

1 Like

Heh I didn't know about that, I'll definitely try and get back with the results. I like it much better than using Webviewer component. I'll keep you posted, thanks!

Hello Timai2, the solution you suggested worked. Thank you indeed.

1 Like

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