Receive Numerics with BLE

I can receive text with the BLE receive String function but can’t receive any numeric data.

If I try to read it with receive byte array it fails with “n is not a function. (In ‘n(void 0,f.t0.message)’,‘n’ is undefined)

Does anyone know what format the receive byteArray function actually requires as input and is there a way to receive numerics? I’d rather not have to convert all my numbers to strings before sending them.

Hi - I’m having the same problem, the “Receive Byte” array fails for me, and I can only receive a string. How do I convert it to the list of integers received from reading the BLE GATT?

1 Like

Oops, edited this post. I was asking about the string you’re able to get and what list of integers you need from it.

1 Like

The call BLE receive block has a green block named data (string) which indicates that you will be receiving text from the sending BLE device. It is just a simple text like Hello world and then you can treat this text any way you would like.

Yes, I’m communicating with a BLE toy (www.heykube.com), and this GATT characteristic can report the current battery voltage and charging status. It’s 2 bytes long.

uint8_t data[2];

voltage_int = data[0] | (data[1] & 0xf) << 8);
voltage = voltage_int / 512.0
charging = data[1] >> 4;

I would expect to receive bytes, [0x0, 0x18] to report that it’s at 4.0 volts and charging (since 0x800 / 512.0 = 4.0) and bit 12 is a 1.

  • Using the Receive Byte Array version crashes the app, so I can only get the Receive String version to do something
  • On the Transmit String, I can use a list of integers and it works to send the correct bytes
  • I was hoping you can just extract the bytes from the string, but it’s not working yet.

I guess this is a bit over my head. I’m familiar with the idea of parsing bytes from a string but… what is the exact string that you’re able to get in Thunkable?

Unfortunately, I get an unreadable string, since it’s just raw HEX data.

Here is the crux of the problem, how can I convert “A” into the integer 65 (from the ASCII table). See this code here, trying to add 0 to the “A” results in NaN on the screen. In C, the equivalent is the atoi() function.

When you say “unreadable”… what is the exact Hex you are seeing?

I spent some time learning to parse hex codes here:

But as far as your question about converting letter to integers, I’m not sure how to do that. I doubt it’s difficult but I’m not at my computer so I can’t work on a demo at the moment.

1 Like

To convert HEX to decimal you need to create two lists; one for all hex numerals and a corresponding decimal list or you can create only the alphanumeric part of the hexadecimal numerals.

You would need to read the received string character by character and compare it to the hex list and take the corresponding decimal value to calculate.

See this example of converting hex colors to RGB

https://x.thunkable.com/projectPage/61742edbc7e59500107319d4

NaN means Not a Number which is of course it is because A is not a digit. However if you want the A to just display as 0A then you should do

Which will display 0A in the Label.

Sorry, it’s a little more low level then this. Normally a BLE read on returns a byte array.

uint8_t rx_bytes[MAX_PACKET];

  • For me, using the BLE “Receive Byte Array” returns an error
  • My current workaround is to try to use the Receive String, and we see if we can extract the bytes. The idea is that ASCII characters, numbers between 0-255. If I receive “Z”, it maps to an integer 90, if I receive a “/” it maps

It sounds like this is not the right approach, I should just debug why Receive Byte array doesn’t work.

I know what is a byte array and in Thunkable you can use JavaScript to get this. Not easy but you can.

See my demo of how to run JavaScript code in Thunkable.
https://x.thunkable.com/projectPage/621c9b6c73ba2500119d10ef

If you enter the following *"/".charCodeAt(0)
You should get 47

image

If you need to convert the number back to a character then use String.fromCharCode(90) which should gives you Z.

Of course you can build your own code and run it using the Web Viewer but you can start with this to test your output.

Very cool, I like the idea of the web-browser posted messages to get Javascript to execute. It’s a pretty cool expert mode function, that I will take advantage of!

For this specific case, I think I should just wait on the Debug for the “Receive Byte Array”, instead of pursing the ASCII parsing.

https://community.thunkable.com/t/why-does-ble-receive-string-work-and-ble-receive-byte-array-not-work/1831529

1 Like

I like to extend the available functions with JavaScript.

See this demo for choosing an image file from your local drive and view it in the screen. Of course, you can choose to save it to Firebase and then use it in your app.

https://x.thunkable.com/projectPage/61f68431c3761001dba12df1

1 Like