1

I have a Bluetooth application in Android 10 and before I can start to scan for the devices I need to request the location permissions. When I install the application for the first time I get the popup for the location, but after that, if I turn them off it won't ask again and I need to ask the user to turn them on every time if they are off.

Here's my code:

override fun onStart() {
    super.onStart()
    requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == PERMISSION_REQUEST_CODE && permissions.contains(Manifest.permission.ACCESS_FINE_LOCATION)) {
        if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
            if (!BluetoothAdapter.getDefaultAdapter().isEnabled) {
                startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), ENABLE_BLT_REQUEST_CODE)
            } else {
                viewModel.discoverBLTDevices()
            }
        } else {
            showToast("Location permission needed to scan for devices.")
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == ENABLE_BLT_REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            viewModel.discoverBLTDevices()
        } else if (resultCode == Activity.RESULT_CANCELED) {
            finish()
        }
    }
}
Netheru
  • 301
  • 1
  • 5
  • 19
  • https://stackoverflow.com/questions/61339719/how-to-ask-user-to-turn-on-location/61406934#61406934 – MMG Apr 29 '20 at 12:56

1 Answers1

1

First time you ask for permission, if user denied, second time you should show custom dialog (or something else) to inform to user that he / she should allow location permission from setting, also navigate to settings.

Example:

private fun requestLocationPermissions() {
    // shouldShowRequestPermissionRationale method checks permission is not granted. 
    // So Requesting permission

    if (!shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) 
|| !shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) {
    requestPermission(PERM_ID, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION))
  } else {
     showPermissionRequiredDialog()
  }
}


fun showPermissionRequiredDialog() {
    val alertDialog = MaterialAlertDialogBuilder(context, R.style.DarkDialogTheme)
        alertDialog.setTitle("title")
        alertDialog.setMessage("your message to user")
        alertDialog.setPositiveButton(R.string.label_ok, DialogInterface.OnClickListener { dialog, which ->
            val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + BuildConfig.APPLICATION_ID))
            context.startActivity(intent)
        })
        alertDialog.show()

    }
}

Also Take look this

Narek Hayrapetyan
  • 1,731
  • 15
  • 27