Checking 4 bytes to mark beginning and end of a data packet on incoming bluetooth data

Hi ,

I have an App that displays near reatime audio levels in appInventor2 and there
is a data stream coming into the app.

When hit a button a control message is send to the hardware and the hardware returns a packet of data.

the packet somewhere in this stream marked by 4 byte header and 4byte tail for other text data. It’s a list of file names on SD card in my custom hardware.

I can stop the app drawing the incoming audio level data by switching off its clock but the data will still be present since its streaming out over DMA from hardware ( therefore the need to have a packet header and tail ) The packet containing the audio recording names will be of an unknown length so I need to check for the tail. Each filename has a delimiter ‘|’ or 0x7C. I experimented with split message into list at the ‘|’ delimiter with some success.

What’s the best approach for looking for the 4 byte needle in the haystack of bytes and checking for its 4 byte tail ? do I have to load 1 byte at a time from BT ? if so how?

It only needs to scan for the one packet and the other data can be ignored…

It it possible to stop the audio level data stream, but it’s not ideal.

I’d also like to find a way to detect packets so I can have different aspects of the app find relevant data in the one data stream.

any ideas would be much welcome :slight_smile:


If you can post a couple of sample streams, one of my fellow Power Users can show you how best to extract the data you require. Since you mention imposing your own delimiters (’|’, good choice), you could if necessary surround the data of interest with a different character pair.

We basically need to know a lot more about the hardware and the control you have on how the audio data is streamed to advise you.

mmm , there is no audio data.

the audio LEVEL data ( not audio ) is streamed as 10 bytes for the audio level displays.
there are 8 char values that are used and 2 for alignment.

the packet i’m trying to find in that stream of bytes can be any length ( within reason ) and appear at any time.

it starts with 4 bytes
0x23 0x2A 0x23 0x2A

it will end with 4 bytes
0x2A 0x23 0x2A 0x23

so the header in text is ## and the tail of the packet is ##

I could change that but need more than 1 otherwise it could just arrive by chance in the audio level values.

the ascil bytes of the text or file names are separated by ‘|’ and I can split those into a list.

The stream of bytes for the audio meters can be ignored.

I am just trying to look for a 4 byte pattern incoming from bluetooth.

I am sending bytes over a UART with DMA to HC-06

As far as appInventor is concerned its just HC-06 sending bytes.

the header is hash star hash star , the tail is star hash star hash … for some reason the forum post removed the * :slight_smile:

I did something like this in the project in this thread …

ABG ! I think it was you that helped get the audio meter levels working !!

by the way , It turned out the glitching I thought existed in the app inventor code was actually drawing non audible noise in the audio tracks :slight_smile: So the audio meters work better than my ears !

OK, i’m looking at you code example in app inventor and i’ll try and see how your scanning incoming bytes for a byte pattern :slight_smile:

I simplified this to 2 x bytes of 0x07 hex as the header as it works for the audio level meter packets.

But know I’m not sure how to join the bytes received to make text names in a list using the separator “|” marker.

8 bytes a time of text arrive but i’m a bit stuck on how to split at the “|” marker

here’s the code i’m using to detect the 2 header bytes at the begining of each 10 byte packet containing the 8 bytes of character data for filenames.

For converting lists of Bytes to text, see the procedures in this project.
(You don’t need the stx and ETX character removal parts.)
Conversion tables are by @Italo.
asc AsciiCHRASCprocedures.aia (4.0 KB)

chr strip_STX_ETX

I’m not sure if I can call bytes_to_text like I am?

I get… Invalid text operation Segment: Start is less than 1 (0).

are the “to result” blocks like a function in C ? do they return results to wherever they are called from?

the error looks like this…

First of all, you broke it when you replaced its input parameter reference
with a reference to one of your global variables.

AI2 value procedures like bytes_to_text are indeed like functions in other languages like C, meant to be reusable with different inputs. You can recognize them by their nipple on the left.

I am suspicious of your global variable names, particularly audioFileNames.
The name implies that it is a list (trailing s in the name), and implies that
each item in that list is an audioFileName. From your incoming bytes stream handling code,
I suspect you need a couple of extra global variables for intermediate values,
like maybe body_bytes for a list of bytes grabbed after the header, and audioFileName
for the text file named value output from a bytes_to_text call against body_bytes, ready for addition to the list audioFileNames.

Notice the difference between audioFileName and audioFileNames, item vs list of items.

Yes , I should build the packet ( the text string of bytes ? )
I called that packet. Then process that with the ‘bytes_to_text’ block

I think I have 2 problems now.

1st is that if I just print all the bytes in the packet I make by using the join block I am getting all the bytes sent from my hardware and there appears to be no filtering of other packets beginning with 88. The packets holding the filename bytes start with 77.

I can see the the correct byte ascii codes for “wav|” arrive this way.

Then if I send these joined bytes in the text variable ‘packet’ to bytes_to_text block I get an error on the app.

I’ll send the screen grab next of the data coming in… not allowed 2 x pics in 1 post!

here’s the data arriving… 4 packets arrive and header data is printed to ! then a load of the wrong packets are there to !

See attached.

You need to read about how lists work, and also to read your own code.
Is English your native language?

Uploading an exported .aia file would help.

Ah , yes I should have been joining bytes to packet!! oops.

This block thew me off track , should have seen the ‘split at spaces’ and realized its the
equivalent of sending a byte list!

Yes , i am English , so no excuse :slight_smile:

I’ll read some more on lists and have another go, this time adding bytes to the packet list and post an AIA.

if > 8 bytes arrive the higher bytes get deleted, that could be a problem…

I suppose bytesAvailableToRecieve for BT is a buffer.

add items to list ?

I get Invalid text operation
Segment: Start is less than 1(0).

RXaudioFilenames_BT.aia (12.0 KB)

If you are asking for help, I recommend you to make it as easy for others to be able to help you …
You probably will get more feedback then…

which means in your case post a screenshot of your relevant blocks…

To download the aia file, upload it to App Inventor, open it, do some bug hunting for you, etc… this takes time, and most people will not do that…
Thank you.


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

Yes agreed, I’m limited to one attachment per post as a new user so forgot to do that.

Can you see anything wrong with the way I’m passing the bytes list ‘packet’ to the TO-RESULT ‘bytes_to_text’ block ? the byte values are ASCII and start with I’d like to just convert them and print them in the app. later I’ll need to split them at ‘|’ into an index-able list.

Also , I’m trying to find a simple way to look for 2 x incoming bytes as a packet header ( id ) I would prefer 3 or 4 byte header to be sure there isn’t by coincidence 2 values the same in the data stream. possible.

My hardware just has one UART IO stream of data ( no BT TCP/IP stack etc ) Its just an HC-06 connected to other hardware.

Attached see a general purpose technique to locate a byte subsequence in a byte stream.
The test bed works off Comma Separated Value (CSV) text

Capture found first pair found first triple found first found triple at index 2 notfound sublist_index.aia (4.2 KB) when btnRandomize Click wrap representations of the byte lists, which I hope you have learned by now.

No, except you could have skipped creating that extra value procedure getAscii and just fed the guts of that procedure into the place you call it. That would have the readability benefit of showing where the Ascii text is coming from.

stream of 8 bytes of audio meter values with 2 bytes 0x08,0x08 header
press button and ascii byte values of 8 bytes with 2 bytes header of 0x07,0x07 header

I’m not sure about how I’m testing getHeaderStart != 0
does a TO-RESULT block return 0 if it has nothing ?

nothing getting to audio meters or text output…

RXaudioFilenames_BT(2).aia (17.0 KB)