Create Bluetooth Android Application Using Android Studio

Introduction

 
Android is one of the most popular operating systems for mobile. In this article, I will show you how to create Bluetooth Android applications using Android Studio.
 
Requirements
Steps to be followed
 
Follow these steps to create a Bluetooth Android application. I have included the source code in the attachment.
 
Step 1
 
Open Android Studio and start a new Android Studio Project.
 
Android
 
Step 2
 
You can choose your application name and choose where your project is to be stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Android
 
Now, select the version of Android and select the target Android devices.
 
Android
 
Step 3
 
Now, add the activity and click the "Next" button.
 
Android
 
Add activity name and click "Finish".
 
Android
 
Step 4
 
Go to activity_main.xml. This XML file contains the designing code for your Android app.
 
Android
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.   
  8.     tools:context="abu.bluetooth.MainActivity" >  
  9.     <RelativeLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical">  
  13.         <TextView  
  14.             android:text=""  
  15.             android:id="@+id/out"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"></TextView>  
  18.   
  19.         <Button  
  20.             android:id="@+id/button1"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_alignParentLeft="true"  
  24.             android:layout_alignParentTop="true"  
  25.             android:layout_marginLeft="30dp"  
  26.             android:layout_marginTop="49dp"  
  27.             android:text="ON" />  
  28.         <Button  
  29.             android:id="@+id/button2"  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:layout_alignLeft="@+id/button1"  
  33.             android:layout_below="@+id/button1"  
  34.             android:layout_marginTop="27dp"  
  35.             android:text="COVERAGE" />  
  36.         <Button  
  37.             android:id="@+id/button3"  
  38.             android:layout_width="wrap_content"  
  39.             android:layout_height="wrap_content"  
  40.             android:layout_alignLeft="@+id/button2"  
  41.             android:layout_below="@+id/button2"  
  42.             android:layout_marginTop="28dp"  
  43.             android:text="OFF" />  
  44.     </RelativeLayout>  
  45.   
  46. </android.support.constraint.ConstraintLayout>  
Step 5
 
Go to  Main Activity.java. This Java program is the backend language for your app.
 
Android
 
The Java code is given below.
  1. package abu.bluetooth;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.app.Activity;  
  7. import android.bluetooth.BluetoothAdapter;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15. import android.widget.Toast;  
  16.   
  17. public class MainActivity extends Activity {  
  18.     private static final int REQUEST_ENABLE_BT = 0;  
  19.     private static final int REQUEST_DISCOVERABLE_BT = 0;  
  20.   
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         final TextView out = (TextView) findViewById(R.id.out);  
  26.         final Button button1 = (Button) findViewById(R.id.button1);  
  27.         final Button button2 = (Button) findViewById(R.id.button2);  
  28.         final Button button3 = (Button) findViewById(R.id.button3);  
  29.         final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.  
  30.                 getDefaultAdapter();  
  31.         if (mBluetoothAdapter == null) {  
  32.             out.append("device not supported");  
  33.         }  
  34.         button1.setOnClickListener(new View.OnClickListener() {  
  35.             public void onClick(View v) {  
  36.                 if (!mBluetoothAdapter.isEnabled()) {  
  37.                     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  38.                     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  39.                 }  
  40.             }  
  41.         });  
  42.         button2.setOnClickListener(new View.OnClickListener() {  
  43.             @Override  
  44.             public void onClick(View arg0) {  
  45.                 if (!mBluetoothAdapter.isDiscovering()) {  
  46.                     Context context = getApplicationContext();  
  47.                     CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";  
  48.                     int duration = Toast.LENGTH_SHORT;  
  49.                     Toast toast = Toast.makeText(context, text, duration);  
  50.                     toast.show();  
  51.                     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  52.                     startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);  
  53.                 }  
  54.             }  
  55.         });  
  56.         button3.setOnClickListener(new View.OnClickListener() {  
  57.             @Override  
  58.             public void onClick(View arg0) {  
  59.                 mBluetoothAdapter.disable();  
  60.                 Context context = getApplicationContext();  
  61.                 CharSequence text = "TURNING_OFF BLUETOOTH";  
  62.                 int duration = Toast.LENGTH_LONG;  
  63.                 Toast toast = Toast.makeText(context, text, 15);  
  64.                 toast.show();  
  65.             }  
  66.         });  
  67.     }  
  68.   
  69.     @Override  
  70.     public boolean onCreateOptionsMenu(Menu menu) {  
  71.         // Inflate the menu; this adds items to the action bar if it is present.  
  72.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  73.         return true;  
  74.     }  
  75.   
  76. }  
Step 6
 
We need to make a Bluetooth request. So, add the Bluetooth permission in AndroidManifest.xml.
 
Android
 
The AndroidManifest.xml code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="abu.bluetooth">  
  4.   
  5.     <uses-permission android:name="android.permission.BLUETOOTH" />  
  6.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
  7.     <application  
  8.         android:allowBackup="true"  
  9.         android:icon="@mipmap/ic_launcher"  
  10.         android:label="@string/app_name"  
  11.         android:roundIcon="@mipmap/ic_launcher_round"  
  12.         android:supportsRtl="true"  
  13.         android:theme="@style/AppTheme">  
  14.         <activity android:name=".MainActivity">  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.   
  23. </manifest>    
Step 7
 
Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the app from errors.
 
Android
 
Step 8
 
Then, click the "Run" button or press shift+f10 to finally run the project. And, choose the "virtual machine" option and click OK.
 
Android
 

Conclusion

 
We have successfully created a Bluetooth Android application using Android Studio.
 
Android
 
Android
 
Android