pranshuagarwal
pranshuagarwal

Reputation: 138

Disabling Bluetooth in Android WITH user consent

I would like to disable bluetooth after I am done with it as part of my cleanup activities. As explained in other questions and the android documentation, the BluetoothAdapter.disable() method disable bluetooth but the method documentation also states

Bluetooth should never be disabled without direct user consent. The disable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.

The obvious approach for user consent would be to make my own popup and ask for consent. Other than this, to keep the approach to enable and disable similiar,

Is there an intent action similiar to Bluetooth Enable ( ACTION_REQUEST_INTENT ) available to disable bluetooth?

Upvotes: 2

Views: 204

Answers (2)

verybadalloc
verybadalloc

Reputation: 5796

There is no intent that exists to achieve what you want. It has to be done through the BluetoothAdapter

Upvotes: 1

Ankush Bist
Ankush Bist

Reputation: 1892

display this dialog onButton click to disable bluetooth

public void showDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Confirm");
    builder.setMessage("Are you sure?");

    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            //Disable bluetooth
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
            if (mBluetoothAdapter.isEnabled()) {
                mBluetoothAdapter.disable(); 
            } 
        }

    });

    builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // Do nothing
            dialog.dismiss();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}

Upvotes: 0

Related Questions