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?
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();
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.
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.
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With