Reputation: 3
I am facing this issue after I debugged:
Process: com.example.testcode, PID: 9965
java.lang.SecurityException: Permission Denial: starting Intent { act=android.bluetooth.adapter.action.REQUEST_ENABLE cmp=com.android.settings/.bluetooth.RequestPermissionActivity } from ProcessRecord{fa501bc 9965:com.example.testcode/u0a160} (pid=9965, uid=10160) requires android.permission.BLUETOOTH_CONNECT
I added permissions to the manifest. The code is saying "add permission check" after adding permissions to the manifest.
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
BluetoothAdapter mBluetoothAdapter;
private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//discovery cihaz buldugunda:
if(action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)){
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch(state){
case BluetoothAdapter.STATE_OFF:
Log.d(TAG,"onReceive: STATE OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG,"onReceive: STATE TURNING OFF");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG,"onReceive: STATE ON");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG,"onReceive: STATE TURNING ON");
break;
}
}
}
};
@Override
protected void onDestroy() {
Log.d(TAG,"onDestroy: called.");
super.onDestroy();
unregisterReceiver(mBroadcastReceiver1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnONOFF = (Button) findViewById(R.id.btnONOFF);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btnONOFF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"onClick: enabling/disabling bluetooth.");
enableDisableBT();
}
});
}
public void enableDisableBT(){
if(mBluetoothAdapter == null){ //eğer cihazında bluetooth yoksa
Log.d(TAG,"enableDisableBT: Does not have BT capabilities.");
}
if(!mBluetoothAdapter.isEnabled()){ //eğer cihazın bluetooth'u acık degilse
Log.d(TAG,"enableDisableBT: enabling BT.");
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); //bluetooth açılsın mı isteği..
startActivity(enableBTIntent); //neden CHECK istiyor ?
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); //IntentFilter ne yapıyor ?
registerReceiver(mBroadcastReceiver1, BTIntent);
}
if(mBluetoothAdapter.isEnabled()){
Log.d(TAG,"enableDisableBT: disabling BT.");
mBluetoothAdapter.disable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
}
}
}```
Upvotes: 0
Views: 2748
Reputation: 413
The set of permissions that you declare in your app depends on your app's target SDK version.
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
Maybe that answer will help you https://stackoverflow.com/a/69972855/16711198
Upvotes: 1