Tuesday, March 5, 2013

Pre-Hello World for ADK

It's a very simple incomplete "Hello World" for ADK, working on Arduino Due with Android Device.



In Arduino Due side, it simple loop to detect if adk isReady(). If adk ready turn on L led, if not turn it off. Also define the URL to download the app if not installed.

#include "variant.h"
#include <stdio.h>
#include <adk.h>

// Accessory descriptor. It's how Arduino identifies itself to Android.
char applicationName[] = "HelloADK"; // the app on your phone
char accessoryName[] = "Arduino Due"; // your Arduino board
char companyName[] = "Arduino-er";

// Make up anything you want for these
char versionNumber[] = "0.0";
char serialNumber[] = "1";
char url[] = "https://sites.google.com/site/arduinosite/exercise/helloadk/HelloADK_0.0.apk";

USBHost Usb;
ADK adk(&Usb, companyName, applicationName, accessoryName,versionNumber,url,serialNumber);

// Pin 13 has an LED connected on most Arduino boards.
int led = 13;

void setup() {
    Serial.begin(9600);
    cpu_irq_enable();
  
    pinMode(led, OUTPUT);
    //Indicate start of program
    digitalWrite(led, LOW);
    delay(2000);
    digitalWrite(led, HIGH);
    for(int i = 0; i <= 2; i++){
        digitalWrite(led, HIGH);
        delay(250);
        digitalWrite(led, LOW);
        delay(250);
    }
}

void loop() {
  
    Usb.Task();
    
    if (adk.isReady()){
        digitalWrite(led, HIGH);
    }else{
        digitalWrite(led, LOW);
    }
    
}

In the Android side, it only prepare itself as a usb accessory and declare intent-filter of "android.hardware.usb.action.USB_ACCESSORY_ATTACHED". Such that when a matched Arduino connected, it will be pop-up. That's all it done.

New a Android Application Project, Compiler With Google APIs.

Modify AndroidManifest.xml

- add <uses-festure> of "android.hardware.usb.accessory".
- add <uses-library> of "com.android.future.usb.accessory".
- add <intent-filter> to define register action of "android.hardware.usb.action.USB_ACCESSORY_ATTACHED".
- add <meta-data> link to resource of "@xml/myfilter".

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloadk"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <uses-feature android:name="android.hardware.usb.accessory"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.android.future.usb.accessory"/>
        <activity
            android:name="com.example.helloadk.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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/myfilter"/>
        </activity>
    </application>

</manifest>


Create /res/xml/myfilter.xml to define resource of usb-accessory. Notice that manufacturer of "Arduino-er" match with companyName, model of "HelloADK" match with applicationName, and version of "0.0" match with versionNumber in Arduino side.

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <usb-accessory
        manufacturer="Arduino-er"
        model="HelloADK"
        version="0.0"/>
</resources>


Next:
- Hello World ADK: Communication between Arduino Due and Android device


No comments:

Post a Comment