简体   繁体   中英

Android communication via Bluetooth with Arduino

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?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM