Android app development to control Arduino over Bluetooth using Android Studio

(Last Updated On: August 7, 2019)

android app development

Description:

android app development– So far I have been using Android applications to control Arduino over Bluetooth. I will share the links of all the projects in which I have used the android applications. Due to a lot of requests from my subscribers and followers on my YouTube channel “Electronic Clinic” to explain how to create your own android application. Finally, I decided to share with you the knowledge, how to develop your own Android app to control Arduino.


For the best understanding and easy follow-up, I am going to control an LED from the android cell phone app. In this tutorial I will add two buttons in the app, one button will be used to turn on the LED and the other button will be used to turn off the LED. Later after understanding the basics then you can add multiple buttons and you can simply copy and paste the code the only difference will be in the ID, which you will see in the programming.

In this tutorial I am going to use a lot of pictures and I will try to explain everything, so this Tutorial can be a bit longer, but trust me if you follow all the steps and read this article, at the end you will be able to make your own Android app to control anything you want. Enough with the introduction, without any further delay, let’s get started!!!



The components and tools used in this project can be purchased from Amazon, the components purchase links are given below.

Arduino Uno: https://amzn.to/2tVz1vu
Mega 2560: https://amzn.to/2ze1kdu

Bluetooth Module: Hc-05: https://amzn.to/2CMHNiN

4v, 5v, and 6v LEDs Pack: https://amzn.to/2YHyRV0

330 ohm resistors pack: https://amzn.to/2NtlAeh
5×7 cm vero board: https://amzn.to/2OHGbvn
female headers: https://amzn.to/2zqmtiJ
connection wires: https://amzn.to/2DpBuW7
Super Starter kit for Beginners: https://amzn.to/2KJvmKG
Jumper Wires: https://amzn.to/2KMoVXs
Bread Board: https://amzn.to/2MS4q8X
12v Adaptor: https://amzn.to/2Ntr6h1
PCB plate: https://amzn.to/2IUwpCt
Variable Supply: https://amzn.to/2MT4Qfj
Digital Multimeter: https://amzn.to/2Nvft9i
Vero Board / strip board: https://amzn.to/2MTf9jD
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2zfoNuJ
Solder wire: https://amzn.to/2ufUMWf
Wire Stripper: https://amzn.to/2KOqxfU
wire cutter: https://amzn.to/2ucIq14
PCB small portable drill machine: https://amzn.to/2Nu62XF

The software’s used:

  1. The Software used for the Android app development
    • Android Studio
  2. Arduino IDE

About the Android Studio:

Android studio is one of the most commonly used software used for android app development which includes designing and programming. Before you follow this tutorial you should have some basic knowledge of how to use the Android studio. Otherwise, It can be really confusing for you. My recommendation is first you should follow some basic tutorials and then you can resume from here.

Before we start the android app development, first of all, I would like to explain the circuit diagram and Arduino programming. Because we can only design a good android application if we know exactly for what purpose we are making this application.

Circuit Diagram:

android app development

This is a very basic circuit diagram designed in CadeSoft Eagle 9.1.0 version. If you want to learn how to make a schematic and PCB then watch my tutorial given in the related projects section at the end.

An LED is connected with Pin number 13 of the Arduino through 330-ohm resistor. This is a 2.5v LED and that’s why I connected this current limiting resistor. If you have a 5v LED then there is no need to connect this resistor. In Video you will see I am not using this resistor as the led I am using is 5v. The main reason I added 2.5v LED in the circuit diagram is that this led is very commonly available. It really doesn’t matter if you use a 2.5v LED or a 5v led or even if you add a relay to control a 220 Vac bulb, it will have no effect on the programming.


On the left side, you can see a Bluetooth module, you can use HC-05 or HC-06 Bluetooth Module. If you are using a Bluetooth for the first time then you can watch my getting started tutorial on the Bluetooth module available in the related projects section. The Bluetooth module Tx and Rx pins are connected with the Arduino’s pin number 2 and pin number 3. Pin number and pin number 3 will be used as the Serial Port, which will be defined in the programming. While the VCC and GND are connected with the Arduino’s 5v and GND.

Arduino Programming:

Arduino Program Explanation:

As you know my friends in Arduino Uno we have only one Serial port which is available on pin number 0 and pin number 1. As I always say never use these pins with any Serial communication supported devices. The Arduino’s default Serial Port should only be used for debugging purposes. You can always define other Serial Ports using the SoftwareSerial library. So that’s the reason I added the SoftwareSerial.h header file.

I defined a Serial Port with the name Blue on pin number 2 and pin number 3 of the Arduino. The pin number is the Rx while Pin number 3 is the Tx.

On the third line, I defined a variable data of the type long int. This variable will be used to store the number which is sent from the Android cell phone.

LED is connected with pin number 13 of the Arduino.

Then I defined two variables password1 and password2 of the type long integer. The number 92 is used to turn on the led while 79 is used to turn off the led. This led can be replaced with a relay, this way you will be able to control anything you want.

Then in the void setup function, I set the LED as output and set it to off state using the digitalwrite function. low means off and High means ON.

To activate the serial communication I used the Serial.begin() function while 9600 is the baud rate and similarly for the Bluetooth module.  then starts the void loop function.

while(Blue.available()==0) ;

this line means that if the Bluetooth module has not received any data from the android cell phone then simply wait here.

if(Blue.available()>0)

this condition means if the Bluetooth module has received data from the android cell phone then store the received number in variable data and then using the if conditions the number stored in variable data is compared with the password1 and password2. If the number is equal to the password1 then the LED is turned ON and if the number stored in data is equal to password2 then the led is turned OFF. So that’s all about the Arduino’s programming.

Android app development:

First off all, open the Android Studio.

android app development


Click on Start a new Android Studio project

android app development

While the Choose your project form is open.

Select empty activity and click on the next button

android app development

After you click on the Next button, the configure your project form is opened, over here you set the project name, package name and you can select a directory where you want to store the application. Make sure the language is set to Java. So after you are done then you can click on the Finish button.

android app development

Wait for app synchronization

android app development

When your project is fully synchronized, as you can see the green dots in the following picture which is an indication that the synchronization is completed and we are ready for the next step.

android app development

Now we will make a layout for the Search and connect buttons. The search button will be used for searching the Bluetooth module and the connect button will be used to connect with the paired Bluetooth module.

For this click on the small arrow symbol given with the lightcontrol and find the Res and click on the small arrow to expand it, under the res then click on the layout to expand it and click on the activity_main.xml this will open the design screen.

android app development

The design screen is opened. Click on the small arrow in front of the java to expand it and then click on the MainActivity.
android app development

Now change the layout form constraint layout to linearlayout

android app development

As you can see in the picture below.

android app development

Now we make another linear layout within the main linearlayout

To create a button in xml we use the button attribute having some property which you can adjust as per your requirements.

android app development

To check the design layout for this change from text mode to the design. As you can see at the end of the coding there are two buttons with captions Design and Text. You can click on the Design button. As you can see the mouse cursor.

android app development

android app development


Now again click on the text button to open the coding. As in this App I am going to use only two buttons. So I can simply copy and paste the code and will change the caption of the button from search to connect.

android app development

Now if again you click on the design button you will see now two buttons are added.

 

android app development

 

It’s a good programming practice to keep checking your design layout as you continue to program. So now we are done with two buttons with captions search and connect. Now we will add the code for the paired device, for this, we use the listview attribute.

android app development

Click on the Design button to check.

android app development

Now open the mainActivity.java class

android app development

Change the appcompatActivity to Activity

android app development

android app development

Now you can follow the following pictures

android app development

Now create a java class

android app development

Enter the name as the ActivityHelper and click ok

android app development

android app development

ActivityHelper.java  Code:

android app development

Now create the second java class

android app development


Enter class name as the preferrencesActivity and click ok

android app development

android app development

android app development

PreferrencesActivity.java Code

Now open again MainActivity.java

android app development

MainActivity.java Code

To remove  this error just click on the red bulb and select create id value resource

android app development

And then click ok

android app development

Now remove this error click on red bulb and click on create layout resource file

android app development

And the click ok

android app development


So now you can see the list_item.xml is created

android app development

Open list_item.xml file and paste this code

Now you see all red error are removed.

android app development

Now we create the main controlling screen, for this right click on package folder

Then New

Then Activity

And click on the Empty Activity

android app development

Open  activity_controlling.xml

android app development

Now change the layout form constraint layout to RelativeLayout

android app development

 

android app development

Open activity_controlling.xml and switch form text to design mode

android app development

Create button  id on

android app development

android app development

Copy the same code and paste and change the id and name

android app development

Now open the Controlling.java class

android app development

android app development


Controlling.java code

Don’t forget to add the bluetooth permission in manifest.xml before you creat the APK file.

android app development

We are done with the designing and coding. Now the next step is to create the APK file.

Create .apk file:

Click on the build menu then hover the mouse on build bundle/(apk) and click on the build apk

android app development

And then wait for around one minute depending on your laptop or computer processing speed.

android app development

After generating the apk file then click on locate and transfer the app-debug.apk to phone and install

android app development


Download:

Download apk file:  light controlling apk

For the android app testing watch video given below.

Watch Video Tutorial:

 

 

 

Android app Bluetooth related Projects:

Arduino Bluetooth Pin Code and Name changing using AT commands

 

Android HMI development- control HMI touch screen using Bluetooth “TFT touch screen”

 

12v Battery Efficiency Monitoring using Arduino and Cell App with Database

 

Arduino Bluetooth controlled Robot using L298n Motor Driver Android App

 

Pulse Sensor/ Heartbeat Rate/ Heart rate measurement using Arduino & Bluetooth

 

Arduino Bluetooth Smart Lock using Android cell phone application

 

How to use GSM and Bluetooth Together To monitor Any Sensors wirelessly using Arduino

 

Wireless battery voltage monitor using Arduino and Bluetooth

 

Wireless Temperature monitoring system using Bluetooth

 

8×8 LED Matrix MAX7219 Control using Bluetooth and Arduino

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Close