Android Interface With ADK: The Android Part

After hacking on the arduino site of the code, now we can go to the android part

For android side, it is a easier in a way. There is nothing to bypass, just make sure to have the right version of android OS. You need 2.3.4 above to be able to use the Accessory API. Though using the API have quite a lot of stuff to setup. And I am a bit confused with the documentation(which is why I share my notes here)

Screen Shot For ADK Test Project

Android Manifest

The manifest is in: https://github.com/sweemeng/ADK-Test-Project/blob/master/AndroidManifest.xml

It is pretty standard across all android app, except “com.android.future.usb.accessory”. Because I am still on Android 2.3.7.

Metadata

Then add a accessory.xml entry, under res/xml. in github https://github.com/sweemeng/ADK-Test-Project/blob/master/res/xml/accessory_filter.xml

Remember from the last post on arduino, the definition goes here.

Now to the code

The source code can be found at github: https://github.com/sweemeng/ADK-Test-Project/blob/master/src/adkTestProject/main/ADKTestProjectActivity.java

Library Used

Essentially openaccessory api open the device as a ParcelFileDescriptor object, which is why you need to import java FileInputStream and FileOutputStream, so you can read and write data to the arduino board

BroadcastReceiver is used because an Android Accessory can broadcast intent to android to launch an android app. IntentFilter and PendingIntent are used to setup  the permission necessary for an App is used. More on that later

Variable(For reference)

Now defining variable

Just so you can reference what object is the variable name is.

BroadcastReceiver

Remember an ADK device can send intent to an android app. So that when you plug your device, an app that is use to run with it can be launch. Essentially what this does is, when an intent is receive from the ADK, setup the app to read from ADK, via OpenAccessory method, or if it is detached, clean up all the connection via Close necessary.

line 8: Get the permission and action from the Arduino Board

line 11: Get accessory connection

line 13 – 14 : Get extra permission, from the Arduino Board

line 16: Open Accessory function to setup all the code and interface to the Arduino Board

line 27: from action UsbManager will check if there the arduino board id detached, then do cleanup

line 31-32: If cleanup is still exist, then call cleanup function

Initialize Accessory Interface

 

This is how you setup to read from ADK. Accessory will be defined from an intent from the ADK, mUsbManager will open the accessory, and return  ParcelFileDescriptor, which will be pass to FileInputStream and FileOutputStream.

FileInputStream is use to read from the ADK, for this project I use FileOutputStream to write to the ADK Board, which start the thread to have them run in background.

Close Connection to Device

Close is easy just close the file descriptor, and set variable to null

Send Commend to Device

Remember I say on the last post that,  you just send an array of byte or read an array of bytes , it is the same here.

line 4: define the holder for data to be send to the Arduino

line 7-15: just to setup the value send to be at most 255, the LED only can access such value. It is also nice to make sure value send from app to arduino, because once go to hardware, thing will be fried(shit just turn real!!!)

line 17-19: just to assign value, remember we send bytes to Arduino, so remember to convert accordingly

line 24: send the byte array to arduino

Retain Devices

Retain ADK accessory to be reuse by new activity

Initialize the app to use Accessory

This register the Permission needed and the BroadcastReceiver, also setup the retained Accessory object

 

To resume an activity

line 7-8: so the connection already closed, it is cool, don’t do anything

line 11-13: get all connected device, if there is nothing return null, else, return the first one(there is only one anyway)

line 17-21: permission checked out, you can open(OpenAccessory) and send value to the arduino(sendCommand)

line 25-29: request permission, again

Using the Using sendCommand

So just get value from UI change, then send the value to send command. It is a seekbar, that value is progress, and we already save color value anyway.

We’re done

I hope this is useful, though I think I might confuse people more, so just leave a comment if there is any question