Question about BT example

rfsingh81

Member
Licensed User
Longtime User
I am trying to play with the BT example and on the other end I have a microcontroller which is receiving the data from this module. I am facing a problem:

My Micro is waiting for decimal number. If the decimal number is 4 it switches on 1 led if 5 then the other. Now My tablet is connected to the module, and when I type 4 on the chat window, I HAVE TO PRESS IT TWICE for the other side to accept it as 4. Same happens with 5.

I would like to ask what happens on the app side. I am more into programming micros and trying to build an app for the first time. I am interested in the following code and thinking if this has something to do with the problem:
B4X:
Sub btnSend_Click
   AStream.Write(txtInput.Text.GetBytes("UTF8"))
   txtInput.SelectAll
   txtInput.RequestFocus
   LogMessage("Me", txtInput.Text)
End Sub

I am sorry but I am not into this programming a lot, so don't know if the above makes perfect sense but I tried to delivery my problem here in a hope that it can be understood. But I welcome any clarification if needed. Thanks
 

KitCarlson

Active Member
Licensed User
Longtime User
I have not used ToastMessageShow, there may be delays and persistence in using it to display AStream_NewData Buffer. It may create problems, in embedded work similar to using printf or delays in an ISR, a no-no. Think of AStream_NewData as a receive ISR.

Things to consider:
AStream_NewData Buffer contents may not be complete, and may happen multiple times to bring in all the data.
To handle this, convert the bytes to strings, use "&" to join the strings.
There are string functions to parse the strings looking for an end marker, that you place in the transmitted code for that purpose.
Then use string functions when data complete, to copy the desired string section to be processed, and delete it from the string buffer, saving what is left, it may be part of the next received data.

Helpful string functions are: &, IndexOf(), StartsWith(), Length, SubString2() ...

There are many other ways to do this too.
 
Last edited:
Upvote 0
Top