Android Fingerprint Authentication Tutorial: How to Guide

Gone are the days when you had to rely on passwords and patterns. In this modern day Android has come a long way. And there are multiple ways for you to look into safety of your data. So, let us look at one of such method: Android fingerprint authentication tutorial.

Let us move forward with step by step tutorial on how to implement fingerprint authentication.

Current approach is the biometric authentication way. There used to be different way ( deprecated now for newer android versions ), which is not covered in this tutorial.

Step #0: Before we begin; android fingerprint authentication tutorial

Before we begin the tutorial. I want to clarify my previous statement. There is one legacy approach for accomplishing the same task.

But on recent android versions everything has been created up-to the standard. So in this tutorial we will only look into the latest approach i.e. Android biometrics.

Step #1: Require biometric permission

Before we can move into showing the biometric prompt for users to authenticate using their fingerprint. We need to ask permission for our app to use the fingerprint sensor.

And we can do so by declaring the following permission in AndroidManifest.xml.

<uses-permission android:name="android.permission.USE_BIOMETRIC" />Code language: HTML, XML (xml)

Step #2: Add biometric dependency

Add the following lines to your app > build.gradle in the dependencies block

def biometricLibraryVersion = "1.1.0"
implementation "androidx.biometric:biometric:$biometricLibraryVersion"Code language: Groovy (groovy)

Step #3: Creating a authentication callback

Let us see the code for creating authentication callback that gets fired when we use the fingerprint authentication.

private val biometricCallback = object : BiometricPrompt.AuthenticationCallback() {
    override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
        super.onAuthenticationSucceeded(result)
println("SUCCESS { $result }")
        // the authentication completed successfully
        // write your code here to do something when authentication successful
    }

    override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
        super.onAuthenticationError(errorCode, errString)
println("ERROR { $errString -> $errorCode }")

        if (errorCode == 7) {

            // show only too many attempts error ; else do nothing
            Toast.makeText(this@MainActivity, errString, Toast.LENGTH_SHORT).show()
        }
    }

    override fun onAuthenticationFailed() {
        super.onAuthenticationFailed()
println("FAILED")
				// the authentication has failed because of some reason 
    }
}Code language: Kotlin (kotlin)

Step #4: Write code to show the authentication prompt

Finally, write the code below to show the biometric prompt to the user.

val biometricPrompt = BiometricPrompt(this, biometricCallback)
val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("Authentication Required")
    .setDescription("We need to verify it's you before proceeding.")
    .setNegativeButtonText("DISMISS")
    .build()
biometricPrompt.authenticate(promptInfo)Code language: Kotlin (kotlin)

After you have successfully integrated the code with your project you can perform a test run. See the Step #3, that is where you will receive the success or failure result of the authentication.

I hope that this android fingerprint authentication tutorial has been of great help to you.

Suggested

Java List Data Structure: Learn Real Use Case Of List

ArrayList in Java: Understand With Example

Run Length Encoding in Java: Learn How To Implement

Interface in OOP: Guide to Polymorphism and Callbacks

Related Posts