Can App Inventor send hex raw data to a Bluetooth device?

You can make an App Inventor list of the hex data as integers and send (Write) as bytes.

So, we have done this successfully when driving one or more servo motors. However, that was using BLE - check the version of Bluetooth your device is using.

If you are receiving a string and know what you need to “cut out” of that string, App Inventor has the Tools to do that via the Text Blocks (no regex).

Thanks, sending is fine.
How can I put each received byte into a separata list item?

This procedure (picture attached), puts all data 00 0E 5B DB 27 4D D1 00 0E 5B DB 27 00 00 01 15 00 00 00 15 00 00 00 00 00 00 00 00 24 FD
into one 1st element of an array.

I guess if I have all items into separate indexes in a list, I can take 14th and 15th element and get “01 15”, what I need as a result.

You would normally approach that problem from the device side by inserting value delimiters.

For example, if the device sent 00|0E|5B|DB|27|4D|D1|00|0E\n (\n indicating end of data), you could split the data at the ‘|’ character.

Does the data arrive delimited in some way, perhaps spaces? If not, your code could count the pairs?

So, you would not be adding the data stream directly to a list, either introduce delimiters at the device (if not already there) or split the data up using Text Blocks.

Note: “Bytes Available to Receive” does not return how many are available, only that there are bytes available, so simply use -1 to ensure all available bytes are received.

And if there isn’t a delimiter:

Thanks,
I think the data from my device goes without delimiters, since it’s a Power meter (counter). And when I do a text printing in App Inventor, it shows all data with spaces in between. So, 0000DE is displayed like 0 0 DE, which has spaces in between, and furthermore, 00 becomes 0 so that I can’t rely on a length.

Don’t know why AppInventor adds spaces to data…

… that’s not likely to be App Inventor adding spaces, but since they are there, you can use them to split the data.

A stream falls short of expected length? How often is the device sending? Some time has to be allowed for App Inventor to process. If you had control of the time interval used by the device I would say set it to be at least 200 milliseconds longer than the App Inventor Clock Timer Interval. In all cases, timer intervals should be set to sensible values - if the data is received every 10 minutes, set the Clock Timer accordingly.

When I tried to use lists (with no avail), the Android app showed me like this:
(0 0 11 21)
and I am sure that my device does not put any parentheses into a data flow. So, either the code generated, or some other thing inserts ) and spaces…

Will try to split by spaces as you suggested. It’s not a constant flow, data is received upon request.

you might want to learn how to work with lists

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

Taifun,

As I began reading the above tutorials, I still don’t know if it is possible with APInv to put data bytes received by BluetoothClient1.ReceiveUnsignedBytes with Number of bytes = Bytes available to receive, each byte into a separate array (i.e. list) element, where I can point to them by index…

Just never assign the list you receive from BluetoothClient1.ReceiveUnsignedBytes into a text field, and you will never get spaces or parentheses. Assign the incoming list directly into a global variable, and work with that from there, testing its list length and selecting from it by item number after checking if the list is long enough.

ABG,
I seem to follow your words, see picture.
When I send this global.count variable to the screen of a phone, it gives spaces and parentheses. And 0 is one symbol, not 00.
Unfortunately, it seems to be no way of making a variable numeric or text in AI2.

Hi Maxim

  1. Do not use .“Bytes Available to Receive” as the number of bytes to receive - that will deliver a potentially incomplete data stream . Use the Math Block “-1” (as I have already shown in my example).

  2. Are you using Bluetooth Receive Text as per my example, but with the text splitting into a List via a Procedure? That should work…

Here is a link to an example that stays within the proper list structure
for a Byte stream …
https://groups.google.com/d/msg/mitappinventortest/4AvhJkZ4_fA/OhOJIB_lBwAJ

I can not recommend a text stream if your data bytes might include decimal values for NL.

@Maxim,

If you want corrected blocks, post an exported .aia file here,
or a right click Download Blocks As Image of the Clock1.Timer
event.

Thanks folks for help, I finally managed to get through by splitting a variable by spaces and putting it to a list.

Is there any library available for CRC16/Modbus calculation with AppInv2? I can’t write it myself :frowning:

AI2 can send bytes.

Here is a translation of https://ctlsys.com/support/how_to_compute_the_modbus_rtu_message_crc/ into blocks, untested …

MODBUS_CRC16.aia (5.5 KB)

1 Like

@Maxim,

I am not sure you have been cured of your habit of trying to use text analysis of lists.
Here is a little medicine for you …

Try to print out the resulting list.

@ABG,

Thanks for your project, it worked just fine!

Thanks for telling us.

Would you care to post your source for others?