0

When I try to send a command String from Android to Arduino right after Bluetooth

connection is established, Arduino doesn't send response String[I think maybe

Arduino got nothing].

But if I put a Thread.sleep(1000) method before write method, It works...

What is the problem?

Am i trying to send a message before Bluetooth connection setting is not yet

ready?

2 Answers 2

1

Think of it as this: the ping pong is happening too fast that the ball is getting lost.

If you are writing before reading and reading before writing, error will occur and the string will be lost.

So yes, one way of solving it is putting some time in between like Thread.sleep

DETAILED:

So this is happening because your arduino is still sending because it has not recognized that your android actually received. During that thread.sleep (1000), your arduino recognized that android received and changes to receive mode. That is why the string is able to be read from your arduino after one second.

If you want, you could loop your sending so that arduino will receive as soon as possible. However, certain circumstances can follow from the looping like receiving redundant strings.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response :) But how can I expect time not to lose my message?? Is there any solution like throwing a trap when Arduino is ready to read??
edited my answer, if you want i can translate in korean.
Oh how kind of you! But no thanks for translating in korean. I totally understand what you want to say. Again Thanks!
1

But if I put a Thread.sleep(1000) method before write method

Try doing multiple writes after the Thread.sleep, to check if the problem occurs only at the right moment when the connection is established, or at every write!

Anyway, by my experience, I would warn you about some things:

  • Baudrate too high, try lowering it.
  • Be aware of some cheap Arduino bluetooth modules, which does not implement any kind of I/O buffering.
  • The part that reads data from your app on the Arduino is doing some kind of stuff that takes time (like I/O operations), or simply it's doing too much stuff, so some data on the serial gets lost (once it happened to me).
  • Be aware that if you are using Bluetooth Low Energy, you are bounded to 20 bytes! If you send more data, it will be lost.

It would be useful a piece of your code!

1 Comment

Thanks for your response and sorry for not posting my code... :) Your comment helps me think closer to my problem!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.