Monday, June 3, 2013

Communication between Android and Arduino Due

This example demonstrate how to send String as byte[] between Android and Arduino Due, with ADK.

Communication between Android and Arduino Due
Communication between Android and Arduino Due

- In Android side (code will be available in next post):
User enter text on EditText on low part of the Android App and tap on the "Send to Arduino" button, the input text will be converted to byte[] and sent out. Any data received will be displayed on upper TextView.

- In Arduino side:
The data received from Android ADK will be sent back to Android.

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

// Accessory descriptor. It's how Arduino identifies itself to Android.
char applicationName[] = "AndroidADKCommunication"; // 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[] = "";
USBHost Usb;
ADK adk(&Usb, companyName, applicationName, accessoryName,versionNumber,url,serialNumber);
// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
int analogInputA0 = A0;   //ADC input
int analogOutput0 = DAC0;  //DAC output
int analogOutput1 = DAC1;  //DAC output

int varA0;
uint8_t adkVarA0[4];
void setup() {
    analogReadResolution(12);  //set ADC resolution to 12 bits
    pinMode(led, OUTPUT);
    //Indicate start of program
    digitalWrite(led, LOW);
    digitalWrite(led, HIGH);
    for(int i = 0; i <= 2; i++){
        digitalWrite(led, HIGH);
        digitalWrite(led, LOW);
#define RCVSIZE 128
void loop() {

    uint8_t buf[RCVSIZE];
    uint32_t nbread = 0;
    if (adk.isReady()){
        digitalWrite(led, HIGH);
        , RCVSIZE, buf);
        if (nbread > 0){
            adk.write(nbread, buf);
        digitalWrite(led, LOW);


Related: Android side code for Communication between Android and Arduino Due

No comments:

Post a Comment