title: Android: How to enable Bluetooth through Application Class?
body: <p>I am building an Android App that requires the Bluetooth connection(including the BluetoothSocket) to persist over the whole lifetime of the App. So, I am trying to establish Bluetooth connection through the Application class. But I have met with a problem in the very first step.</p>
  
  <p>Here, I am trying to enable Bluetooth by asking the user's permission.
  This is the code I have tried:</p>
  
  <pre><code>import android.app.Application;
  import android.bluetooth.BluetoothAdapter;
  import android.content.Intent;
  import android.widget.Toast;
  
  public class BluetoothApplication extends Application {
  
      BluetoothAdapter bluetoothAdapter;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
          if (bluetoothAdapter == null) {
              Toast.makeText(getApplicationContext(), "Device doesn't Support Bluetooth", Toast.LENGTH_SHORT).show();
          } else {
              if (!bluetoothAdapter.isEnabled()) {
                  Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                  startActivityForResult(enableAdapter, 0);
              }
          }
      }
  }
  </code></pre>
  
  <p>But the <code>startActivityForResult</code> method displays an error saying <code>Cannot resolve method startActivityForResult(android.content.Intent, int)</code></p>
  
  <p>Is there a way out of this?</p>
  
  <p>Or do I have to explicitly enable Bluetooth without asking the user's permission?
  (However, this is the situation which I would like to avoid)</p>
  
  <p>Thank you</p>