Android: The Disable button is pressed in the Turn On Bluetooth dialog box.

How can I handle the Deny button , click the Bluetooth button to open the dialog box. I'm tired of using the " OnDismissListener " and the " OnCancelListener " even tried the " onActivityResult ", but it didn't work. Code ...

private BluetoothAdapter mBluetoothAdapter;
    private static final int REQUEST_ENABLE_BT = 1;

        @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isBleSupportedOnDevice()) {
            initializeBtComponent();

        } else {
            finish();
        }
    }

    private boolean isBleSupportedOnDevice() {
        if (!getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "BLE is not supported in this device.",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void initializeBtComponent() {
        final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);


    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
}

, "" ", , " "" ". , - , .

+4
2

onActivityResult.

requestCode, REQUEST_ENABLE_BT

, "" "", onActivityResult .

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  if(requestCode == REQUEST_ENABLE_BT){
   startBluetoothStuff();
  }

 }

, .

, -

boolean isBluetoothManagerEnabled()
{
  // Some code
}

public void startBluetoothStuff()
{
   if(isBluetoothManagerEnabled())
   {
      // Do whatever you want to do if BT is enabled.
   }
   else
   {
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
   }
}
0

, RESULT_CANCELED onActivityResult(). - :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==REQUEST_ENABLE_BT && resultCode==RESULT_CANCELED){
        ... Do your stuff...
    }
}
0

All Articles