Control an Android device from Arduino

In a we discussed how to control Arduino from Android device. In this article we will discuss how to Control an Android device from Arduino.

To Android device we will send 2 variables: random number and state of the button.

Arduino Code

#include "spi.h"
#include "adb.h"

// Adb connection.
Connection * connection;
long lastTime;
byte rndNum;    // Random number
int buttonState = 0;  // State of the button
uint16_t tosend;      // Data send to Android
const int buttonPin = 2;  // Button PIN

void setup()
  pinMode(buttonPin, INPUT);

  // Initialization ADB.

  // Open an ADB stream to the phone's shell. Auto-reconnect. Use any unused port number eg:4568
  connection = ADB::addConnection("tcp:4568", true, NULL);


void loop()
  if ((millis() - lastTime) > 100)  // Every 100 ms
    rndNum = random(1, 99);    // Random number from 1 to 99
    buttonState = digitalRead(buttonPin);  // State of the button

    tosend = (rndNum << 8) | buttonState;  // Make a word from 2 bytes

    connection->write(2,(uint8_t*)&tosend);  // Send 2 bytes

    lastTime = millis();

  // Poll the ADB subsystem.

Android code

For our Activity we need 2 elements: TextView and Switch.


Source code of MainActivity:

package com.example.arduino53;


import org.microbridge.server.Server;
import org.microbridge.server.AbstractServerListener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends Activity {

    private int Ard_data1 = 0;
    private int Ard_data2 = 0;
    public final String APP_NAME = "arduino53";

    Server server = null;

    public void onCreate(Bundle savedInstanceState) {
        // Create TCP server (MicroBridge LightWeight)
          server = new Server(4568); //Port
      } catch (IOException e)
          Log.e(APP_NAME, "Unable to start TCP server", e);

        server.addListener(new AbstractServerListener() {

            public void onReceive(org.microbridge.server.Client client, byte[] data)
                Log.d(APP_NAME, "data0:"+data[0]+"; data1:"+data[1]);
                if (data.length<2) Log.e(APP_NAME, "The data less than 2 bytes:"+data.length);

                Ard_data1 = data[0];
                Ard_data2 = data[1];

                //Any update to UI can not be carried out in a non UI thread like the one used
                //for Server. Hence runOnUIThread is used.
                runOnUiThread(new Runnable() {
                    public void run() {
                        new UpdateData().execute(Ard_data1,Ard_data2);

    protected void onDestroy (){

    class UpdateData extends AsyncTask<integer, integer,="" integer[]=""> {
        // Called to initiate the background activity
        protected Integer[] doInBackground(Integer... ArdState) {
            return (ArdState);  //Return to onPostExecute()

        protected void onProgressUpdate(Integer... values) {
            // Not used in this case

        protected void onPostExecute(Integer... result) {
            //Log.d(APP_NAME, "onPostExecute0:"+result[0]);
            //Log.d(APP_NAME, "onPostExecute1:"+result[1]);

            TextView txt_btn_Arduino = (TextView) findViewById(;
            txt_btn_Arduino.setText(String.valueOf(result[1]));    // Print a random number from Arduino to activity

            Switch switch1 = (Switch) findViewById(;

            if(result[0] == 1){
                switch1.setChecked(true);       // Toggle state switch
            else switch1.setChecked(false);

In this code uses method onReceive() which is called whenever data comes from Arduino. To update UI elements on Activity we use a class AsyncTask.

Download source code for Arduino, Android and Libraries

8 thoughts on “Control an Android device from Arduino”

  1. I wanted to start this project on my computer, but i didn’t get any data from my android to arduino…
    Can you help me?

  2. i wanted to run this project between arduino uno r3 and android os this possible?
    or just use mega adk board?

  3. Hi, I want to control VLC player from Arduino, play + pause buttons, and potentiometer to control volume. Possible?

  4. it gives error in here

    public void onReceive(org.microbridge.server.Client client, byte[] data)

    in the client parameter. why?

Leave a Reply to aziz Cancel reply

Your email address will not be published. Required fields are marked *