What is bluetooth in android

Android bluetooth is provide the facility by which you can transfer the file from one device to another.

In my prgram run the app it will show you the two buttons when you click on BluetoothON button it will give the message bluetooth is on and when you click on another bluetoothOFF button it will off the bluetooth.


Bluetooth:.

In this section i am going to show you the bluetooth on and off with practical example.

First open the android studio and create a project and give the name of your project like BluetoothDemo.

The screenshots of the program is as follows:

bluetooth image bluetooth image

You need to drag and drop the two Button from pallet. Open the activity_main.xml file and code look like this


<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="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

<Button
    android:id="@+id/btnon"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="BlueTooth ON"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:onClick="ON"
    />
<Button
    android:id="@+id/btnoff"
    android:layout_marginLeft="10dp"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="BlueTooth OFF"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:onClick="OFF"
    />

</LinearLayout>
       

Open the MainActivity java class and write code look like this.


package com.example.niitpc.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity   {

BluetoothAdapter bluetoothAdapter;
Intent intent;
int requestCodeBT;
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate( savedInstanceState );
   setContentView(R.layout.activity_main );

   bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
   intent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
   requestCodeBT=1;

 }

public void ON(View v)
{
  if(bluetoothAdapter==null)
  {
    Toast.makeText( MainActivity.this,"Bluetooth is not supperted by this device",Toast.LENGTH_LONG ).show();
  }
  else
  {
   if(!bluetoothAdapter.isEnabled())
   {
     startActivityForResult(intent,requestCodeBT );
   }
}
}
public  void OFF(View v)
{
 if(bluetoothAdapter.isEnabled())
 {
   bluetoothAdapter.disable();
   Toast.makeText( MainActivity.this,"Bluetooth is off",Toast.LENGTH_LONG ).show();
 }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCodeBT==requestCode)
{
  if(resultCode==RESULT_OK)
  {
   Toast.makeText( MainActivity.this,"Bluetooth is on",Toast.LENGTH_LONG ).show();
   }else if(resultCode==RESULT_CANCELED)
   {
    Toast.makeText( MainActivity.this,"Bluetooth is cancelled",Toast.LENGTH_LONG ).show();
   }

  }

 }
}



Explanation:

BluetoothAdapter bluetoothAdapter:Here i have declared bluetooth object of BluetoothAdapter.

Intent intent:Here i have declared the intent object of the Intent.

int requestCodeBT:Here i have declared the variable requestCodeBT and their data type is int.

bluetoothAdapter=BluetoothAdapter.getDefaultAdapter():Here i have called the getDefaultAdapter() with the help of BluetoothAdapter.This method return the BluetoothAdapter object.

intent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE ):Here i have initialized the intent object and passing the constant ACTION_REQUEST_ENABLE as argument.Activity Action: Show a system activity that allows the user to turn on Bluetooth

requestCodeBT=1:here i have assign the 1 into requestCodeBT variable.

public void ON(View v):Here i have created the method ON.The ON method is on the bluetooth.

if(bluetoothAdapter==null):This if statement check the bluetoothAdapter is null if it is true then.

Toast.makeText( MainActivity.this,"Bluetooth is not supperted by this device",Toast.LENGTH_LONG ).show():Here i have print the message Bluetooth is not supported by this device.

else:if condition is false then control come inside the else statement.

if(!bluetoothAdapter.isEnabled()):The if statement check the bluetoothis not enabled then.

startActivityForResult(intent,requestCodeBT ):Here i have called the startActivityForResult() that take the two argument first is intent and second is requestCodeBT.

public void OFF(View v):Here i have created another method OFF that off the bluetooth.

if(bluetoothAdapter.isEnabled()):this statement check the bluetoothAdapter is enabled then.

bluetoothAdapter.disable():Here we called the disable() with the help of bluetoothAdapter it will off the bluetooth.

protected void onActivityResult(int requestCode, int resultCode, Intent data):Here i have override the onActivityResult().It will take three argument first is requestedCode second is resultCode and third is intent

if(requestCodeBT==requestCode):Here i have checked the requestedCodeBT and requestCode are equal then.

if(resultCode==RESULT_OK):Here i have checked the resultCode is equal to RESULT_OK then.

Toast.makeText( MainActivity.this,"Bluetooth is on",Toast.LENGTH_LONG ).show():This statement print the message Bluetooth is on with the help of Toast.

else if(resultCode==RESULT_CANCELED)Here we check bluetooth is cancelled then.

Toast.makeText( MainActivity.this,"Bluetooth is cancelled",Toast.LENGTH_LONG ).show():This statement print the message Bluetooth is cancelled.

You must set the ON() to android:onClick of the BluetoothON button and OFF() set on bluetoothOFF button.


Bluetooth

Bluetooth on and off for video click here

back                     next