Fingerprint Authentication using BiometricPrompt
In this blog, we are going to learn about Fingerprint Authentication using BiometricPrompt.The Android Framework and Security team recently released the AndroidX Biometric Library, a support library that supersedes all previous iterations of the API.
The library makes all the features announced in Android 10 (API level 29) available all the way back to Android 6 (API level 23).
BiometricPropt UI :
BiometricPropt provides there own stable UI and Dialogs. In the previous FingurePrint Manager, there is no option of the UI. There is only a custom view.
Android app integration :
1 |
implementation 'androidx.biometric:biometric:1.0.0-alpha03' |
Integrate this dependency to the build.gradle (app) file.
Check whether your device supports any biometric authentication. :
Firstly, You should create and biometricManager object.
1 2 3 4 |
val biometricManager = BiometricManager.from(context) if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS){ } |
Create an instance of BiometricPrompt:
In this paragraph, We are going to create a BiometricPropt object. Firstly, You should instantiate BiometricPrompt
early in your Activity
/Fragment
lifecycle, preferably inside onCreate()
or onCreateView()
. This is a key difference between FingerprintManager
and BiometricPrompt
. It was OK to instantiate FingerprintManager
just in time to call authenticate()
. But for BiometricPrompt
to send callbacks to the proper activity, say in case of configuration changes, you must instantiate it a bit before you need to call authenticate()
.
1 2 3 |
private var executor: Executor? = null private var biometricPrompt: BiometricPrompt? = null private var promptInfo: PromptInfo? = null |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
executor = ContextCompat.getMainExecutor(this@HomeActivity) biometricPrompt = BiometricPrompt(this@HomeActivity, executor!!, object : BiometricPrompt.AuthenticationCallback() { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { super.onAuthenticationError(errorCode, errString) Toast.makeText(applicationContext, "Authentication error: $errString", Toast.LENGTH_SHORT) .show() } override fun onAuthenticationSucceeded( result: BiometricPrompt.AuthenticationResult) { super.onAuthenticationSucceeded(result) Toast.makeText(applicationContext, "Authentication succeeded!", Toast.LENGTH_SHORT).show() } override fun onAuthenticationFailed() { super.onAuthenticationFailed() Toast.makeText(applicationContext, "Authentication failed", Toast.LENGTH_SHORT) .show() } }) |
Create PrompInfo object to show the biometric prompt dialog :
BiometricPrompt.PromptInfo
is a required parameter for authenticating with the BiometricPrompt
API. It supplies important instructions to the prompt, such as whether explicit user confirmation is required (note: explicit confirmation is the default behavior, and should always be applied if the API is being used for payments or transactions — for use with app or account sign-in, the explicit confirmation can be set to false to enable a more streamlined experience). BiometricPrompt.PromptInfo
also allows your app to provide additional context for the ongoing transaction by setting a title, subtitle, and other descriptive text to appear on the prompt.
Your BiometricPrompt.PromptInfo
might look like this:
1 2 3 4 5 |
promptInfo = PromptInfo.Builder() .setTitle("Biometric login for my app") .setSubtitle("Log in using your biometric credential") .setNegativeButtonText("Use account password") .build() |
Now you just need to call to authenticate method of biometriPrompt :
1 |
biometricPrompt!!.authenticate(promptInfo!!) |
Fingerprint Authentication using BiometricPrompt :
Here we achieve this,