Updated 13 November 2019
Android Biometric Api is part of the AndroidX Biometric Library for biometric authentication. We can check biometric sensor in device with the help of Biometric Api. It provides UI to developers. There is no need to create custom UI. It supports all type of biometric authentication in device.
NOTE: For Android Biometric Api, min support Android version is Android P.
Android 6.0 (API level 23) introduced FingerprintManager. It is used only for fingerprint sensors. It doesn’t provide any UI to developers Developers needed to build their own fingerprint UI. In Android 9.0(P) FingerprintManager is deprecated. It releases a new API called BiometricPrompt.
Use to manage system-provided biometric dialog.
1. Add dependency in gradle
1 2 3 4 5 |
dependencies { def biometric_version = "1.0.0" implementation "androidx.biometric:biometric:$biometric_version" } |
2. In your Activity/Fragment, check whether your device supports any biometric authentication.
1 2 3 4 |
val biometricManager = BiometricManager.from(context) if (biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS){ } |
3. Create a instance of BiometricPromptThe BiometricPrompt constructor needs 2 things ->
a. Executor -> specify a thread for callbacks
b. AutheticationCallback object -> object which has three methods
onAuthenticationSucceeded() -> called after successful login with biometric
onAuthenticationError() -> called when unrecoverable error occurs
onAuthenticationFailed() -> called when user is rejected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private fun getInstanceOfBiometricPrompt(): BiometricPrompt { val executor = ContextCompat.getMainExecutor(this) val callback = object: BiometricPrompt.AuthenticationCallback() { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { super.onAuthenticationError(errorCode, errString) } override fun onAuthenticationFailed() { super.onAuthenticationFailed() } override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { super.onAuthenticationSucceeded(result) } } val biometricPrompt = BiometricPrompt(this, executor, callback) return biometricPrompt } |
4. Create prompt object to show biometric app.
1 2 3 4 5 6 7 8 9 |
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 } |
5. Now, call authenticate function for biometric authentication
1 |
biometricPrompt.authenticate(getPromptInfo()) |
Example ->
To know more about Android Biometric Api, check following link -> Biometric
Hope, this blog is helpful for you.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.