Android How to - Enable the bluetooth adapter








Question

We would like to know how to enable the bluetooth adapter.

Answer

The following method shows how to enable the bluetooth adapter.

It sends an Intent to enable the Bluetooth.

//from  ww w  .  j  a v  a 2  s.  co  m
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
public class Main {
  /**
   * Enable the bluetooth adapter.
   */
  public static void bluetoothEnable(Activity activity, boolean discoverable, int reqCode) {
    if(discoverable) {
      Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
      discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
      activity.startActivityForResult(discoverableIntent, reqCode);
    } else {
      Intent bluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      activity.startActivityForResult(bluetoothIntent, reqCode);
    }
  }
}