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. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity" >
  7. <TextView android:text="" android:id="@+id/out" android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"></TextView>
  9. <Button
  10. android:id="@+id/button1"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentLeft="true"
  14. android:layout_alignParentTop="true"
  15. android:layout_marginLeft="30dp"
  16. android:layout_marginTop="49dp"
  17. android:text="TURN_ON" />
  18. <Button
  19. android:id="@+id/button2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_alignLeft="@+id/button1"
  23. android:layout_below="@+id/button1"
  24. android:layout_marginTop="27dp"
  25. android:text="DISCOVERABLE" />
  26. <Button
  27. android:id="@+id/button3"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_alignLeft="@+id/button2"
  31. android:layout_below="@+id/button2"
  32. android:layout_marginTop="28dp"
  33. android:text="TURN_OFF" />
  34. </RelativeLayout>
Output
The following shows the initial output:
By clicking on Button1:
By clicking on Button2:
By clicking on Button3: