BLUETOOTH_CONNECT permission - java

I have a problem, I try to activate Bluetooth but first I check for BLUETOOTH_CONNECT runtime permission (required by the BluetoothAdapter.enable()). Every time I request the permission is denied without displaying any dialog box, I tryed to request another permission and it works fine (displays dialog box with options - allow or denie) . I tryed to reinstall the app and clear its cache memory, i also checked for the app permissions allowed but i couldnt find Bluetooth. I also have the permissions in the Manifest.
public void enableordisableBT() {
if (MyBluetoothAdapter == null) {
Log.d(TAG,"Bluetooth not supported");
} else if (!MyBluetoothAdapter.isEnabled()) {
Log.d(TAG,"Bluetooth is currently not enabled");
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "You have already granted this permission!", Toast.LENGTH_SHORT).show();
MyBluetoothAdapter.enable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
}
else
{
requestConnectPermission();
}
} else if (MyBluetoothAdapter.isEnabled()) {
MyBluetoothAdapter.disable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
}
}
private void requestConnectPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.BLUETOOTH_CONNECT)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed because of this and that")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT}, CONNECTION_REQUEST_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH_CONNECT}, CONNECTION_REQUEST_CODE);
Log.d(TAG," permisiune in curs de cerere ");
}
}
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case CONNECTION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Permission Granted,access to connection available", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Permission Denied, access to connection unavailable, app requires BLUETOOTH_CONNECT permission in order to operate", Toast.LENGTH_LONG).show();
}
break;
case SCAN_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Permission Granted,access to scan available", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Permission Denied, access to scan unavailable, app requires BLUETOOTH_SCAN permission in order to operate", Toast.LENGTH_LONG).show();
}
break;
}
}

Related

Why my button work juste one time ? - Android studio

I am trying tp send sms by clicking a button bt this button is working juste one time.
And i don't know how I can get it so the button can be pressed an infinite amount of time.
Can you please help me to fix that.
this is my code :
Button sendBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
}
protected void sendSMSMessage() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+212xxxxxx", null, "Je suis en danger, voici ma localisation : https://goo.gl/maps/xamxKW62p34wWpBU8", null, null);
Toast.makeText(getApplicationContext(),
"SMS sent.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
}
}
}
You are missing else branch in sendSMSMessage, so you are not sending SMS if SEND_SMS permission is already granted. You are also not doing anything if you need to show permission rationale, which results in a broken experience.
Overall I would suggest to read carefully the docs on requesting permissions. It explains both your approach with onRequestPermissionsResult as well as the new recommended way of using ActivityResultContracts.
Without rewriting your code to the contracts I would fix it like this:
protected void sendSMSMessage() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) {
// fixme: show explanation
// before requesting the permission again
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
} else {
sendSmsImpl();
}
}
#Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendSmsImpl();
} else {
// fixme: explain that it can't send SMS without the permission
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
// !!! NOTE: you still need break inside switch/case
// even with curly braces
break;
}
}
}
private void sendSmsImpl() {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("+212xxxxxx", null, "Je suis en danger, voici ma localisation : https://goo.gl/maps/xamxKW62p34wWpBU8", null, null);
//todo: use sentIntent argument of sendTextMessage to detect success/error
Toast.makeText(getApplicationContext(),
"SMS sent.", Toast.LENGTH_LONG).show();
}

How to add files access permission on device android

My problem is when I am not getting files permission in the device.
Below image is showing: photos and media access are showing but files access not showing.
In the above permission access files permission not showing like below: I need below permissions including files access.
I use permision in the manifest:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.CAMERA" />
From the MainActivity:
private static final int CAMERA_PERMISSION_CODE = 100;
private static final int STORAGE_PERMISSION_CODE = 101;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storage = findViewById(R.id.storage);
camera = findViewById(R.id.camera);
// Set Buttons on Click Listeners
storage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
STORAGE_PERMISSION_CODE);
}
});
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
checkPermission(Manifest.permission.CAMERA,
CAMERA_PERMISSION_CODE);
}
});
}
// Function to check and request permission.
public void checkPermission(String permission, int requestCode)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, permission)
== PackageManager.PERMISSION_DENIED) {
// Requesting the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[] { permission },
requestCode);
}
else {
Toast.makeText(MainActivity.this,
"Permission already granted",
Toast.LENGTH_SHORT)
.show();
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions,
#NonNull int[] grantResults)
{
super
.onRequestPermissionsResult(requestCode,
permissions,
grantResults);
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Camera Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Camera Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
else if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Storage Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Storage Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
#NonNull String[] permissions,
#NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"Storage Permission Granted",
Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(MainActivity.this,
"Storage Permission Denied",
Toast.LENGTH_SHORT)
.show();
}
}
}
Changing the targetSdkVersion from 30 to 29 in my build.gradle file changed the dialog for me from photos/media to photos/media/files. I believe it has something to do with how API 30 is more strict with scoped storage.
Adding the below line in the Application portion of Manifest
android:requestLegacyExternalStorage="true"
solves the problem.

App does not ask for permissions on some devices

We have an app, where at some point we want to save a picture.
We implemented Methods to ask for permission.
On my Device (Galaxy S9, news update) it all works fine. It asks for permission and saves the picture. And the device (Galaxy A7) of my gf too.
On other devices, it does not ask for permission and so does not save the picture. I already deleted the app many times to be sure the permissions we're not already given on my phone.
addPicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (ContextCompat.checkSelfPermission(adding.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
picintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
picintent.putExtra(MediaStore.EXTRA_OUTPUT, picuri);
startActivityForResult(picintent, cameracode);
} else {
requestStoragePermission();
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
picintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
picintent.putExtra(MediaStore.EXTRA_OUTPUT, picuri);
startActivityForResult(picintent, cameracode);
}} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Kamera nicht unterstützt", Toast.LENGTH_SHORT).show();
}
}
});
private void requestStoragePermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)){
new AlertDialog.Builder(this)
.setTitle("Berechtigung benötigt")
.setMessage("Diese Bereichtigunjg wird zum speichern des Bildes benötigt")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(adding.this,new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
}else {
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Berechtigung erteilt", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Berechtigung nicht erteilt", Toast.LENGTH_SHORT).show();
}
}
};

After accept permission how to open a activity?

I trying to do a simple functionality. After the user accept permission for camera i need to open a new activity, but how do this?
This is a code. I check the permission and the request it. On my else if i open a new activity.
#RequiresApi(api = Build.VERSION_CODES.M)
#OnClick(R.id.scanButton)
void scanZxing(View view) {
if(getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(context, "We need permissions to acces your CAMERA!", Toast.LENGTH_SHORT).show();
((MainActivity) context).checkCameraPermission();
} else if (view.getId() == R.id.scanButton) {
Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
startActivityForResult(intent, 0);
}
}
#TargetApi(23)
public void checkCameraPermission(){
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
return;
}
if (this.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, REQUEST_CODE_CAMERA);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "We need your permission to open camera",
LENGTH_SHORT).show();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
You can check the permission request result in your activities onRequestPermissionsResult methodm and if user granted the permission then open the activity
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_CAMERA &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permisssion was granted
} else {
//permisssion was not granted
}
}
I am supposing you want to open ScanBarcodeActvity.
you can do it in following way
public void openScanBarCodeAcitvity() {
Intent intent = new Intent(getActivity(), ScanBarcodeActvity.class);
startActivityForResult(intent, 0);
}
Call above method at following place in addition to scanZxing() method
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_CAMERA:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Thanks for your permission", LENGTH_SHORT).show();
//permission granted, open activity
openScanBarCodeAcitvity();
} else {
Toast.makeText(this, "We need your permission to open camera",
LENGTH_SHORT).show();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}

Displaying a TextView depending on whether Android permission is disabled?

I'm trying to display a TextView which appears visible only when a particular Android permission, i.e. WRITE_EXTERNAL_STORAGE, is not enabled. The idea is to explain to the user why they need to enable it in case they've disabled it the first time, and then have them click the TextView to launch the permission request again. The following is not working for me. How can I accomplish this? I have this in my onCreate() method:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
if (NetworkHelper.isOnline(this)) {
new AppendParseData().execute();
}
}
TextView checkFilePermission = (TextView) findViewById(R.id.checkFilePermission);
if (!checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
checkFilePermission.setVisibility(View.VISIBLE);
}
try this way when you request for permission(I have used READ_CONTACTS permission you can use WRITE_EXTERNAL_STORAGE instead)
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
requestContactsPermissions1();
} else {
setTextVisablity(false);
checkSimCard();
}
in onRequestPermissionsResult
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
readContacts();
setTextVisablity(false);
} else {
// Permission Denied
setTextVisablity(true);
Toast.makeText(MainActivity.this, "READ_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
in the end setTextVisablity method
public void setTextVisablity(boolean isShow) {
if (isShow) {
tv.setVisibility(View.VISIBLE);
} else {
tv.setVisibility(View.GONE);
}
}
Use this code:-
int hasWriteContactsPermission = ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
return;
}
else{
//Permission already granted show textview here
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
Toast.makeText(MainActivity.this, " PERMISSION_GRANTED", Toast.LENGTH_SHORT)
.show();
} else {
// Permission Denied
Toast.makeText(MainActivity.this, " Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

Categories

Resources