Hi guys, in this tutorial we'll see how to implement an Android App and an Arduino sketch that exploit UDOO's potentials thanks to the ADK (Accessory Development Kit). Take it as a learning excercise, because we're just using the very basic Hello World you previously saw with Arduino. This time however, the LED will be turned on via an Android App. The Accessory Development Kit provided by Google allows to building accessories for Android devices based on the Arduino framework so with UDOO we have the Android device and his Arduino accessory in the same board. There’s an Arduno Due on UDOO so we can use the Google ADK 2012. What we need is: UDOO MicroSD with Android preinstalled LED The development environments: Android SDK and Arduino IDE, installed on an external PC Knowledge of Android App programming If you've met all the above requirements, let’s boot Android! And let's understand how the Sam3x Arduino controller is connected to IMX6, where Android runs. Using the ADK, the communication between i.MX6 processor running Android and SAM3x8E processors is not made through the shared serial port. It comes through the processors’ native USB OTG bus, instead. i.MX6’ s native USB OTG port can be switched between SAM3X8E microcontroller and external microUSB port CN3. The switching is managed by two i.Mx6 pins.. It is necessary to power OTG USB bus by plugging the jumper J2, which enables the voltage supply line of the bus. In this configuration i.MX6 processor communicates with SAM3X8e using AOA protocol. To do this you must program SAM3X8E with a sketch including some specific libraries and then install on Android an app configured to use AOA protocol. At boot the connection is between the two processors, plugging an USB cable to CN3 connector will have no effect, since CN3 is disconnected. For further informations you can check the dedicated guide to Android + Arduino ADK programming. Now go in the setting -> developer options and click on External OTG port enabled Now we can plug the microUSB cable to che CN3 connector, a dialog pop-up will then appear and you have to allow the debug for your external computer. Check the box for always allow from this computer. Once you've allowed the debugging let's see another way to connect UDOO to your external pc through the Wi-Fi. To do that, we just installed an app that enables adb over wifi, there are some in the store. We installed adbWireless. Once you've prepared your development environment, let's see how to build our Android APP. For the Android app we use the USB Accessory mode that allows users to connect USB host hardware (the Arduino side of UDOO) specifically designed for Android-powered devices. The Accessory mode feature is supported by Android since the 3.1 version (API 12). When the Android-powered device is in USB accessory mode, the connected USB hardware (an Android USB accessory in this case) acts as the host and powers the bus. These are the needed components of the App: AndroidManifest.xml The following list describes what you need to add to your application's manifest file before working with the USB accesory APIs Include a <uses-feature> element that declares that your application uses the android.hardware.usb.accessory feature. Code: <uses-feature android:name="android.hardware.usb.accessory" /> Set the minimum SDK of the application at least to API Level 12. Since UDOO comes with the last Android image with the 4.2.2 version the targetSdkVersion is 17. <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="17" /> If you want your application to be notified of an attached USB accessory, specify an <intent-filter> and<meta-data> element pair for the android.hardware.usb.action.USB_ACCESSORY_ATTACHED intent in your main activity. The <meta-data> element points to an external XML resource file (res/xml/accessory_filter.xml) that declares identifying information about the accessory that you want to detect. <activity …… <intent-filter> <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter" /> </activity> res/xml/accessory_filter.xml In the XML resource file, declare <usb-accessory> elements for the accessories that you want to filter. Each<usb-accessory> can have the following attributes: manufacturer model version The same attributes have to declared in the Arduino sketch running on SAM3X Code: <?xml version="1.0" encoding="utf-8"?> <resources> <usb-accessory manufacturer="Aidilab" model="UDOO_ADK" version="1.0" /> </resources> UDOOBlinkLEDActivity.java The package you need to import is android.hardware.usb. This contain the classes to support the accessory mode Code: import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager; Here is a summary of the relevant code for handling USB connections : mUSBManager = (UsbManager) getSystemService(Context.USB_SERVICE); ….. UsbAccessory acc = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); Before communicating with the USB accessory, your applicaton must obtain permission from your users. To explicitly obtain permission, first create a broadcast receiver. This receiver listens for the intent that gets broadcast when you call requestPermission(). The call to requestPermission() displays a dialog to the user asking for permission to connect to the accessory This is the Broadcast Receiver code: Code: private static final String ACTION_USB_PERMISSION = "org.udoo.androidadkdemo.action.USB_PERMISSION"; …. private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { UsbAccessory accessory = (UsbAccessory)intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY); if (intent.getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED, false)) { openAccessory(accessory); } else { Log.d(TAG, "permission denied for accessory "+ accessory); } mPermissionRequestPending = false; } } …. To register the broadcast receiver, put this in your onCreate() method in your activity: Code: mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); registerReceiver(mUsbReceiver, filter); To display the dialog that asks users for permission to connect to the accessory, call therequestPermission() method: mUsbManager.requestPermission(accessory,mPermissionIntent); To communicate with the accessory you need a file descriptor to set up input and output streams to read and write data: private void openAccessory(UsbAccessory accessory) { mFileDescriptor = mUsbManager.openAccessory(accessory); if (mFileDescriptor != null) { mAccessory = accessory; FileDescriptor fd = mFileDescriptor.getFileDescriptor(); mInputStream = new FileInputStream(fd); mOutputStream = new FileOutputStream(fd); ….. We use the output stream to send message to the Arduino processors of UDOO. yte[] message = new byte[1]; ... message[0] = (byte)1; ... mOutputStream.write(message); For example the application sends the byte ‘1’ in order to instruct the accessory to turns on the LED. Now, we just set up, compile and Upload our Arduino Sketch. UDOOArduinoADKDemo.ino The accessory code must make a few calls to initialize USB connectivity, including setting the accessory identification strings: Code: char descriptionName[] = "UDOOAndroidADKDemo"; char modelName[] = "UDOO_ADK"; char manufacturerName[] = "Aidilab"; char versionNumber[] = "1.0"; char serialNumber[] = "1"; char url[] = "http://www.udoo.org"; USBHost Usb; ADK adk(&Usb, manufacturerName, modelName, descriptionName, versionNumber, url, serialNumber); The identification strings must match the USB accessory filter settings specified in the connecting Android application,otherwise the application cannot connect with the accessory. Once USB is enabled with code shown above, the accessory listens for connection requests. The ADK library handles listening and connection details, so the accessory calls USB.Task(); once during each loop execution. The accessory must then check for a live USB connection to process commands and receive messages. : Code: if (adk.isReady()) { adk.read(&nbread, RCVSIZE, buf);// read data into buf array if (nbread > 0) { if (buf[0] == 1) // compare received data digitalWrite(LED_PIN,HIGH); else digitalWrite(LED_PIN,LOW); … Now, Arduino will listen as an USB accessory, and if it receives the byte 1, it will turn on the LED, otherwise it will turn it off. To check that's working, all you have to do is connect the LED to the declared PIN, in this case 13. If you need more documentation, you can just check those links out: USB Accessory http://developer.android.com/guide/topics/connectivity/usb/accessory.html ADK 2012 http://developer.android.com/tools/adk/adk2.html And here you'll find the complete code. http://udoo.org/download/files/Tutorials/AndroidADKDemoBase/UDOOADKDemo.zip This was just a simple tutorial showing how to interface Android and Arduino natively on UDOO. We're already preparing some brand new tutorials to further digg into that matter, enabling you to create advanced Android powered projects!