Hardware Reference
In-Depth Information
the host port on the arduino Mega adK provides some power to signify a live connection to the android device;
however, it is not capable of providing a charge. as such, extra circuitry is needed to introduce enough power to charge a
connected device.
Note
Connect the Arduino as in the preceding figure; it needs to be connected to the computer for programming,
and then later to the Android via a micro USB-to-USB A cable after the Android application has been completed.
Step through Listing 4-1 and upload the code to the Mega ADK board, remembering to switch the board type. To
select the board type in the Arduino IDE, choose Tools Board Arduino Mega 2560 or Mega ADK. In conjunction
with the connection between Android and the Arduino, the code also sets up a serial connection to the computer for
debugging purposes at a baud rate of 115200. The code will print ready to the serial port when the setup function has
finished, and other debugging information will be printed when the code is connected to a working application later.
Listing 4-1. Arduino Code for Connecting to an Android Application
#include <AndroidAccessory.h> // needed library to work with ADK connections
// initialize the accessory object with identifying data
AndroidAccessory ADK("Manufacturer", "Model", "Description",
"1.0", "Pro Arduino", "0000000012345678");
void setup() {
Serial.begin(115200);
ADK.begin(); // start the connection to the Android
pinMode(13, OUTPUT);
Serial.print("ready"); } // end setup
void loop() {
if (ADK .isConnected()) { // check for live connection
if (ADK.available() > 0){ // check for incoming data
Serial.println(ADK .peek()); // print incoming data for visible inspection
if (ADK.read() == 1) // pin HIGH for an incoming value of 1
// everything else pin is low
digitalWrite(13 , HIGH);
else
digitalWrite(13 , LOW);
} // end if (ADK .available() > 0)
} // end if (ADK .isConnected())
} // end void loop()
The Android ADK Application
Programming apps for Android can be an involved process, especially for widespread or commercial programs.
This section provides a crash course in the development of Android applications; some of the ADK code is based on
work from AllAboutEE ( http://allaboutee.com ) . The focus is getting Arduinos to communicate with the Android
devices—note that some of the fundamentals of Java and some advanced features of Android application programming
may be glossed over. The hardest part for some when deciding to start writing applications for Android is the change
in languages—Arduinos are coded in C++ and Android is developed in Java. For others, the event-driven GUI
development might be a new experience. Chapter 3 introduced some of the concepts of event-driven applications and
working with two different code structures. Aside from Java, Android also introduces XML resources; XML is another
code language that is necessary when developing Android applications. Four different XML files are used when
 
 
Search WWH ::




Custom Search