-3

how to DISABLE bluetooth on the Onclick of a button?

public void btd(View view) { 
    BluetoothAdapter blue = BluetoothAdapter.getDefaultAdapter();

        blue.disable();
}

OR

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
         if (mBluetoothAdapter == null) {
             // Device does not support Bluetooth

             }else{

             if (!mBluetoothAdapter.isEnabled()) {

                mBluetoothAdapter.enable();

             }else{

              mBluetoothAdapter.disable();    

             }

             }


<uses-permission android:name="android.permission.BLUETOOTH" />  
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

the code only works if i enable bluetooth vis code, but if its enable via notificationbar, then the app gets force closed!!

Here is error Log http://justpaste.it/fd7t

JRE.exe
  • 801
  • 1
  • 15
  • 28

1 Answers1

0

Simple

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (mBluetoothAdapter.isEnabled()) {
   mBluetoothAdapter.disable(); 
} 
Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81