Following are the steps to display Biometric authentication prompt :
Step 1 : Add following dependency in build.gradle
implementation "androidx.biometric:biometric:1.1.0"
Step 2 : Add following permission in AndroidManifest.xml
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
Step 3 : Create a BiometricUtils.kt class and add the following code :
package com.androja.mudit.listy.util
import android.content.Context
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.androja.mudit.listy.R
import com.androja.mudit.listy.interfaces.FingerPrintAuthListener
class BiometricUtils {
private var listener: FingerPrintAuthListener? = null
/**
* set listener for the call back back to be calling activity or fragment
*/
fun setListener(listener: FingerPrintAuthListener) {
this.listener = listener
}
/**
* Build biometric prompt and authentication listener
*/
private fun initBiometricPrompt(activity: FragmentActivity): BiometricPrompt {
val executor = ContextCompat.getMainExecutor(activity)
val callback = object: BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
listener!!.onFingerPrintAuthentication(false)
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
listener!!.onFingerPrintAuthentication(false)
}
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
listener!!.onFingerPrintAuthentication(true)
}
}
return BiometricPrompt(activity, executor, callback)
}
/**
* Add biometric prompt information to be displayed to the user by passing the required parameters
*/
private fun createPromptInfo(title: String, subTitle: String, desc:String, activity: FragmentActivity): BiometricPrompt.PromptInfo {
return BiometricPrompt.PromptInfo.Builder().apply {
setTitle(title)
setSubtitle(subTitle)
setDescription(desc)
setNegativeButtonText(activity.getString(R.string.alert_negative_btn))
}.build()
}
/**
* To display the biometric login prompt
*/
fun showBiometricPrompt(title: String, subTitle: String, desc:String, activity: FragmentActivity) {
val promptInfo = createPromptInfo(title, subTitle, desc, activity)
val biometricPrompt = initBiometricPrompt(activity)
biometricPrompt.apply {
authenticate(promptInfo)
}
}
/**
* To check if the devices supports biometric authentication
*/
fun isBioMetricEnabled(ctx: Context) : Boolean {
val biometricManager = BiometricManager.from(ctx)
return biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK) ==
BiometricManager.BIOMETRIC_SUCCESS
}
}
Step 4 : Add the following code in the calling activity or fragment :
private fun loadFingerPrintUnlock() {
try {
if(SharedPrefs.getInstance(requireContext()).isFingerPrintAuth) {
val biometricUtil = BiometricUtils()
biometricUtil.setListener(this)
biometricUtil.showBiometricPrompt(getString(R.string.biometric_login_title),
getString(R.string.biometric_login_subtitle),
"",
requireActivity())
}
else
parentView!!.findNavController().navigate(R.id.action_homeFragment_to_dashboardFragment)
}
catch (ex: Exception) {
ex.printStackTrace()
}
}
And you are done...