Need example android Bluetooth permission code - android

I need an example java code of Bluetooth permissions. Android 12.
I know that I have to put this in the AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH"/>
From my research I think I have to use these tools to get my phone to ask me for Bluetooth permissions:
ActivityCompat
ContextCompat
PackageManager
onRequestPermissionsResult
But no luck after many hours of tests.
I even tried using libraries from GitHub to ask for "easy" permissions for me but no luck:
https://github.com/googlesamples/easypermissions
https://github.com/guolindev/PermissionX
I tried lowering my target SDK version so I don't need to ask for the user entry but "AndroidX" gave me an error telling that its not compatible with lower SDK's.
It would help me a ton if you have a basic working Bluetooth app java code so I can figure out how to code my own app.

For the new Bluetooth permissions on Android 12, you need these permissions:
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
You can refer to this blog post for more details.

For bluetooth permission on Android 12, you need these permission, depend on your usage:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
In your activity, you can add this to check the permission is allowed or not:
private static final int ACCESS_COARSE_LOCATION_RESULT_CODE = 4;
private static final int BLUETOOTH_RESULT_CODE = 5;
private static final int DANGEROUS_RESULT_CODE = 1;
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
ACCESS_COARSE_LOCATION_RESULT_CODE);
}
else if (ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH},
BLUETOOTH_RESULT_CODE);
}
else if(ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_SCAN},
DANGEROUS_RESULT_CODE);
}
}
else if(ContextCompat.checkSelfPermission(this,
Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.BLUETOOTH_CONNECT},
DANGEROUS_RESULT_CODE);
}
}
To understand more, you can check the documentation.

Related

Why does the permissions dialog not appear when I ask for background location?

I am attempting to request background location permission for my app, but the locations permission dialog doesn't appear at all if I ask for background location.
If I just ask for coarse location and fine location, the permissions dialog appears as it should.
Manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
code:
requestPermissionsIfNecessary(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
Manifest.permission.RECEIVE_BOOT_COMPLETED
});
...
...
private void requestPermissionsIfNecessary(String[] permissions) {
ArrayList<String> permissionsToRequest = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
permissionsToRequest.add(permission);
}
}
if (permissionsToRequest.size() > 0) {
ActivityCompat.requestPermissions(
this,
permissionsToRequest.toArray(new String[0]),
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
No permissions dialog appears with this code. But if I comment out the 'background' permission lines, the dialog appears and the user can choose permissions.
Same result whether targeting Android 11 or 12. So what am I missing here?
This is expected behavior from android 10. Background location permission can not be granted from application as user permission. Instead you should tell user with some kind of pop up and redirect user to setting page.
OK, with help from #alokHarman and other threads here I have arrived at the following (in OnCreate):
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_BACKGROUND_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", getPackageName(), null));
startActivity(intent);
}
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(getPackageName())) {
Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(intent);
}
I have also directed the user to change the power settings for this app, because if the app is allowed to go into deep sleep than it matters not whether background location is activaed or not.

Problem After Permission Change in Publish App

I have a android app in play store. That has only internet permission. Now I change in app user can use their camera & storage.
I add privacy policy on play store and in link in app also. I also mention these permission in release text.
When I upload new app display message like this
minSdkVersion 21
targetSdkVersion 28
versionCode 4
versionName "4.0"
Permissions
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera2.full" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Permission Code
private void permission(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(StartActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
//Display_Rermission_Reason();
}else {
Intent intent = new Intent(StartActivity.this, PrivacyPolicyActivity.class);
startActivity(intent);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// close the app
Toast.makeText(MainActivity.this, "Sorry!!!, you can't use this app without granting permission", Toast.LENGTH_LONG).show();
//Display_Rermission_Reason();
}
}
}
After publish the app it display in google play store
This app is incompatible with all of your devices.
Any version 5+ and above
Change this in manifest,
<uses-feature android:name="android.hardware.camera2.full"
android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus"
android:required="false" />
Have a look at this link below which might help you,
https://developer.android.com/guide/topics/manifest/uses-feature-element

Missing permissions required by LocationManager

Android Studio 3.4.
A device on android 6.0
in manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myproject">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="md.qsystems.android.tango.permission.MAPS_RECEIVE"/>
<!-- MAP -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
In my simple class (no activity/fragment) I have:
public class LocationHelper {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
I have compile error:
Missing permissions required by LocationManager.requestLocationUpdates: android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION
}
Call this checkAndRequestPermissions() Method whenevr you want permission
public boolean checkAndRequestPermissions() {
int internet = ContextCompat.checkSelfPermission(mContext,
Manifest.permission.INTERNET);
int loc = ContextCompat.checkSelfPermission(mContext,
Manifest.permission.ACCESS_COARSE_LOCATION);
int loc2 = ContextCompat.checkSelfPermission(mContext,
Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (internet != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.INTERNET);
}
if (loc != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (loc2 != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions((Activity) mContext, listPermissionsNeeded.toArray
(new String[listPermissionsNeeded.size()]), 1);
return false;
}
return true;
}
You need to have both of those permissions to your manifest file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
So check your manifest file and make sure that you have both of them.
Add ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION in AndroidManifest File
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Ask for permission at runtime to use device current location as below :
if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}else{
// Write you code here if permission already given.
// Call LocationHelper
}
Handle when user allow to use device current location or not :
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// Write you code here if permission already given.
// Call LocationHelper
} }else{
// you must ask location permission again
}
}

Trying to prompt user to set permissions on app launch in AndroidManifest

Okay, so in my AndroidManifest.xml file, I'm trying to set my permissions such that when the App launches, the user is asked to allow Location + Storage permissions.
I'm working off the BluetoothLeGatt example, and I used the uses-permission-sdk-23 tags to do it.
For reference, here's my code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bluetoothlegatt"
android:versionCode="1"
android:versionName="1.0">
<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
<!-- Declare this required feature if you want to make the app available to BLE-capable
devices only. If you want to make your app available to devices that don't support BLE,
you should omit this in the manifest. Instead, determine BLE capability by using
PackageManager.hasSystemFeature(FEATURE_BLUETOOTH_LE) -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission-sdk-23 android:name="android.permission.BLUETOOTH"/>
<uses-permission-sdk-23 android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#android:style/Theme.Holo.Light">
<activity android:name=".DeviceScanActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DeviceControlActivity"/>
<service android:name=".BluetoothLeService" android:enabled="true"/>
</application>
</manifest>
try this
step 1 :- add permission that you want in manifiest file like this
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION,
step 2 : ask runtime permission like this
String permission = android.Manifest.permission.ACCESS_FINE_LOCATION;
if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.
checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SearchCityClass.this, new String[]
{permission}, PERMISSION_GPS_CODE);
}
step 3: handle permission result like this
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_GPS_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, location_permission_granted_msg, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, location_permission_not_granted_msg, Toast.LENGTH_SHORT).show();
}
}
}
uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
In MainActivity add the following
if (Build.VERSION.SDK_INT < 23) {
//We already have permission. Write your function call over hear
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Here we are asking for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
//If the app is running for second time, then we already have
permission. You can write your function here, if we already have
permission.//
}
}
do same for memory storage..

missing permissions required by BluetoothAdapter.isEnabled.BLUETOOTH

I add bluetooth to my app but am running into the following problem. When I do the code:
BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
}
if(!bluetoothAdapter.isEnabled()) {
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter, 0);
}
The error is on the following line:
if(!bluetoothAdapter.isEnabled())
Error:
Missing permissions required by BluetoothAdapter.isEnabled :android.permissions.BLUETOOTH
I added the following permissions to the android manifest file:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
but am still getting the same issue. Do you know what I did wrong?
You only need <uses-permission> tag since you are using a permission. You don't need <permissions> tag since this is not your custom permission.
Maybe you have placed <uses-permission> tag in the wrong element in manifest, but I cannot say anything else with the information you provided
Since your bluetoothAdapter can be null, either add to second IF null pointer check, or add a RETURN in first IF
It is also nice to have <uses-feature> tag
Let's say you have a button and then you click on it, Bluetooth needs to turn on. Here's a quick example how you would do it for Android 6.0 and above.
First, declare this variable in your Activity/Fragment:
public static final int PERMISSION_ASK = 1001;
Now, let's say this buttons is going to enable the bluetooth.
setOnClickListener:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(isBluetoothPermissionGranted()) {
// app already has required permissions
// do some task here
} else {
// app does not have permission yet.
// ask for permissions
askForBluetoothPermissions();
}
} else {
// Android version below 6.0, no need to check or ask for permission
// do some task here
}
}
});
Checking if application already has the required permission:
#RequiresApi(api = Build.VERSION_CODES.M)
private boolean isBluetoothPermissionGranted() {
boolean granted = false;
int bluetoothGranted = checkSelfPermission(Manifest.permission.BLUETOOTH);
int bluetoothAdminGranted = checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN);
if(bluetoothGranted == PackageManager.PERMISSION_GRANTED &&
bluetoothAdminGranted == PackageManager.PERMISSION_GRANTED) {
granted = true;
}
return granted;
}
If required permission is not granted, here's how you would ask for it:
#RequiresApi(api = Build.VERSION_CODES.M)
private void askForBluetoothPermissions() {
String[] permissions = new String[] {
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN
};
requestPermissions(permissions, PERMISSION_ASK);
}
And finally, here's how you would confirm if the user granted you the permission(s) or not:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_ASK:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
// all requested permissions were granted
// perform your task here
} else {
// permissions not granted
// DO NOT PERFORM THE TASK, it will fail/crash
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
Hope this helps
For Android 6.0 and above you must add the following to manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<permission android:name="android.permission.BLUETOOTH" android:label="BLUETOOTH" />
<permission android:name="android.permission.BLUETOOTH_ADMIN" />
<permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Categories

Resources