1

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?

Anuraj R
  • 524
  • 2
  • 8
  • 24

1 Answers1

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