Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable/disable bluetooth programmatically in android

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?

like image 565
user458295 Avatar asked Sep 27 '10 18:09

user458295


People also ask

How do I enable Bluetooth to disable apps on Android?

In android, we can disable or turn off Bluetooth just by invoking a BluetoothAdapter method disable(). Following is the code snippet to disable or turn off Bluetooth in android applications using disable() function. BluetoothAdapter bAdapter = BluetoothAdapter. getDefaultAdapter();

How do you check Bluetooth is on or off in android programmatically?

Call isEnabled() to check whether Bluetooth is currently enabled. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, call startActivityForResult() , passing in an ACTION_REQUEST_ENABLE intent action.

Which method used to disable Bluetooth?

One option for deactivating Bluetooth on an Android device is via the Status Bar. Swipe down from the top of the screen to display the Status Bar and swipe down again to locate and tap Bluetooth to switch it on or off. Another option for turning off your Android device's Bluetooth is via the Settings app.


1 Answers

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"/> 
like image 110
Prijin Koshy Alex Avatar answered Sep 29 '22 03:09

Prijin Koshy Alex