Hardware Reference
In-Depth Information
The blinkLED function is run every time the toggle button is pressed. This function creates an array of 1 byte that
is set based upon what state the toggle button is in. The toggle button's state is handled by the widget class, so code
does not need to be added. Once the array value is set, the write function is called to send the byte to the Arduino.
Any number of buttons can be created to send any amount of data (for example, to create a directional pad to use an
Android device as a robot controller).
Listing 4-6. CH4ExamplesActivity.java, Part 7 of 7
private void registerUIobjects(){
buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLED);
}
public void blinkLED(View v) {
byte[] BytestoSend = new byte[1];
if (buttonLED.isChecked())
BytestoSend[0] = (byte) 1; // button shows current LED State "ON"
else
BytestoSend[0] = (byte) 0; // button shows current LED State "OFF"
write(BytestoSend); // sends the byte to the ADK
} // end void blinkLED(View v)
} // end public class CH4ExamplesActivity
Verifying the Code
Now that both parts of the example are complete, the Mega ADK board should already have the sketch from Listing 4-1
installed. The code for the Android needs to be installed on a supporting device. You can accomplish this by running
the application from the Eclipse IDE with the Android device connected via the ADB or by manually installing it from the
.apk file located in the bin folder of the workspace/ project name folder. When the application is installed on
the Android device, make sure that it can run; it will automatically start if the program was installed by Eclipse. The
program will have a single button centered on the screen; the button will be in the off state.
Close the program for now and plug the Mega ADK into the computer to power the board. For debugging on
the Arduino side, start the serial monitor at 115200 baud. After the Arduino is powered on, plug the Android device
into the host side with the appropriate USB cable (in most circumstances, the cable used to charge the device will
work). If all is working, a pop-up should appear on the Android screen asking for permission to start the application
shortly after the board is plugged in. If the pop-up does not appear, try pressing the reset button on the Arduino or
reconnecting the USB cable to the Android device.
Once the program is running, the toggle button should now be able to turn the LED off and on over the USB
connection. The serial monitor should have printed the connection initiation and should echo a 1 or 0 every time the
toggle button is pressed. If the ADB is available over wireless, type adb logcat into a terminal and check the printing
log for mentions of the MEGA_ADK program.
if using completed android project from apress check that the Build target is Googleapis api level 10 located in
project ➤ properties ➤ android ➤ Build target.
Note
Completing the Framework
Now that Arduino and Android have been introduced to each other, the next example expands on the code from last
example. The next example uses the same project and introduces the handling of two-way communication by creating
an ADK monitor with similar functionality to the serial monitor included with the Arduino IDE.
 
 
Search WWH ::




Custom Search