Android Development Guide for the Internet of Things enthusiasts — I

Darren Mistry
6 min readMay 4, 2018

--

In the field of Internet of Things, creating sensor nodes and communicating with them is not enough. For any good IoT application to succeed, it is necessary to have a good mechanism of data storing and analysis and a responsive, user-friendly interface (UI). Recently, I encountered application development in my IoT project and my professor advised me to write a guide to help future IoT students. So I will be starting my journey as a blogger by talking about various components frequently used in an application and will simultaneously create an app which can serve as an interface for a basic smart home project.

Creating a New Android Project:

Click here and download Android Studio. Install the Android Studio IDE on your system. Once you are done with the installation, create a new Android Studio Project.

Creating a new Android Studio Project

Check Include Kotlin support if you wish to code further in Kotlin. In this tutorial, we will be coding in Java so no need to checkmark it. Click Next.

Selecting minimum API

Choosing a minimum target device is a very important step. Lower the API, more the number of devices your application could run on. I would advise choosing API 19: Android 4.4 (KitKat). Click Next.

Selecting layout

In this step, you can choose from various built-in activities provided. For our purpose, choose Empty Activity. Click Next.

Final step

Give a meaningful name to your activity which suggests the purpose. In this case, give the name as LoginActivity since this will be our launch activity where we will tempt the user to sign up or log in. Click Finish and wait for your project to build.

Creating a new layout:

Now we will add two edit-texts, one for email and one for password input and two buttons, one for signing up and one for logging in. You can add views by drag and drop or by writing XML code.

Design mode (Drag and drop)

As you begin to drag-drop, corresponding XML code is automatically generated in the Text mode. Likewise, when you create layouts using XML code, you can see the preview in design mode.

XML code for login activity

Tip: Always give id attribute to views created. This will be useful when we will write Java code to interact with these views. This is a basic layout. You can add font color, background color-image, margins, padding, re-adjust width, height, etc. to beautify your layout.

Now let’s try to run this code. Plug in your Android device via USB and click on Run -> Run ‘app’. A dialog box will appear where you should be able to see your device. If it is not visible, try updating your ADB or installing drivers for your device. Click on OK if your device is visible. Your app will now launch on your device.

Running app for the first time

Now we will add the backend for the UI we created. Open LoginActivity.java. Copy the following code:

Here we have connected the views created in XML layout in our Java class using the findViewById() method and the ids we assigned in the previous step. Then we have defined events when the buttons are clicked using the setOnClickListener() method. Right now they will display a Toast message showing the input of the email edit text. Run the app.

Creating a new Firebase project:

Firebase is a platform for mobile and web application development provided by Google. First of all, go to https://console.firebase.google.com/. Login using your Gmail account if you are not currently logged in. Once you are logged in, your window will look something like this:

Firebase home page

Click on add project to create a new project.

Creating a new project

Give a project name and select your country/region. Firebase will automatically assign you a project ID. However, you can change it as per your convenience. Click on Create Project.

Firebase Authentication:

Security is one of the most important aspects of an application. A user should be able to view only his/her data and not someone else’s. Also by enforcing for users to sign in, it is easier for the developer to maintain data and handle various events.

In your Firebase console, click on the Authentication option in the navigation bar on left.

Authentication initialization

As you can see, Firebase provides a lot more than just authentication and real-time database. In this tutorial series, we will discuss only authentication and real-time database. If you are interested in learning more about a particular feature, click on that feature in the navigation menu on the left and then click on the Go to docs link on the right.

Setting up authentication mode

Click on the sign-in method tab to set up a sign-in method. As you can see, there are many methods available for email/password, phone, Google, Facebook, Github, etc. For sake of simplicity, choose email/password by clicking on the enable switch.

Now open your android studio. Click on Tools -> Firebase. On the right side, you can see Assistant Pane. Under Authentication, click on Email and password protection to set up libraries and get code structure. For more information, go to https://firebase.google.com/docs/auth/.

Click on Connect to Firebase and Add Firebase Authentication to your app to add the dependencies automatically to your gradle files. Select the project which you created earlier in the menu to connect your app to that project.

Setting up Firebase Authentication in android studio

Create an instance of FirebaseAuth in your LoginActivity.java file. Initialize it with FirebaseAuth.getInstance() in the onCreate() method. Some of the useful methods of FirebaseAuth class are :

createUserWithEmailAndPassword(email,password) — creates a new user with a given email and password. Add an onCompletionListener() to specify what the app should do when the process of creating a user is done. To be more specific, you can also add an onSuccessListener() and onFailureListener().

signInWithEmailAndPassword(email,password) — sign in a user with given credentials provided there exists a user with those credentials. Here also you can add onCompletionListener(), onSuccessListener() and onFailureListener().

getCurrentUser() — returns a FirebaseUser object containing information of the user who has currently logged in. If this object is null, it means no user has logged in. If the object is not null, you can use methods like getDisplayName(), getEmail(), etc. to access information about the user.

So now the updated LoginActivity.java will be:

Final code with Firebase Authentication

Full code: https://github.com/Death14Stroke/iot-android-demo

So dear readers, that was integrating Firebase Authentication in your android app. In my next blog, we will see how to add Firebase real-time database to the android app. Till then, happy coding!

Read part — 2 for learning how to use Firebase realtime database.

--

--

Darren Mistry

Android developer by day, Gujarati shayri writer by night. DA-IICTian. Oracle.