Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permissions error on Android 12, 13 #102

Open
hernando-montoya opened this issue Aug 23, 2022 · 3 comments
Open

Permissions error on Android 12, 13 #102

hernando-montoya opened this issue Aug 23, 2022 · 3 comments

Comments

@hernando-montoya
Copy link

The scanning activity crash.

java.lang.SecurityException: Need android.permission.BLUETOOTH_SCAN permission for android.content.AttributionSource@d39f7202: Starting discovery.

But the permission is already present in the manifest.

@Dumankrg2019
Copy link

Dumankrg2019 commented Sep 14, 2022

tv_device_selected, Maybe you need use permission on run time.

` private fun hasConnectBluetooth() =
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED
private fun hasScanBluetooth() =
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED

private fun requestPermissions() {
    var permissionsToReques = mutableListOf<String>()
    if(!hasScanBluetooth()) {
        permissionsToReques.add(Manifest.permission.BLUETOOTH_SCAN)
    }
    if(!hasConnectBluetooth()) {
        permissionsToReques.add(Manifest.permission.BLUETOOTH_CONNECT)
    }
    if(permissionsToReques.isNotEmpty()) {
        ActivityCompat.requestPermissions(this, permissionsToReques.toTypedArray(), 0)
    }
}
override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)

    if(requestCode == 0 && grantResults.isNotEmpty()) {
        for(i in grantResults.indices) {
            if(grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                Log.e("permission is:", " ${permissions[i]} разрешено")
                initView()
            }
        }
    }
}`

and use requestPermissions fun in your activity or fragment

@tspoke
Copy link
Contributor

tspoke commented Sep 29, 2022

Since android 12 you need to add two extras permissions, depending on your need but in my case I do this using Dexter to request permissions :

final List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissions.add(Manifest.permission.BLUETOOTH);
permissions.add(Manifest.permission.BLUETOOTH_ADMIN);
        
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    permissions.add(Manifest.permission.BLUETOOTH_CONNECT);
    permissions.add(Manifest.permission.BLUETOOTH_SCAN);
}

Dexter.withContext(requireActivity())
                .withPermissions(permissions)
                ....

@salihinalfarizi
Copy link

hi guys @hernando-montoya @tspoke @Dumankrg2019 , i get this issue too, so which solution need to implement for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants