Turning on and Turning Off Bluetooth By Programming in Android

Procedure
  1. Start Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java file.
  4. Create an activity_main.xml for layout design.
  5. Add three buttons and one TextView fields in the XML layout.
  6. Then look up buttons by their ids in MainActivity.java file.
  7. Use BluetoothAdapter like this:
     
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.
    getDefaultAdapter();
     
  8. Set the functionality of Bluetooth on buttons.
  9. On the first button enable Bluetooth like this:
     
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
     
  10. On the second button enable Bluetooth Duration like this:
     
    Intent enableBtIntent = new
    Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
     
  11. On third button disable the Bluetooth like this: 
     
    mBluetoothAdapter.disable();
Code is given below
 
MainActivity.java
  1. package com.example.bluetooth;    
  2. import android.os.Bundle;    
  3. import android.app.Activity;    
  4. import android.view.Menu;    
  5. import android.app.Activity;    
  6. import android.bluetooth.BluetoothAdapter;    
  7. import android.content.Context;    
  8. import android.content.Intent;    
  9. import android.os.Bundle;    
  10. import android.util.Log;    
  11. import android.view.View;    
  12. import android.widget.Button;    
  13. import android.widget.TextView;    
  14. import android.widget.Toast;   

  15. public class MainActivity extends Activity {    
  16.  private static final int REQUEST_ENABLE_BT = 0;    
  17.  private static final int REQUEST_DISCOVERABLE_BT = 0;    
  18.  @Override    
  19.  protected void onCreate(Bundle savedInstanceState) {    
  20.   super.onCreate(savedInstanceState);    
  21.   setContentView(R.layout.activity_main);    
  22.   final TextView out = (TextView) findViewById(R.id.out);    
  23.   final Button button1 = (Button) findViewById(R.id.button1);    
  24.   final Button button2 = (Button) findViewById(R.id.button2);    
  25.   final Button button3 = (Button) findViewById(R.id.button3);    
  26.   final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
  27.   if (mBluetoothAdapter == null) {    
  28.    out.append("device not supported");    
  29.   }    
  30.   button1.setOnClickListener(new View.OnClickListener() {    
  31.    public void onClick(View v) {    
  32.     if (!mBluetoothAdapter.isEnabled()) {    
  33.      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);    
  34.      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);    
  35.     }    
  36.    }    
  37.   });    
  38.   button2.setOnClickListener(new View.OnClickListener() {    
  39.    @Override    
  40.    public void onClick(View arg0) {    
  41.     if (!mBluetoothAdapter.isDiscovering()) {    
  42.      Context context = getApplicationContext();    
  43.      CharSequence text = "MAKING YOUR DEVICE DISCOVERABLE";    
  44.      int duration = Toast.LENGTH_SHORT;    
  45.      Toast toast = Toast.makeText(context, text, duration);    
  46.      toast.show();    
  47.      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);    
  48.      startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);    
  49.     }    
  50.    }    
  51.   });    
  52.   button3.setOnClickListener(new View.OnClickListener() {    
  53.    @Override    
  54.    public void onClick(View arg0) {    
  55.     mBluetoothAdapter.disable();    
  56.     Context context = getApplicationContext();    
  57.     CharSequence text = "TURNING_OFF BLUETOOTH";    
  58.     int duration = Toast.LENGTH_LONG;    
  59.     Toast toast = Toast.makeText(context, text, 15);    
  60.     toast.show();    
  61.    }    
  62.   });    
  63.  }    
  64.  @Override    
  65.  public boolean onCreateOptionsMenu(Menu menu) {    
  66.   getMenuInflater().inflate(R.menu.activity_main, menu);    
  67.   return true;    
  68.  }    
  69. }    
activity_main.xml
  1.    
  2.   
  3. <RelativeLayout  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     xmlns:tools="http://schemas.android.com/tools"  
  6.    
  7.    android:layout_width="match_parent"  
  8.    
  9.    android:layout_height="match_parent"  
  10.    
  11.    tools:context=".MainActivity" >  
  12.     <TextView android:text="" android:id="@+id/out" android:layout_width="wrap_content"  
  13.    
  14.        android:layout_height="wrap_content"></TextView>  
  15.          
  16.     <Button  
  17.       android:id="@+id/button1"  
  18.       android:layout_width="wrap_content"  
  19.       android:layout_height="wrap_content"  
  20.       android:layout_alignParentLeft="true"  
  21.       android:layout_alignParentTop="true"  
  22.       android:layout_marginLeft="30dp"  
  23.       android:layout_marginTop="49dp"  
  24.       android:text="TURN_ON" />  
  25.         
  26.     <Button  
  27.       android:id="@+id/button2"  
  28.       android:layout_width="wrap_content"  
  29.       android:layout_height="wrap_content"  
  30.       android:layout_alignLeft="@+id/button1"  
  31.       android:layout_below="@+id/button1"  
  32.       android:layout_marginTop="27dp"  
  33.       android:text="DISCOVERABLE" />  
  34.         
  35.     <Button  
  36.       android:id="@+id/button3"  
  37.       android:layout_width="wrap_content"  
  38.       android:layout_height="wrap_content"  
  39.       android:layout_alignLeft="@+id/button2"  
  40.       android:layout_below="@+id/button2"  
  41.       android:layout_marginTop="28dp"  
  42.       android:text="TURN_OFF" />  
  43.         
  44. </RelativeLayout> 
Output
 
The following shows the initial output:
 
 
By clicking on Button1:
 
 
 
By clicking on Button2: 
 
 
By clicking on Button3: