Fingerprint authentication in android

Tram Ho

Nowadays, fingerprint authentication has become popular when online payments are so familiar to us.

Today’s article I want to focus on this topic, how can we deploy fingerprint authentication in Android, just 4 basic steps, let’s get started!

Step 1: Add permission permissions to AndroidManifest.xml

Step 2: Check if the device supports biometric authentication

We will check the conditions:

  • The device is running Android 6.0 and above
  • The device has a fingerprint sensor
  • The user has granted your application access to the fingerprint sensor.
  • User has registered at least one fingerprint on their device.

We can create an Util class to test the above conditions: public class BiometricUtils {

Step 3: Display the BiometricPrompt dialog

Once the conditions in step 2 have been verified, can we check if the Android version of the device is Android P? because BIometricPrompt dialog is only supported in Android P

Using the BiometricPrompt Builder, we can:

  • setTitle () – Set a title to display (Required)
  • setSubtitle () – Set subtitles to display (Optional)
  • Description setDes () – Set a description to display (Optional)
  • setNegativeButton () – Sets the text for the negative button (Required). You must also provide an Executor version and a click listener for the negative button. Note: You can customize the icon or error message used in a dialog.

Step 4: Handling authentication via callback functions

Next, we use BiometricPrompt.AuthenticationCallback to listen for authentication events from the user. It includes 4 methods:

onAuthenticationSceeded

This function will be run when the fingerprint has been successfully matched to one of the registered fingerprints on the device, from which the object will be authenticated.

onAuthenticationFails

This function will be run when the fingerprint does not match any fingerprint registered on the device, this callback will be triggered.

onAuthenticationError

This function will be run when an unrecoverable error is encountered and the authentication process has completed without success. The function is provided with an error code to identify the cause of the error, along with an error message. The different types of error codes that can occur are:

  • BIOMETRIC_ERROR_LOCKOUT THEM Operation was canceled because the API was locked due to too many attempts.
  • BIOMETRIC_ERROR_LOCKOUT_PERMANENT – The operation was canceled because BIOMETRIC_ERROR_LOCKOUT happened too many times.
  • BIOMETRIC_ERROR_NOClickACE – The operation could not be completed because there is not enough space to complete the operation.
  • BIOMETRIC_ERROR_TIMEOUT – The timeout occurred because the current request has run too long.
  • BIOMETRIC_ERROR_UNABLE_TO_PROCESS – The sensor cannot process the current image.
  • BIOMETRIC_ERROR_USER_CANCELED – The user has canceled the operation.
  • BIOMETRIC_ERROR_VENDOR – If there are conditions that do not fall into any of the above categories.
  • BIOMETRIC_ERROR_NO_BIOMETRICS – User does not have any biometrics registered in the device.
  • BIOMETRIC_ERROR_CANCELED – The operation was canceled because the biometric sensor is not available.
  • BIOMETRIC_ERROR_HW_NOT_PRESENT – Devices without biometric sensors.
  • BIOMETRIC_ERROR_HW_UNAVAILABLE – Device hardware not available.

onAuthenticationHelp

This function is called when a non-fatal error occurred during validation. The callback will be provided with a help code to identify the cause of the error, along with a help message. The different types of help codes that can occur are:

  • BIOMETRIC_ACQUIRED_IMAGER_DIRTY – The biometric image is too noisy due to suspected dust on the sensor.
  • BIOMETRIC_ACQUIRED_INSUFFICIENT – Biometric image too noisy to process. This could be for a variety of reasons but in general this is due to the image being unreadable.
  • BIOMETRIC_ACQUIRED_PARTIAL – Only one biometric image was detected.
  • BIOMETRIC_ACQUIRED_TOO_FAST – The biometric image is incomplete because the user moves their finger around the sensor too quickly.
  • BIOMETRIC_ACQUIRED_TOO_SLOW – Biometric image could not be read due to lack of movement from the user around the sensor.

Conclude

So with 4 basic steps, we can deploy the fingerprint login authentication into our project

References

https://proandroiddev.com/5-steps-to-implement-biometric-authentication-in-android-dbeb825aeee8

Share the news now

Source : Viblo