Receiving String fromHC-05

Hello everyone.

I have an Arduino Mega 2560 and Bluetooth module (HC-05).
I want to send a string ("Hello World") from Arduino via HC-05 to my phone ( Galaxy S9) on Screen 2..
I encountered a problem in which I always get weird message on label 3 and 4.
Sometimes it is question mark, sometimes it is "hello world hello" and sometimes it is "q".
I do not know how to fix it. Anyone who can help would be greatly appreciated.

from_hc_05_to_mitappinventor.aia (17.9 KB)

This is the Code on Arduino IDE


This is the received Messages

Thank you @Patryk_F for responding.

I did what you asked, after selecting the Mac address nothing changed: still no connection.
And when I tried to select the Bluetooth module again, the list picker did not even open.

What I showed has nothing to do with the fact that you cannot connect to the Bluetooth device. You didn't mention in the first post about the lack of connection with the Bluetooth module. You mentioned incorrectly received messages. My solution fixes incorrectly received messages.

If you have other problems apart from those you mentioned in the first post, please write about them in detail.

1 Like

Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

To receive YAML format messages, test if the incoming message contains ':' . If true, split it at ':' into a list variable, and find the prefix in item 1 and the value in item 2.


1 Like

I notice you are working in Screen2.

Switching Screens breaks the Bluetooth connection.

For such an app, it is better to stay in Screen1 and use Vertical Arrangements instead of Screens.

1 Like

Hi @ABG, thanks for your reply.

  1. I will not be using just one Bluetooth module. I am going to use two Bluetooth Modules: one on Serial1 and the other on Serial3 because I noticed that changing screens disconnects the phone from Bluetooth.

  2. I do not understand why using AI2 code on screen 2 does not always make me able to connect to HC-05, although when I try another AI2 code, it connects with no problem.

  3. I have tried everything including your suggestions but it does not work.

  4. When I use Call BT Receive Text NumberOfBytes [ -1] instead of [call BT1 bytes available to receive ] the application on my phone gets blocked and I have to exit.

I would be grateful if you could correct the code I am about to send you, it is important for my thesis.

Thanks in advance.
from_hc_05_to_mitappinventor.aia (18.0 KB)

Sorry, I can't support multiple screens using Bluetooth.

Search the forum for such attempts.


Ok, no Problem. I will use only one Screen.
Could you help me to change the codes on AI2 ?

For multiple values per line:

Here is an updated blocks sample illustrating these ideas ...

BlueTooth_delimiter_sample.aia(3.4 KB) global message


1 Like
  1. I don't see the point of using two BT modules...
  2. It's simple. Because you don't disconnect from the BT module before leaving the screen1. Therefore, before leaving the screen1, disconnect the BT connection.
  3. I don't see these changes in the recently shared aia, you are rather waiting for someone to make the changes for you.
  4. This may happen if you don't set DelimiterByte to 10 as we suggested. This may also happen when you showed us a different Arduino code than the one you actually use.
1 Like

hi Mr @Patryk_F

Thanks for your Feedback

  1. I'm not trying to get anything done for me, it's just that I didn't understand certain points
    from Mr @ABG. Also, I have to solve the problem before next week.

  2. I did not change the Arduino code.

  3. I am new to AI2, so I am trying my best to learn as fast as possible, add to that a lot of
    Updates to the platform have happend , as the examples that I have do not match with the new Extensions.

  4. I already told you that when I put " receive text number of bytes ] [-1], the application does not respond anymore. I tried it again and it still does not respond. In addition to that
    the Bluetooth module will only connect if Bytes available to receive >= 0.

  5. The clock is set to 300ms like the Arduino code ( delay (300)).

I will upload the example . Thank you for your Time

BlueTooth_delimiter_sample (1).aia (13.1 KB)


Your sketch sends '|' but your app splits at '!'.

Do you see the difference?


Everything already said by @Patryk_F and @ABG is definitely correct, please be sure to follow their hints.

I would only add some info about the Arduino side:
Please be aware that the delay() function in the Mega implementation completely blocks the CPU, that becomes irresponsive to any other activity, among with also interrupts are blinded off. This will become a big issue when your app will evolve and you will also transmit from the App to the board. To overcome this issue, as already explained in many other topics, you'd better use the millis() function instead, to create your own Delay() function.
Something like:
void Delay(unsigned long TimeToWait)
unsigned long now = millis(); // read the internal clock
while ( (millis()-now) < TimeToWait); // do nothing and wait
example of use is
Delay(1000); // waits for 1 second while leaving alive the CPU

As a second hint:
are you sure that the HC05 baudrate is set @38400 ? Not all the HC05 are factory set at that baudrate. The majority of them is set @9600. The baudrate 38400 is the fixed baudrate when you want to program the HC05 parameters and you enter the AT mode, but most probably the working baudrate is 9600. Therefore you should set the Serial3 baudrate accordingly.

Third: on the Mega board the Serial3 pins are Tx pin 14 and Rx pin 15 ; they shall be connected "crossed" to the HC05: i.e. Tx (14) to Rx of the HC05 and Rx(15) to the Tx of HC05. Preferably between the pin 14 and the HC05 Rx you should put a voltage divider, so to reduce the voltage to 3 volts (more or less) since the HC05 works @3.3V. This is not strictly mandatory, but to not overload the HC05 input capability it is better to do this protection, sooner or later.

Lastly, to be sure that the Arduino side is working fine (and before getting crazy in looking for ghosts on AI2 side) you could use a ready made app like Serial Bluetooth Terminal that you can download freely from Google Playstore By installing this app on your Android device, you will be able to communicate with the Mega, without your app. When you'll be done with the Arduino side (i.e. the Mega correctly sending data to the Serial Bluetooth Terminal app) you'll be at the 50% of the job, and you'll focus your efforts only on the AI2 side.

Best wishes.


What Android do you use for testing? Because I see that you set Delimiter Byte to 10, but only for Android 12 and newer.
Apart from that and what ABG mentioned, the blocks look good. As Uskiara mentioned, the problem may be incorrectly set speed in bt. With a slightly changed speed, some of the sent characters may be correct and some false.

This would be correct:

The end of line character may be corrupted or may simply not be present and that is why the application hangs.

1 Like

Thank you @ABG @uskiara @Patryk_F

It worked. It turns out that the problem lies in the "delay" function.

@Patryk_F I have Android Version 10



Nice to read that you solved !
Best wishes for your thesis :muscle:

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