I recently bought an Arduino ADK board, after downloading the sample code I was overwhelmed with the amount of C and Java code I saw. Furthermore the sample code requires that you have all these sensors that are sold separately on top of the already expensive board, and this is supposed to be a beginners board?
But enough with the rant, I’ve managed to trimmed the C and JAVA down to what is only necessary to blink the built in LED in the Arduino, the full code and explanation is here, please spread it around.
The App’s Interface
The Android app consists of a toggle button which we’ll use to toggle the state, on or off, of the LED. In Android user interfaces are called layouts, so let’s make a layout with a toggle button.
Android app interface as seen in phone
The ToggleButton xml tag has several properties including dimensions (width and height), position, text size and the ID and onClick attributes. The ID attribute is used to identify inputs (buttons, check boxes etc) in our Java code, the onClick attribute is the function which will executed upon a click.
In the code above, our button’s ID is toggleButtonLED and the function to execute when it’s clicked is blinkLED.
Asking For Permission
Whenever you download an app, if you don’t just click OK on everything without reading, you’ll notice that it lists some permissions that it requires from the device. All these permissions are listed in the Android manifest file ("manifest"? LOL sounds like a peace treaty doesn’t it?), we need to list our USB permissions in this file.
Be sure to change the minSdkVersion to the Android SDK version your project is using.
How To Route Your USB Device To The Correct Application
The message you'll get when you connect your board to the Android device
Whenever you connect your board the Android device (phone or table) will need to know which app the board uses, we do that with this file.
You may change the values in the attributes but they will have to same as in the Arduino file.
How To Handle The Clicks, Send Data To The Board
The following code is part of the ArduinoBlinkLED.java file, don’t worry about where it goes for now, I will tell you that later, for now just understand the following explanations.
To make our button be recognized by Java we need to store it in an object. So we declared our object as a ToggleButton object:
private ToggleButton buttonLED;
Then we store our button in the buttonLED object:
// toggleButtonLED is the name we used in the layout/main.xml code above.
buttonLED = (ToggleButton) findViewById(R.id.toggleButtonLED);
We also need to load the layout into our app.
// main comes from our interface's file name: main.xml
To handle the button clicks we’ll need to make the function blinkLED, just as we promised in the layout/main.xml code above. This is the function’s code:
What we are doing here is we are making a byte array which will hold the value 0 if the button is checked and the value 1 if it’s not checked, read the comments in the code. The byte is sent to the Arduino ADK board by the write function.
We’ll continue in the same file on the next section.
Android Arduino Communication
The code that handles USB communication is standard code you’ll need for all your apps, you can either not worry about it or simply check out each function and by the variable names am confident you’ll understand what it’s doing, also in the code to handle clicks is included here. Am sorry I can’t explain each piecebut as you’ll see it could take a while, but the important part is what I explained above.
We only need ONE java file, not 100’s like the "demo" version and here it is.
How To Receive Data From The Android Device
Here comes the easy part. This is the only Arduino code you need. Notice how the manufacturer, model and version values matches that of our accessory_filter.xml file, if any of these change your board will not know which app to load.
Just in case you don’t know where the code I gave you goes: