0

I'm developing an android app that needs a BT connection. After reading the Android Developers page, the given method such us isEnabled() doesn't work. The error is cannot resolve symbol isEnabled. The imported library is android.bluetooth.BluetoothAdapter. In the manifest file, following the instructions of the Android page, I also have inserted the permission to Bluetooth, BT admin and fine location. The code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    Log.i("Fallo","Dispositivo sin bluetooth");
}

if (!bluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new     Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

}
Juan CA
  • 156
  • 4
  • 15

1 Answers1

1

You are creating your BluetoothAdapter object outside the onCreate method. Put everything inside the onCreate curly brackets.

Gredow
  • 120
  • 6
  • still one error in `startActivityForResult(enableIntent, REQUEST_ENABLE_BT` show same error in `REQUEST_ENABLE_BT` – Juan CA Oct 17 '19 at 19:05
  • Check this out: https://stackoverflow.com/questions/8188277/error-checking-if-bluetooth-is-enabled-in-android-request-enable-bt-cannot-be-r This is not related with the initial question,though. – Gredow Oct 17 '19 at 19:15