Android And Arduino Bidirectional Communication
Overview
Visit our Tutorials section to learn more about: Android And Arduino On UDOO: Bidirectional Communication.
Hi guys,
in this tutorial we'll extend our Android ADK communication skills. You've already learned how to interact with Arduino trough Android in our previous Android and Arduino Hello World Tutorial, and now we'll understand how to exploit bidirectional communication capabilites in Android environment.
First, let's clear what is bidirectional communication: trivially speaking, we have bidirectional communication patterns when both Android and Arduino Compatible Sam3X (in this particular scenario, but the principle is valid also elsewhere) talk to each other, both sending and receiving data.
So, let's start! In this tutorial we just use the exact same code of Android and Arduino Hello World Tutorial, plus adding the readings from a Sharp 0A41SK ping IR sensor (which you may suspect we love so much, since we used it also here and here ).
UDOOArduinoADKDemoBidirectional.ino
First we connect our Ping IR Sensor, you can use the reference wiring shown here and edit our Arduino Sketch to read distance readings and send it via OTG. So, in the Arduino sketch we add code to read values from the analog sensor, cut off the outlier values to cut the noise for this specific sensor, and send the bytes with adk method write()
distance = analogRead(IR_PIN);
if (distance < 100) {
distance = 100;
} else if (distance > 900){
distance = 900;
}
bufWrite[0] = (uint8_t)(2076/(distance - 11) + 4); // calculate the distance in centimeters
adk.write(sizeof(bufWrite), (uint8_t *)bufWrite); // write the distance to Android
MainActivity.java
Then we edited the Android app adding a piece of code that reads the messages received from the Arduino sketch and display them in a textview. The activity start an AsyncTask to set up the reading from Arduino in another thread, avoiding to lock the main UI thread.
...
@Override
public void onResume() {
super.onResume();
mAdkManager.open();
mAdkReadTask = new AdkReadTask();
mAdkReadTask.execute();
}
/*
* We put the readSerial() method in an AsyncTask to run the
* continuous read task out of the UI main thread
*/
private class AdkReadTask extends AsyncTask<Void, String, Void> {
private boolean running = true;
public void pause(){
running = false;
}
protected Void doInBackground(Void... params) {
while(running) {
publishProgress(mAdkManager.readSerial()) ;
}
return null;
}
protected void onProgressUpdate(String... progress) {
distance.setText((int)progress[0].charAt(0) + " cm");
}
}
...
That's it! As you can see, this is quite a trivial example. Take it as a little suggestion, we're sure you can unleash all your creativity and create amazing projects with this basic principle! If you just want to take a look at the complete code, feel free to land on our Git repository.