I was writing some code to turn on or off bluetooth adapter. But the problem is that I am using android 14 and it is not working. It calls the method but bluetooth still dooes’t turns off if enabled or on if not enabled. I don’t know why this issue occurs. Can you please provide me some solution. I think this method is deprecated for latest SDK. But still can you please tell me solution
Here is my code
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_BLUETOOTH_PERMISSION = 100;
private BluetoothAdapter bluetoothAdapter;
private TextView tvBt;
private static final String TAG = "MainActivity";
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action != null && action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
updateBluetoothStatus(state);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Button and TextView
Button btnBt = findViewById(R.id.BtBtn);
tvBt = findViewById(R.id.BtTv);
// Check for Bluetooth permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_DENIED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_BLUETOOTH_PERMISSION);
}
}
// Initialize Bluetooth adapter
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (bluetoothManager != null) {
bluetoothAdapter = bluetoothManager.getAdapter();
} else {
Log.e(TAG, "BluetoothManager is null");
tvBt.setText("Bluetooth is not supported on this device");
return;
}
if (bluetoothAdapter == null) {
Log.e(TAG, "BluetoothAdapter is null");
tvBt.setText("Bluetooth is not supported on this device");
return;
}
// Register for Bluetooth state change broadcasts
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(bluetoothReceiver, filter);
// Set initial state of Bluetooth in TextView
updateBluetoothStatus(bluetoothAdapter.getState());
// Set OnClickListener for the button
btnBt.setOnClickListener(v -> {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
if (bluetoothAdapter.isEnabled()) {
Log.d(TAG, "Attempting to disable Bluetooth");
boolean success = bluetoothAdapter.disable();
if (!success) {
Log.e(TAG, "Failed to initiate Bluetooth disable");
}
} else {
Log.d(TAG, "Attempting to enable Bluetooth");
boolean success = bluetoothAdapter.enable();
if (!success) {
Log.e(TAG, "Failed to initiate Bluetooth enable");
}
}
} else {
Log.e(TAG, "BLUETOOTH_CONNECT permission not granted");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_BLUETOOTH_PERMISSION);
}
});
}
private void updateBluetoothStatus(int state) {
switch (state) {
case BluetoothAdapter.STATE_ON:
tvBt.setText("Bluetooth is ON");
break;
case BluetoothAdapter.STATE_OFF:
tvBt.setText("Bluetooth is OFF");
break;
case BluetoothAdapter.STATE_TURNING_ON:
tvBt.setText("Bluetooth is turning ON");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
tvBt.setText("Bluetooth is turning OFF");
break;
default:
tvBt.setText("Bluetooth state unknown");
break;
}
Log.d(TAG, "Bluetooth status updated: " + tvBt.getText().toString());
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_BLUETOOTH_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, proceed with Bluetooth operations
updateBluetoothStatus(bluetoothAdapter.getState());
} else {
// Permission denied, disable Bluetooth operations
tvBt.setText("Bluetooth permission denied");
Log.e(TAG, "Bluetooth permission denied");
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Unregister the receiver when the activity is destroyed
unregisterReceiver(bluetoothReceiver);
}
}
I tried to turn off bluetooth when it was on , but it didn’t work. I am using android 14. I have all neccessroy permissions
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothcontrol">
<!--Put the permissions between the manifest and application opening tags-->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Bluetooth">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>