2

Okay so my application is to be used on a device that does not have a fingerprint sensor but has a face unlock option. I tested the following code with my phone (Xiaomi Redmi note 8 pro) which has both fingerprint and face unlock options. the fingerprint unlock is working fine. But if I remove my fingerprints from the phone settings, the face unlock is not working.

private fun setUpBiometric() {
        val biometricManager = BiometricManager.from(this)
        when (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
            BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
                activityBinding.biometricCheck.visibility = View.GONE
                activityBinding.words.text = "No biometric features available on this device."
            }
            BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
                activityBinding.biometricCheck.visibility = View.GONE
                activityBinding.words.text = "Biometric features are currently unavailable."
            }
            BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
                // Prompts the user to create credentials that your app accepts.
                val enrollIntent = Intent(Settings.ACTION_BIOMETRIC_ENROLL).apply {
                    putExtra(
                        Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
                        android.hardware.biometrics.BiometricManager.Authenticators.BIOMETRIC_STRONG or android.hardware.biometrics.BiometricManager.Authenticators.DEVICE_CREDENTIAL
                    )
                }
                this.startActivityForResult(enrollIntent, 1)
            }

        }

        executor = ContextCompat.getMainExecutor(this)
        biometricPrompt = BiometricPrompt(this, executor,
            object : BiometricPrompt.AuthenticationCallback() {
                override fun onAuthenticationError(
                    errorCode: Int,
                    errString: CharSequence
                ) {
                    super.onAuthenticationError(errorCode, errString)
                    Toast.makeText(
                        applicationContext,
                        "Authentication error: $errString", Toast.LENGTH_SHORT
                    )
                        .show()
                }

                override fun onAuthenticationSucceeded(
                    result: BiometricPrompt.AuthenticationResult
                ) {
                    super.onAuthenticationSucceeded(result)

                    Log.d("TAG", "onAuthenticationSucceeded: "+result.authenticationType)
                    val prefs = SharedPrefsRepository(this@LoginActivity)
                    CoroutineScope(Dispatchers.IO).launch {
                        prefs.saveUser(LoginResponse())
                        startActivity(Intent(this@LoginActivity, HomeActivity::class.java))
                        finish()
                    }

                }

                override fun onAuthenticationFailed() {
                    super.onAuthenticationFailed()
                    Toast.makeText(
                        applicationContext, getString(R.string.authetication_failed),
                        Toast.LENGTH_SHORT
                    )
                        .show()
                }
            })

        promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle(getString(R.string.biometric_login))
            .setSubtitle(getString(R.string.use_biometric_for_login))
            .setNegativeButtonText(getString(R.string.cancel))
            .build()

    }

this is called on a click listener.

biometricPrompt.authenticate(promptInfo)

can someone please tell me what to do for face unlock?

Ammar Ahsan
  • 83
  • 1
  • 9
  • That a device has face unlock doesn't necessarily mean that face recognition is available to third-party apps. The requirements from Google for a biometric sensor to be exposed to third-party apps are higher compared to when it's used for unlocking the device. – Michael Feb 17 '22 at 09:57
  • Is there any list of devices that Google has where the face unlock is allowed/not allowed for third party apps? – Ammar Ahsan Feb 17 '22 at 10:06

0 Answers0