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

Asked

Viewed 36 times

0

Inside an android application, I have a screen that requires bluetooth to be 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);
      }

Until then beauty, when bluetooth is disabled, appears the native screen of Android asking to enable it

"The app is requesting permission to activate Bluetooth. Allow?"

If the user press "YES" it enables bluetooth, however, I did not find how to treat if the user press "NO".

1 answer

1

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

Just apply the function onActivityResult 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
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.