54

I'm trying to do this:

    BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
    if (bt == null){
        //Does not support Bluetooth
        status.setText("Your device does not support Bluetooth");
    }else{
        //Magic starts. Let's check if it's enabled
        if (!bt.isEnabled()){
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }   
    }

But I get this error:

REQUEST_ENABLE_BT cannot be resolved to a variable

How can I fix it?

Sergio Juan
  • 553
  • 1
  • 4
  • 4

3 Answers3

121

REQUEST_ENABLE_BT is a request code that you provide. It's really just a number that you provide for onActivityResult. It will be the requestCode (first parameter) of onActivityResult when the activity returns. You could put any number you want as long as it's consistent in the return method.

In other words, put a line like this in the top of your Activity:

private final static int REQUEST_ENABLE_BT = 1;

DeeV
  • 35,865
  • 9
  • 108
  • 95
7

The document says The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult() implementation as the requestCode parameter.

You can pass any integer value > 0 there.

Soundararajan
  • 2,000
  • 21
  • 23
-9

override method startActivityForResult as bellow.

private void startActivityForResult(Intent enableBtIntent,Object rEQUEST_ENABLE_BT2) {
}
Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
Cenk
  • 1
  • 6
    I'm surprised this answer doesn't have a -100 because it's incorrect from the beginning to the end. 1) You cannot override a method with a different signature (except for covariant subtypes). 2) You cannot override a method and reducing its access level. 3) You didn't give any hint what should be in this new method and why it's needed. 4) Adding a different method does not help answering what the parameter to the existing method should be used for. 5) changing the name of a parameter doesn't affect its function 6) your ALL_CAPS mixed with camelCase is very strange. Phew. – JHH Sep 12 '16 at 13:34