Fingerprint-BiometricAuthentication-Android
Preview
Authentication Succeeded
Authentication Failed
Error
implementation " androidx.biometric:biometric:1.1.0"
private lateinit var executor: Executor
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt .PromptInfo
executor = ContextCompat .getMainExecutor(this )
biometricPrompt =
BiometricPrompt (this , executor, object : BiometricPrompt .AuthenticationCallback () {
override fun onAuthenticationError (errorCode : Int , errString : CharSequence ) {
super .onAuthenticationError(errorCode, errString)
binding.tvAuthStatus.text = " Error " + errorCode
binding.tvAuthStatus.setTextColor(Color .parseColor(" #FF0000" ))
}
override fun onAuthenticationSucceeded (result : BiometricPrompt .AuthenticationResult ) {
super .onAuthenticationSucceeded(result)
binding.tvAuthStatus.text = " Authentication succeeded"
binding.tvAuthStatus.setTextColor(Color .parseColor(" #00FF00" ))
}
override fun onAuthenticationFailed () {
super .onAuthenticationFailed()
binding.tvAuthStatus.text = " Authentication failed"
binding.tvAuthStatus.setTextColor(Color .parseColor(" #9C27B0" ))
}
})
promptInfo = BiometricPrompt .PromptInfo .Builder ()
.setTitle(" Biometric Authentication" )
.setSubtitle(" Log in using fingerPrint" )
.setNegativeButtonText(" Cancel" )
.build()
binding.btnAuth.setOnClickListener {
biometricPrompt.authenticate(promptInfo)
}