I want to on my bluetooth programmatically while opening the android app, the user will get any notifications to allow the bluetooth also.Because android 6 and above the gps also needed for the effective working. how to achieve this?
Asked
Active
Viewed 4,186 times
1 Answers
5
The below snippet will work for you
public static boolean setBluetooth(boolean enable) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean isEnabled = bluetoothAdapter.isEnabled();
if (enable && !isEnabled) {
return bluetoothAdapter.enable();
}
else if(!enable && isEnabled) {
return bluetoothAdapter.disable();
}
// No need to change bluetooth state
return true;
}
to achieve this you need two permissions mentioned in your manifest
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Happy Coding :)

Shrenik Shah
- 1,900
- 1
- 11
- 19
-
thank you for the quick response – Anuraj R Oct 13 '16 at 09:54