Android SDK

Send information from your Android device to Ubidots.

After doing this tutorial you should be able to communicate with our API using our Android Library within your Android application.

We have developed an example app, the Android GPS Tracker:

You can find the source code of the Android GPS Tracker in our Github page


Android is an Open-Source operating system created by Android, Inc. and bought by Google in 2005. It’s the most used operating system in mobile devices thanks to its availability for third party manufacturers to include it on their devices free of charge.


To accomplish this tutorial, you’ll need:

Setting up Android Studio

  • Download Android Studio from Android Developers
  • If you’re using Windows, just install it. If you’re using Linux, just double-click on “studio.sh” or open it via Terminal ./studio.sh. And if you’re using OS X, just drag & drop “Android Studio.app” to the Applications folder.

Preparing your Ubidots account

First first: Create a Datasource called “Android Phone” (or whatever you like) and then add variables and widgets.

  1. As a logged in user navigate to the sources tab.

  2. Create the Datasource by clicking the on the orange button located in the upper right corner of the screen.

  3. Select the Generic option.

  4. We just need to fill the data and click on the Create button.

  5. Click on the created Datasource and then on “Add new variable”. As we are going to send our battery level, we will create a variable named batteryLevel

  6. Take note of the variable ID as we are going to need it later for our application to work.

  7. Create a token under “My profile” tab. We’ll need it to access all the posibilities that our API have.

  8. Add the widget to your dashboard by clicking the orange button with the “+” symbol inside.

    • We are going to add to widgets: The first showing our battery level in a line chart.
    • And the second showing the last value of our battery

Coding and pushing data from our Android phone

Second goal: Create our application to send data from it to Ubidots!

  1. Remember I told you to download and install Android Studio? Well, now it’s time to open it. A window will popup and you’ll click on the “New project” button.

  2. Choose any name you want for the application. I will call it UbidotsApp (I don’t have a lot of imagination for names, sorry!).

  3. I also recommended you to have an Android phone (or emulator) with Android 4.0 or higher. For this tutorial I will use the API 14 of Android, that targets all the phones using Android 4.0 or higher. You can also use our API in older versions of Android, but you’ll have to learn how to use support libraries

  4. We will use a Blank Activity for this project. I know it’s more recommended to use Fragments, but this is going to be just for learning.

Android Studio will begin to create our project. It may take some time to finish that.

When it finishes, it will show us a screen like this.

You see that code? It’s XML, we use XML in Android to create our views (what the user will see). Replace all the code created by Android Studio with this::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_centerHorizontal="true" />

You just modified the view of our application, hooray! Now, we will import the Java library of Ubidots in our application. We have 2 “builde.gradle” files, we will use the first one (the one that is inside the tree of our application).

We will add the next two lines inside the dependencies brackets::

compile 'com.ubidots:ubidots-java:1.6.4'
compile 'com.google.code.gson:gson:2.3'

And it should look like this:

Now we should sync Gradle, so it can download the libraries and add them to our application.

Remember that we are going to send this information (or read it) using Internet. Android won’t let the application access to Internet without the consent of the user. So we shall ask the user for permission to use Internet. Open AndroidManifest.xml and add this line before the tag.::

    <uses-permission android:name="android.permission.INTERNET" />

At this time of the tutorial we have already modified the View, we have added new dependencies to our project and have asked the user for permission to use the Internet. What’s missing? Oh, yes! Modify the controller so it fits our bussiness logic.

Search MyActivity and then double-click on it.

Again, code everywhere! This is Java, it’s the main programming language for Android. Here’s where everything is handled, this is the heart of your application. Replace everything inside the file with this::

package edu.self.ubidotsapp;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

import com.ubidots.ApiClient;
import com.ubidots.Variable;

public class MyActivity extends Activity {
    private static final String BATTERY_LEVEL = "level";
    private TextView mBatteryLevel;
    private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            int level = intent.getIntExtra(BATTERY_LEVEL, 0);

            mBatteryLevel.setText(Integer.toString(level) + "%");
            new ApiUbidots().execute(level);

    protected void onCreate(Bundle savedInstanceState) {

        mBatteryLevel = (TextView) findViewById(R.id.batteryLevel);

    protected void onStart() {
        registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    protected void onStop() {

    public class ApiUbidots extends AsyncTask<Integer, Void, Void> {
        private final String API_KEY = "our_api_key";
        private final String VARIABLE_ID = "our_variable_id";

        protected Void doInBackground(Integer... params) {
            ApiClient apiClient = new ApiClient(API_KEY);
            Variable batteryLevel = apiClient.getVariable(VARIABLE_ID);

            return null;


You can see that we created a nested class called ApiUbidots, this class extends AsyncTask. AsyncTask is an Android class created to do asynchronous operations. Extending this class is mandatory for all the operations that needs to connect to the Internet. We are doing some GET and POST requests, so… we should use it.

Now you can execute your code and launch it on your phone (or emulator). If you don’t know how to build and run the project you just made, just check out this guide from Android Developers. The result of the project will look like this.

And this is how the widgets in your dashboard should look, too. Ups! There it is! You created your first Android application using Ubidots! This is just the beginning, start reading the documentation and create more exciting things.

Join thousands of fellow IoT developers using Ubidots to build production-ready IoT solutions.

Sign up for free