How do I get the result of the window that asks to activate Bluetooth?

0

In an android application, I have a screen that requires that bluetooth is enabled, if it is disabled, the part of the code below asks the user to enable it:

final BluetoothManager bluetoothManager =
       (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            ((MainActivity) context).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
      }

Up until then, when bluetooth is disabled, the native Android screen asks you to enable it.

"The application is requesting permission to enable Bluetooth. Allow?"

If the user presses "YES" it enables bluetooth, however, I did not find how to handle it if the user pressed "NAO".

    
asked by anonymous 06.11.2017 / 16:20

1 answer

1

I found the answer to my question on stackoverflow in English.

Just apply the onActivityResult function in the activity in question (in my case MainActivity )

 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // Answer of bluetooth intent
    if (resultCode == 0) {
        // do something
    }
}
    
06.11.2017 / 16:22