Connecting Android with Cloud Service Ubidots

Let’s face it, an old Android phone is hard to sale.

So, let’s make something useful from it. Let’s make a Bike Tracking System from an “old Android Phone” (Android 4 or higher).

Even older phones, 3-4 years old, normally have GPS and are by default connected to the Internet via SIM card. This will be enough for the basic project, the “Bike Tracking System”.

This is the goal

Getting information about the battery status and the position of the bike.

Ubidots Dashboard


Parts are needed for the project:

  • Android phone with Android version 4 or higher
  • SIM card with a data plan
  • Free Ubidots account, a cloud service for things
  • Power bank is optional
  • A box that will hold the phone and eventually the power bank.

Costs for the project:

I pay about $4 for the data plan with 0.5 GB per month.

Ubidots Account

The first thing to do is, getting an Ubidots Account.

Ubidots is a Cloud Service for Internet of Thing. It supports a wide range of hardware like Arduino, Raspberry Pi by providing API’s. And against many other Cloud services for things, Ubidots also has an API for Android that let your Android App connect to the service and send and get information.

Go to and get yourself an Account.

If you want to read about other Cloud Services for Things, you may like the following post:

Downloading the Ubidots Tracking App or Source

On Ubidots Android Page you find a link to the Google Play Store where you can download the ready to use Tracking app. If you want to customize the App, you may want to download the Source Code from Github:

For this example, we will use 2 Apps, theApp from Google Play for the Tracking part and the App for the Battery Level which is explained here (based on Ubidots docs).

Add Source

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


Add Variable

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.


Customize the icon for your Datasource if you want.


API Credits

From the API Credits, you will need the API Key later in Android Studio


Take note of the variable ID as we are going to need it later for our application to work. You will need it later in Android Studio.


Dashboard Add Source

To your Dashboard add the Datasource for the Battery Level and the location you get from the Tracking App.


Android App for the Battery Level

Open Android Studio and create a New Project with a Blank Activity.


Now, there are four steps to do:

  • Editing the AndroidManifest.xml
  • Editing the build.gradle (Module app)
  • Editing the MainActivity or whatever you name it.
  • Editing the activity_main.xml

Editing the AndroidManifest.xml

To allow Internet connections, add the following line to your AndroidManifest.xml:

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

Editing the build.gradle (Module app)

Add to the dependencies section the 2 following lines:

compile ‘’
compile ‘com.ubidots:ubidots-java:1.6.4’

Click on Sync Project with Gradle file to include the libraries.

Editing the MainActivity or whatever you named it.

Replace all the code from the MainActivity but not the first line, which contains your package information:

        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 MainActivity 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(;

    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 = "your API KEY";
        private final String VARIABLE_ID = "your battery level ID";

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

            return null;

Copy your API Key in here:
private final String API_KEY = "your API KEY";

And copy the ID for your Battery Level Variable in here:
 private final String VARIABLE_ID = "your battery level ID";

Editing the activity_main.xml

Replace alle the code from the activity_main.xml with the following:

<RelativeLayout xmlns:android=””
android:layout_centerHorizontal=”true” />

Thats it!
Now you can execute your code and launch the app on your phone.

To see the Battery Level and the Location, we need to run both apps at the same time. If you like the general idea, you can go from here and make your customized version and put everything in One App.

For me it's time to pack everything together and take a bike tour for testing.





Good Luck!