How to enable/disable bluetooth programmatically in android

user458295 picture user458295 · Sep 27, 2010 · Viewed 171k times · Source

I want to enable/disable bluetooth through the program. I have the following code.

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

But this code is not working in SDK 1.5. How can I make it work?

Answer

prijin picture prijin · Jun 8, 2011

this code worked for me..

//Disable bluetooth
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.disable(); 
} 

For this to work, you must have the following permissions:

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