Fingerprint Verification Using androidx BiometricPrompt

  • 2021-12-13 09:48:43
  • OfStack

After androidsdk version is greater than 29, fingerprint verification using FingerprintManagerCompat is discarded, and the usage of FingerprintManagerCompat is not described here. Skeleton requires the use of the new api to complete fingerprint verification. Of course, BiometricPrompt can not only do fingerprint verification, but this article only explains how to use BiometricPrompt for fingerprint verification.
Official api: https://developer.android.google.cn/reference/androidx/biometric/package-summary? hl=zh-cn

Guide packets first


 implementation 'androidx.biometric:biometric:1.0.1'

And then its construction method


1.BiometricPrompt(@NonNull FragmentActivity fragmentActivity,
        @NonNull Executor executor, @NonNull AuthenticationCallback callback)
  2. BiometricPrompt(@NonNull Fragment fragment,
        @NonNull Executor executor, @NonNull AuthenticationCallback callback)

The two constructor parameters are basically 1. There is an runnable interface in executor, which will be called back after each fingerprint operation. Note: If the method of AuthenticationCallback takes effect, the run method of runnable must be executed in runnable.
There are three callback methods in callback,
1. onAuthenticationError (int errMsgId, CharSequence errString). Fingerprint verification error will call this method, and the value of errMsgId corresponds to the constant in BiometricPrompt
2. onAuthenticationSucceeded (@ NonNull @ NotNull BiometricPrompt. AuthenticationResult result), which is called after fingerprint verification is successful, and the way of successful verification is obtained through result. getAuthenticationType, and the parameter types are checked by themselves.
3. onAuthenticationFailed () identifies failed calls, and the exact timing of the call is not clear. . You can refer to the official documents

One BiometricPrompt. PromptInfo parameter is required to display fingerprint verification, and a pop-up window will pop up for display. Initialize with builder. You can set title, subTitle, description and NegativeButtonText. The usage is as follows


 new BiometricPrompt.PromptInfo.Builder().setTitle("title")
   .setSubtitle("subTitle")
   .setDescription("description")
   .setDeviceCredentialAllowed(false)
   .setNegativeButtonText("button").build()

It should be noted that there can only be one setDeviceCredentialAllowed and setNegativeButtonText, that is, setNegativeButtonText is not empty, setDeviceCredentialAllowed must be false
Verify whether the device opens the fingerprint through BiometricManager. from (context). canAuthenticate () = = BiometricManager.BIOMETRIC_SUCCESS method;
Code presentation:


private BiometricPrompt biometricPrompt;
       private void startFinger(){
       			 biometricPrompt = new BiometricPrompt(this, new Executor() {
              			  @Override
              			  public void execute(Runnable command) {
                 			   command.run();
            			    }
         			   }, new FingerCallBack());
     			  biometricPrompt.authenticate( new BiometricPrompt.PromptInfo.Builder().setTitle("title")
      			 .setSubtitle("subTitle")
       			 .setDescription("description")
       			 .setDeviceCredentialAllowed(false)
      			 .setNegativeButtonText("button").build());
       }
          private void cancelFinger() {
      			  if (biometricPrompt != null) {
          			  biometricPrompt.cancelAuthentication();
     			   }
    }

private class FingerCallBack extends BiometricPrompt.AuthenticationCallback {
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            super.onAuthenticationError(errMsgId, errString);
            Log.e("fingers", "onAuthenticationError");
        }
        @Override
        public void onAuthenticationSucceeded(@NonNull @NotNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            cancelFinger();
            Log.e("fingers", " Successful recognition  onAuthenticationSucceeded");
        }
        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Log.e("fingers", "onAuthenticationFailed  ");
        }
    }

Related articles: