Android Development

How to use Biometric(Fingerprint) in Android?

how-to-use-biometric-fingerprint-in-android

1) What is Biometric? -> Authenticate by using biometric data, and perform cryptographic operations

  • Declaring dependencies -> Add the dependencies for the artifacts you need in the build.gradle file for your app or module:

         

         dependencies {

				
					 
  // Java language implementation
implementation "androidx.biometric:biometric:1.1.0"

// Kotlin
implementation "androidx.biometric:biometric-ktx:1.2.0- 
 alpha05"
				
			

       }
2) How to show a biometric authentication dialog?

-> Implementing biometric authentication, such as face recognition or fingerprint recognition, is one way to safeguard sensitive information or premium content within your app. This guide will show you how to support biometric login processes in your app.

  • Declare the strong authentication that your app supports. -> To define the types of authentication that your app supports, use the BiometricManager.Authenticators interface. The system lets you declare the following types of authentication:
    • BIOMETRIC_STRONG
    • BIOMETRIC_WEAK
    • DEVICE_CREDENTIAL : (Authentication using a screen lock credential – the user’s PIN, pattern, or password.)
  • Pass an authentication type or a bitwise mixture of types into the setAllowedAuthenticators() function to define the forms of biometric authentication that your app permits. The code snippet below illustrates how to implement authentication using a Class 3 biometric or a screen lock credential :
				
					 promptInfo = new 
   BiometricPrompt.PromptInfo.Builder()
   .setTitle("Biometric login for my app")
   .setSubtitle("Log in using your biometric 
  credential")
  .setAllowedAuthenticators(BIOMETRIC_STRONG | 
   DEVICE_CREDENTIAL)
 .build();
				
			

Check that biometric authentication is available

				
					 BiometricManager biometricManager = BiometricManager.from(this);

				
			

switch (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)) { case BiometricManager.BIOMETRIC_SUCCESS: Log.d(“MY_APP_TAG”, “App can authenticate using biometrics.”); break; case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE: Log.e(“MY_APP_TAG”, “No biometric features available on this device.”); break; case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE: Log.e(“MY_APP_TAG”, “Biometric features are currently unavailable.”); break; case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED: // Prompts the user to create credentials that your app accepts. final Intent enrollIntent = new Intent(Settings.ACTION_BIOMETRIC_ENROLL); enrollIntent.putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED, BIOMETRIC_STRONG | DEVICE_CREDENTIAL); startActivityForResult(enrollIntent, REQUEST_CODE); break; }

  • Complete the following steps to add biometric authentication to your app using the Biometric library:

1). Include a link to the androidx.biometric library as a dependency in the build.gradle file for your app module.

2). Using the logic in the following code snippet, display the biometric login dialog in the activity or fragment that hosts it:

				
					private Executor executor;
   private BiometricPrompt biometricPrompt;
  private BiometricPrompt.PromptInfo promptInfo;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_login);
    executor = ContextCompat.getMainExecutor(this);
    biometricPrompt = new 
 BiometricPrompt(MainActivity.this,
        executor, new 
  BiometricPrompt.AuthenticationCallback() {
    @Override
    public void onAuthenticationError(int errorCode,
            @NonNull CharSequence errString) {
        super.onAuthenticationError(errorCode, errString);
        Toast.makeText(getApplicationContext(),
            "Authentication error: " + errString, 
      Toast.LENGTH_SHORT)
            .show();
       }

     @Override
      public void onAuthenticationSucceeded(
            @NonNull BiometricPrompt.AuthenticationResult 
       result) {
        super.onAuthenticationSucceeded(result);
        Toast.makeText(getApplicationContext(),
            "Authentication succeeded!", 
       Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onAuthenticationFailed() {
        super.onAuthenticationFailed();
        Toast.makeText(getApplicationContext(), 
    "Authentication failed",
            Toast.LENGTH_SHORT)
            .show();
       }
     });

     promptInfo = new BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric 
   credential")
        .setNegativeButtonText("Use account password")
        .build();

   // Prompt appears when user clicks "Log in".
   // Consider integrating with the keystore to unlock 
  cryptographic operations,
   // if needed by your app.
    Button biometricLoginButton = 
  findViewById(R.id.biometric_login);
   biometricLoginButton.setOnClickListener(view -> {
        biometricPrompt.authenticate(promptInfo);
   });
     }
				
			
Share On: