I’m working on a project involving a robot controlled by an Android tablet. The robot tablet is running Android Pie 9 and uses a main application, which manages the robot’s reactions. We have developed an Android application that overlays the main app with a transparent background, so that the main app still runs in the background. The tablet has USB ports and I’m trying to connect an Arduino Nano board. We are using Arduino Nano A000005 ATmega328, 16MHz. The application, developed with Android Studio (Java) is based on the robot’s Android SDK (that might be the source of the problem ?).
The goal is to send a message from my app to the Arduino board via serial USB connexion.
On the Android side
For the USB communication with Arduino, we first tested the connection by listing the USB devices using the UsbManager in Android. The tablet correctly identifies the Arduino Nano, here is the code I’m using :
if (device != null && connection != null && endpointOut != null) {
sendData("Hellon");
} else {
Toast.makeText(MainActivity.this, "USB Device not ready", Toast.LENGTH_SHORT).show();
}
break;
And sendData() is :
private void sendData(String message) {
if (connection != null && endpointOut != null) {
byte[] bytesToSend = message.getBytes();
int result = connection.bulkTransfer(endpointOut, bytesToSend, bytesToSend.length, 1000);
if (result < 0) {
Log.e("USB", "There was an error sending data");
} else {
Log.i("USB", "Data sent : " + message);
}
}
}
When we send data from the Android app to the Arduino using this code, the RX LED on the Arduino lights up, indicating that data is being received. And I get the log “Data sent” on Android Studio.
On the Arduino side
I made a quick code that lights the LED_BUILTIN when “Serial.available()>0” :
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Setup done");
}
void loop() {
if (Serial.available() > 0) {
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
Serial.read();
delay(2000);
}
}
It works fine when I use the serial monitor on Arduino IDE, but it doesn’t work when my robot is sending the message… even though the RX led lights up.
What I’ve tried
I’ve tried multiple types of messages, but it is still the same.
I’ve asked chatGPT about that and it told me that if the RX LED lights up and the “Serial.available() > 0” condition is still False, it may be because of the connection setup : my baud rate is 9600 on the Arduino and maybe it is not the same speed on the Android side.
How can I know (or setup) the baud rate on the Android code ?
I’ve also tried using another Arduino, but the problem persists.
How can I solve this ?
Thank you in advance for your answers !
Alexis_Leobotics is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.