Hardware Reference
In-Depth Information
ASCII—a value of 0x61 sent will print an a . The Android app is ready to accept incoming messages from any Arduino
sketch that is using the AndroidAccessory.h library, giving the same feeling as the serial functions.
the AndroidAccessory.h functions replicate the serial functions and inherit the functionality of print and
println , and offer formatting options such as BIN , DEC , and HEX .
Note
Arduino
The Arduino could be left up to the imagination at this point, but the next example sets up a quick echo from the serial
monitor to the ADK application and vice versa. This is handled by two while loops that will write the incoming data
when available from one side to the other. This example does not require any extra hardware connected to the pins on
the Arduino.
Load the sketch in Listing 4-15 to the Arduino Mega ADK.
Listing 4-15. Modified Software Serial Sketch to Link the Echo Serial to the ADK and Vice Versa
#include <AndroidAccessory.h>
AndroidAccessory ADK("Manufacturer", "Model", "Description",
"1.0", "Pro Arduino", "0000000012345678");
void setup() {
Serial.begin(115200);
ADK.begin();
}
void loop() {
if (ADK.isConnected()) {
while ( Serial.available() > 0){
ADK.write (Serial.read());
}
while ( ADK.available() > 0){
Serial.write(ADK.read());
}
} // end if (ADK.isConnected)
} // end loop
Verifying the Code
With the application on the Android device and the sketch installed on the Arduino, plug the USB from the computer
into the Arduino and start a serial monitor at baud 115200. Once the monitor is ready, make sure that the application
is not currently open on the Android device and plug the Mega ADK host side into the Android USB. The application
should automatically ask for permission to start with the attached accessory. If after a few second the pop-up does not
appear, try reconnecting the USB on the Android side or pressing the reset button on the Arduino. Once the program
starts, the serial monitor should have printed some connection information, and data can be entered into either side's
input boxes and sent, and should appear on the output box on the other device.
 
 
Search WWH ::




Custom Search