1

我有一个应用程序,为了打开应用程序,用户需要输入他的指纹或使用生物识别 API 中提供的 PIN 码选项(他之前在手机中设置过)。

因此,当用户使用他的指纹验证自己时,它可以正常工作。但是,当用户使用他的 PIN 码进行身份验证时,将使用错误代码 5 调用身份验证错误回调,这不是正常行为。

我试图检查为什么会发生这种情况,但我无法弄清楚。

我的代码如下:

if (!hasEnrollments()) {

        /*
         * This means that a user that previously has enrolled
         * biometrics, no longer have them
         * we hence cannot let the user proceed until
         * new enrolments are configured.
         * We then show a dialog to the user explaining the situation
        */

        biometricEnrolmentDialog()

        return
    }

    _executor = ContextCompat.getMainExecutor(requireContext().applicationContext)
    _biometricPrompt = BiometricPrompt(
        this, _executor,
        object : BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                Log.i("BIO_TEST", "Entered Auth Error")
                Log.i("BIO_TEST", "ErCode: $errorCode errStr: $errString")
                /* Error occurred while authenticating */
                toast(getString(R.string.error_biometric_authentication_error, errString))
                /* Close the app */
                requireActivity().finish()
            }

            override fun onAuthenticationSucceeded(
                result: BiometricPrompt.AuthenticationResult
            ) {
                super.onAuthenticationSucceeded(result)
                Log.i("BIO_TEST", "Entered Auth Succeeded")
                /* Biometric has succeeded */
                viewModel.canNavigateToMainScreen.value = true
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                Log.i("BIO_TEST", "Entered Auth Failed")
                /* Biometric has failed */
                toast(getString(R.string.error_biometric_authentication_failed))
                /* Close the app */
                requireActivity().finish()
            }
        })

    _promptInfo = BiometricPrompt.PromptInfo
        .Builder()
        .setTitle(getString(R.string.text_title_alert_biometric_authentication))
        .setSubtitle(getString(R.string.text_title_alert_biometric_authentication_message))
        .setDeviceCredentialAllowed(true)
        .setConfirmationRequired(false)
        .build()


    /* Prompts the user for biometric authentication */
    _biometricPrompt.authenticate(_promptInfo)

当我使用指纹进行身份验证时,会调用身份验证成功回调,并且应用程序工作正常。但是当我改用 pin 时,它会进入身份验证错误回调并关闭我的应用程序。

我正在使用生物识别 APIandroidx.biometric:biometric:1.0.1

有人可以帮帮我吗 ?

4

0 回答 0