Saturday, March 9, 2013

Hello World ADK: Communication between Arduino Due and Android device

The former post demonstrate half "Pre-Hello World for ADK". It's further implemented to include communication between Arduino Due and Android Device.



  • In Arduino Due side: it monitor any incoming data from accessory, and send it back.
  • In Android side, it will send out the text when Send button clicked. And display the text received from USB.
It implement the function of bi-direction communication between Arduino Due and Android Device.

The code in Arduino side is listed here:
#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.1";
char serialNumber[] = "1";
char url[] = "https://sites.google.com/site/arduinosite/exercise/helloadk/HelloADK_0.1.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);
    }
}

#define RCVSIZE 128

void loop() {
  
    char helloworld[] = "Hello World!\r\n";
  
    uint8_t buf[RCVSIZE];
    uint32_t nbread = 0;
  
    Usb.Task();
    
    if (adk.isReady()){
        digitalWrite(led, HIGH);
        
        adk.read(&nbread, RCVSIZE, buf);
        if (nbread > 0){
            adk.write(nbread, buf);
        }
        
    }else{
        digitalWrite(led, LOW);
    }
    
}

The Android side code will be shown in next post "Hello World ADK: Android code".

Remark: The example tested run on HTC One with Android 4.1.1, but NOT work on Nexus One with Android 2.3.6.

2 comments:

  1. Hi,
    Would you know what transfer rate is achievable between the DUE and an Android device ?
    Thank you,
    Paul

    ReplyDelete