I'm trying to disable Bluetooth on the button click but it not work
hear, What I Do
if (SDK_INT >= Build.VERSION_CODES.S) {
if (checkPermission(Manifest.permission.BLUETOOTH_CONNECT) && checkPermission(Manifest.permission.BLUETOOTH_SCAN)) {
BluetoothAdapter adapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
if (adapter != null) {
if (adapter.getState() == BluetoothAdapter.STATE_ON) {
Log.e("BT", "disable");
adapter.disable();
} else if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
if (!adapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
Log.e("BT", "enable");
} else {
Log.e("BT", "Else");
}
} else {
Toast.makeText(UltimateHomeLauncherActivity.this, "Bluetooth is not supported on your hardware", Toast.LENGTH_SHORT).show();
}
} else {
List<String> deniedPermissions = new ArrayList<>();
deniedPermissions.add(Manifest.permission.BLUETOOTH_CONNECT);
deniedPermissions.add(Manifest.permission.BLUETOOTH_SCAN);
requestRuntimePermissions(1011, deniedPermissions.toArray(new String[0]));
}
}
I'm add Bluetooth permission in Manifest also.
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
android.permission.BLUETOOTH_CONNECT is a runtime permission and requires the consent of the user.
However, with next API level 33 enable()/disable() of the BluetoothAdapter will be deprecated and are not longer allowed. Therefore, it is probably best for such a function to navigate the user to the Bluetooth system dialog and ask him to turn the function on or off there.
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