Java Programming

How to Create Android App for Arduino Sensor Monitoring over Bluetooth

Description:

Create Android App

How to Create Android App for Arduino Sensor Monitoring over Bluetooth- I have been using Android applications for monitoring  Arduino sensors 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 monitor any sensor which is connected with the Arduino.

In this tutorial, you will learn how to create your own android cell phone application for monitoring any Sensor using Arduino, HC-05 Bluetooth module and an Android cell Phone. For the best understanding, I am using a variable resistor as the sensor. Later this variable resistor can be replaced with any sensor you want. This sensor monitoring application is designed using Android Studio. This is version 2 of the Android cell phone application designing.

While in version 1 of the android cell phone application designing, I created an application to control an LED using only two buttons.


 

In this tutorial, we will cover

  1. Complete circuit diagram explanation
  2. Interfacing
  3. Arduino programming
  4. Step by Step android cell phone App designing using Android Studio and finally
  5. Testing

without any further delay, let’s get started!!!

Amazon Links:

Bluetooth Module: Hc-05:

12v Adaptor:

Arduino Uno

Arduino Nano

Mega 2560:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

DISCLAIMER:

Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



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.


HC-05 Bluetooth Module with Arduino Circuit Diagram:

Create Android App

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.

 

 

For the demonstration purposes, I am using a variable resistor. This variable resistor can be easily replaced with any sensor you want. As you can see the middle leg of the variable resistor is connected with the Arduino’s Analog pin A0. While the other two legs of the variable resistor are connected with the Arduino’s 3.3v and GND.



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, which completely explains how to change the name of the Bluetooth module and how to change the pairing code.

The Bluetooth module Tx and Rx pins are connected with the Arduino’s pin number 2 and pin number 3. Pin number 2 and pin number 3 will be used as the Serial Port, which will be defined in the programming. While the VCC and GND pins are connected with the Arduino’s 5v and GND.

Arduino Bluetooth Module Programming:

Arduino Bluetooth Program explanation:

As you know my friends in Arduino 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 for communication with other 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. Pin number 2 is the Rx while Pin number 3 is the Tx.

On the third line, I defined a pin for the Variable Resistor which is connected with the Analog pin A0 of the Arduino.

Then I defined a variable Vdata of the type integer, this variable will be used to store the value of the variable resistor.



As you know my friends every Arduino and Mega program has at least two functions which are the void setup and void loop functions. This is inside the void setup function that we tell the controller what are the input pins and what are the output pins. As in this tutorial, I am using a variable resistor as the sensor so that’s why I set it as the input using the pinMode function.

Then I activated the Serial communication and I selected 9600 as the baud rate. One more thing about the void setup function. It executes only one time when the Arduino or Mega board is powered up.

Then starts the void loop function.

Using the analogRead function we read the analog pin A0 to which a variable resistor is connected and store the value in variable vdata.

And lastly we send the data to the Android cell phone application. After every 1 second data is sent to the Android cell phone sensor monitoring application. So that’s all about the Arduino programming.



Android app development:

First of all, open the Android Studio.

Create Android App

Click on Start a new Android Studio project

Create Android App

While the Choose your project form is open.

Select empty activity and click on the next button

Create Android App

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.


Create Android App

Wait for app synchronization

Create Android App

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.

Create Android App

After synchronization is completed the MainActivity.java and activity_main.xml open automatically

Create Android App

Before i start the main programming I want to tell you that there are two main classes which we first need to add

one is ActivityHelper.java

and the second is PreferencesActivity.java

now create ActivityHelper.java

right click on the package folder, then in new select java class.

Create Android App

Enter name as the ActivityHelper and click ok

Create Android App

Create Android App

ActivityHelper.java code:

Create Android App

Now create the second java class and follow the same steps as explained above.

Create Android App


Enter class name as the preferrencesActivity and click ok

Create Android App

Create Android App

preferrencesActivity.java code:


Create Android App

now you can see that we made two main classes one is ActivityHelper and the other is preferrencesActivity

Create Android App

Now we can start our main application  programming

Now we design our first launcher screen in which there will be two buttons search and connect and  listview

Open the activity_main.xml

Create Android App

Now change the layout form constraint layout to linearlayout

Create Android App

As you can see in the picture below.

Create Android App

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.

Create Android App

Create Android App

To check how the design look like just click on design button as you can see in below picture

Create Android App

Create Android App


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.

Create Android App

Create Android App

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 button with captions Design and Text. You can click on the Design button.

Create Android App

It’s a good programming practice to keep checking your desing 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 pair device, for this we use the listview attribute.

Create Android App

Create Android App

activity_main.xml code:

Now click on MainActivity.java tab as you can see

Create Android App

Change the appcompatActivity to Activity

Create Android App

Create Android App

MainActivity.java  code:

 

To remove these error just click on red bulb and click on create layout resource file “list_item”

Create Android App

And press ok

Create Android App

As you can see list_item.xml file is create open it and paste the code

Create Android App

list_item.xml  code:

 

So the list_item error is removed

Create Android App

To remove this errer just click on red  bulb and then click on  id value resource “action_sitting”

Create Android App


And press ok

Create Android App

Create Android App

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

Then New

Then Activity

And click on the Empty Activity

Create Android App

Enter the activity name which you want  and click finish

Create Android App

Now open  activity_monitoring_screen.xml and change the layout form constraint layout to

LinearLayout

Create Android App

Create Android App

activity_monitoring_screen.xml code:

Create Android App

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 button with captions Design and Text. You can click on the Design button.

Create Android App

Now open  MonitoringScreen.java

Create Android App

MonitoringScreen.java code:

 

And finally open the AndroidManifest.xml and add Bluetooth permission

Create Android App

AndroidManifest.xml  code:

For testing the app create apk file.so for apk file  click on build menu and then then click on build bundle(s)/apk and then build apk

Create Android App

Wait for generating apk file after generating the file a popup dialog will show as dialog shown just click on locate as you can see in the below picture

Create Android App

After clicking  on locate a folder will open where the apk is stored then just copy the app-debug.apk file and paste in the phone and install

Create Android App


Download:

Download apk file: sensor app apk file

For the Android app testing and explanation watch video tutorial is given below

Watch Video Tutorial:

 

Arduino Android App Related Projects:

https://www.electroniclinic.com/android-app-development-to-control-arduino-over-bluetooth-using-android-studio/

 

https://www.electroniclinic.com/12v-battery-efficiency-monitoring-using-arduino-and-cell-app-with-database/

 

https://www.electroniclinic.com/unlock-my-car-with-my-phone-start-engine-start-a-c-anti-theft-using-arduino/

 

https://www.electroniclinic.com/android-hmi-development-control-hmi-touch-screen-using-bluetooth-tft-touch-screen/

 

https://www.electroniclinic.com/arduino-bluetooth-controlled-robot-using-l298n-motor-driver-android-app/

 

https://www.electroniclinic.com/pulse-sensor-heartbeat-rate-heart-rate-measurement-using-arduino-bluetooth/

 

https://www.electroniclinic.com/arduino-bluetooth-smart-lock-using-android-cell-phone-application/

 

https://www.electroniclinic.com/how-to-use-gsm-and-bluetooth-together-to-monitor-any-sensors-wirelessly-using-arduino/

 

https://www.electroniclinic.com/wireless-battery-voltage-monitor-using-arduino-and-bluetooth/

 

https://www.electroniclinic.com/bluetooth-arduino-pin-code-and-name-changing-using-at-commands/

 

https://www.electroniclinic.com/wireless-temperature-monitoring-system-using-bluetooth/

 

https://www.electroniclinic.com/8×8-led-matrix-max7219-control-using-bluetooth/

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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...

14 Comments

  1. Your guides are great! The step by step walkthough for Android Studio is very good and helped me go from zero to having a functioning app. Thanks so much!!

  2. Thanks so much for the tutorial, it worked perfectly for the project I’m working on.
    Just wondering if you know how to solve a small problem, I would like the bluetooth connection to remain active even with the background application. I’m new to Android and I don’t know how to solve it.

  3. Where did the value of potentiometer saved in the Android app that is display on screen. Bcoz I want to store this value in database so from where I get this value?

  4. thank you so much sir but i have one question i want to store this value database but i cant figure out where did the value of potentiometer is stored so that i can transfer this value into database kindly tell me in which file and array/variable the value is stored

  5. Thanks a lot. It is very good project. I run it but when click on search button it gives message Bluetooth not found. Kindly guide what to do? There is no error in the code.

  6. Thanks to Engineer Fawad, my problem is fixed. Actually, i try to run it on Emulator that does not directly support Bluetooth. First of all, i run app on Android cell and before it make sure to enable the usb debugger mode of cell due to that app directly install on my cell and working.

  7. I have a question. I’m struggling to understand why PreferencesActivity class is needed in this project. Can you explain to me what exactly this class is doing?

  8. Hi there. I’ve been trying to implement a system-based app over your bluetooth connection. I was wondering to have your brother’s answer by email, but it is highly needed for us to have an explanation. We are about to deliver a project and we are running out of time. It would be nice to have your help. I followed your YT channel and also commented I loved your video 🙂
    Regards.

  9. Hi, I am facing an issue right now regarding receiving the data from the Arduino I followed all the steps you showed here and tried to implement it on, and also tried to do it with your Apk.file but non of the apps delivered the data into my screen. What do you think the problem is? is it from the Arduino? or the Bluetooth device?

Leave a Reply

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

Back to top button