Android Biometric Authentication Example

In this post, you will learn how to implement biometric authentication in android application. Android provides biometric api which is advanced form of fingerprint authentication.

In Biometric Authentication, you can also authenticate with your device password or PIN. Biometric Api is easy to use and more secure. FingerPrintManager was deprecated in API 28. Biometric Prompt shows system dialog for authentication process.

 

Add below dependency in your app level build.gradle file

implementation ‘androidx.biometric:biometric:1.1.0-alpha02’
 
 

Add required permission for biometric authentication

<uses-permission android:name=”android.permission.USE_BIOMETRIC”/>
 
 

Check Biometric Features available in device or not

First we need to check our android device supports biometric features or not using biometricManager.canAuthenticate() method. BIOMETRIC_SUCCESS indicates that Authentication is successful.

BIOMETRIC_ERROR_NO_HARDWARE indicates that device hardware does not support biometric authentication. BIOMETRIC_ERROR_HW_UNAVAILABLE indicates that Biometric Features are currently unavailable in device. BIOMETRIC_ERROR_NONE_ENROLLED indicates that User has not submitted any biometric credential in device.

val biometricManager = BiometricManager.from(this)

when (biometricManager.canAuthenticate()) {

BiometricManager.BIOMETRIC_SUCCESS -> {
    Log.d(“MainActivity: “, “Application can authenticate with biometrics.”)
    showBiometricDialog()
}

BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
     Log.e(“MainActivity: “, “Biometric facility is not available in this device.”)

BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->
    Log.e(“MainActivity: “, “Biometric facility is currently not available.”)

BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED ->
     Log.e(“MainActivity: “, “Any biometric credential is not added in this device.”)

}

 
 

Create BiometricPrompt Instance

BiometricPrompt class displays UI dialog for biometric authentication. This dialog performs biometric authentication and give results to the application. We also need to provide authentication callbacks to BiometricPrompt instance. There are three types of authentication callbacks.

private var executor: Executor? = null
private var biometricPrompt: BiometricPrompt? = null

executor = ContextCompat.getMainExecutor(this)
executor?.let {

biometricPrompt = BiometricPrompt( this, it,
object : BiometricPrompt.AuthenticationCallback() {

override fun onAuthenticationError( errorCode: Int, errString: CharSequence) {
    super.onAuthenticationError(errorCode, errString)
    Toast.makeText( applicationContext, “Authentication error: $errString”, Toast.LENGTH_SHORT).show()
    finish()
}

override fun onAuthenticationSucceeded( result: BiometricPrompt.AuthenticationResult ) {
    super.onAuthenticationSucceeded(result)
    Toast.makeText( applicationContext, “Authentication Successful”, Toast.LENGTH_SHORT).show()
}

override fun onAuthenticationFailed() {
    super.onAuthenticationFailed()
    Toast.makeText( applicationContext, “Authentication failed”, Toast.LENGTH_SHORT).show()
    finish()
}

})
}

 
 

Create BiometricPromptInfo Instance

We can submit UI changes in BiometricPromptInfo instance. We can create this instance using BiometricPrompt.PromptInfo.Builder().

private var biometricPromptInfo: BiometricPrompt.PromptInfo? = null

biometricPromptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle(“Biometric Authentication”)
    .setSubtitle(“Use your biometric credential for authentication”)
    .setDeviceCredentialAllowed(true)
    .build()

 
 

Start Biometric Authentication

We can start authentication process using authenticate() method. We can also cancel authentication using cancelAuthentication() method.

biometricPromptInfo?.let {
    biometricPrompt?.authenticate(it)
}
 
 
 
android biometric authentication
 
 

Leave a Reply