Reputation: 65
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?
Upvotes: 0
Views: 235
Reputation: 724
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.
Upvotes: 1
Reputation: 408
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:
It would be useful a piece of your code!
Upvotes: 1