Arduino Projects

Arduino Bluetooth controlling application development using Android Studio

Android Application development for Bluetooth controlling

Description:

Arduino Bluetooth controlling application development using android studio- in this article, I am going to show you to design and develop an android application that controls anything over Bluetooth. in this article, I am covering only the android side. for Arduino programming and  circuit diagram visit my article, Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth


Arduino Bluetooth controlling Application Designing and Development:

First of all, open the Android Studio.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Click on Start a new Android Studio project

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

While Choose your project form is open.

Select the empty activity and click on the Next button

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

After you click on the Next button, then 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.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Wait for app synchronization

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

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.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Before starting app design there are two main classes which are PreferencesActivity.java and ActivityHelper.java

Now create a java class

Arduino Bluetooth controlling

Enter name as the ActivityHelper and click ok

Arduino Bluetooth controlling


And past below code

ActivityHelper.java code

package com.example.lightcontrol;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.preference.PreferenceManager;

public class ActivityHelper {
public static void initialize(Activity activity) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);

String orientation = prefs.getString(“prefOrientation”, “Null”);
if (“Landscape”.equals(orientation)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if (“Portrait”.equals(orientation)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
}
}

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now create the second java class

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Enter the class name as the preferrencesActivity and click ok

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth


PreferrencesActivity.java code
package com.example.lightcontrol;

import java.util.Map;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class PreferencesActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityHelper.initialize(this);
// Using this for compatibility with Android 2.2 devices
}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference pref = findPreference(key);

if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
pref.setSummary(listPref.getEntry());
ActivityHelper.initialize(this);
}

if (pref instanceof EditTextPreference) {
EditTextPreference editPref = (EditTextPreference) pref;
pref.setSummary(editPref.getText());
}
}

@Override
protected void onPause() {

PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}

@Override
protected void onResume() {
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
Map<String, ?> keys = PreferenceManager.getDefaultSharedPreferences(this).getAll();

for (Map.Entry<String, ?> entry : keys.entrySet()) {
// Log.d(“map values”, entry.getKey() + “: ” + entry.getValue().toString());
Preference pref = findPreference(entry.getKey());
if (pref != null) {
pref.setSummary(entry.getValue().toString());
}
}

super.onResume();
}

}


Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

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

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now change the layout form constraint layout to linearlayout

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

As you can see in the picture below.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now we make another linear layout within the main linearlayout

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

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

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. As you can see the mouse cursor.

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

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. Click on the Design button to check.Copy the below code in your activity_main.xml

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

activity_main.xml code

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
tools:context=”.MainActivity”>

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:layout_marginTop=”20dp”>

<Button
android:id=”@+id/search”
android:layout_width=”wrap_content”
android:layout_height=”match_parent”
android:layout_marginLeft=”90dp”

android:text=”Search”/>

<Button
android:id=”@+id/connect”
android:layout_width=”wrap_content”
android:layout_height=”match_parent”
android:text=”Connect”/>

</LinearLayout>

<ListView
android:id=”@+id/listview”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
</ListView>

</LinearLayout>
Now open the mainActivity.java class and paste the below code

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

MainActivity.java code



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

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

So now you can see the list_item.xml is created

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Open list_item.xml file and paste this code

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:gravity=”center_vertical” >

<!– Defining where should text be placed. You set you text color here –>

<TextView
android:id=”@+id/lstContent”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_margin=”4dip”
android:textSize=”20sp”
android:gravity=”left”
android:padding=”5dp”
android:textColor=”#000000″ />

</LinearLayout>

Now we build the main controlling window, click on the package folder right now

Then New

Then Activity

And click on the Empty Activity

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Open activity_controlling.xml

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now change the layout form constraint layout to RelativeLayout

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Open the activity controlling.xml and switch the text type to the design mode

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Before creating a toggle button first of all copy these two images in drawable folder which is giving below

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

right-click on drawable and click on the android resource file

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

The resource file name is on_off and press ok and paste the below code

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

Now open activity_controlling.xml and paste the below code



Finally, open the Controlling.java file

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

and paste the below in your Controlling.java file

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

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

Creating Arduino Bluetooth controlling application .apk file:

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

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

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

 

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth

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

Arduino Bluetooth controlling system for Home Automation using Arduino & Bluetooth


Bluetooth enabling permission

You must claim two permissions to use Bluetooth features in your software. BLUETOOTH is the first of these. To perform some Bluetooth communication, you need this permission, such as requesting a link, accepting a connection, and transferring data.

ACCESS FINE LOCATION is the other permission to be declared. Your device requires this permission because you can use a Bluetooth scan to collect information about the user’s location. This data can come from the user’s own phones, as well as from Bluetooth beacons used at places like shops and transit facilities.

You will declare the BLUETOOTH ADMIN permission in addition to the BLUETOOTH permission if you want your app to trigger network discovery or modify Bluetooth settings. Many applications only need this permission for local Bluetooth devices to be discovered. Unless the program is a “power manager” which modifies Bluetooth settings upon user request, the other features provided by this permission should not be used. Declare permission(s) for Bluetooth in your manifest application file. Don’t forget to add the Bluetooth permission in manifest.xml before you create the APK file.


Download apk file: home automation app



Final Design of Home Automation Android Bluetooth App:

Arduino Bluetooth controlling

Related Articles

Leave a Reply

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

Back to top button