TheJadav

Biometric API in Android – Add security in app

With Android 10, android introduced biometric api. It is important to add security to app, this api makes it easy. All you need to just implement one single api and you can use all the security features which user have in device, like facelock, fingerprint lock, iris

Biometrics api

With Android 10, android introduced biometric api. It is important to add security to app, this api makes it easy. All you need to just implement one single api and you can use all the security features which user have in device, like facelock, fingerprint lock, iris. Thanks to google for this awesome api.

Import biometric api

  dependencies {
    def biometric_version = "1.0.0" //check for latest version

    implementation "androidx.biometric:biometric:$biometric_version"
  }

Is my device supports biometric authentication?

You can simply check whether device supports biometric API or not with BiometricManager.

        val biometricManager = BiometricManager.from(this)
        if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS){
            Toast.makeText(this, "My device supports biomatric", Toast.LENGTH_SHORT).show()
        }

Display biometric prompt

We need instance of BiometricPrompt which requires PromptInfo, display to user. Below methods can be used for that:

    private fun instanceOfBiometricPrompt(): BiometricPrompt {
        val executor = ContextCompat.getMainExecutor(this)

        val callback = object: BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                showMessage("$errorCode :: $errString")
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                showMessage("Authentication failed for an unknown reason")
            }

            override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                showMessage("Authentication was successful")
            }
        }

        return BiometricPrompt(this, executor, callback)
    }

    private fun getPromptInfo(): BiometricPrompt.PromptInfo {
        val promptInfo = BiometricPrompt.PromptInfo.Builder()
                .setTitle("My App's Authentication")
                .setSubtitle("Please login to get access")
                .setDescription("My App is using Android biometric authentication")
                .setDeviceCredentialAllowed(true)
                .build()
        return promptInfo
    }

    private fun showMessage(s: String) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show()
    }

Note:
This is only used for those devices which have biometric authentication or which supports biometric authentication.

This will use default authentication which used by user for his device.

Pros:

  • Single or common UI for all. 
  • No explicitly check for which authentication way user used
  • It also uses its alternative unlock pattern with setDeviceCredentialAllowed(true)

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message