Biometric

Biometric is an official Biometric library provided by Google. It uses system-level Biometric authentication, including fingerprints, faces, and irises, that are currently available on Android devices.

To use Biometric libraries to add Biometric authentication, add Biometric library dependencies:

    dependencies {
        implementation 'androidx. Biometric: biometric: 1.0.0 - beta01'
    }
Copy the code

Check whether biometric authentication is available

Check whether biometrics are available: Check whether the device supports biometricauthentication before calling the BiometricPrompt by using the canAuthenticate() method in the BiometricManager class.

    BiometricManager biometricManager = BiometricManager.from(this);
    switch (biometricManager.canAuthenticate()) {
        case BiometricManager.BIOMETRIC_SUCCESS:
            Log.d("Applications can be biometrics for authentication.");
            break;
        case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
            Log.e("There are no working biometric features on the device.");
            break;
        case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
            Log.e("Biometrics is currently unavailable.");
            break;
        case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
            Log.e("User did not enter biometric data.");
            break;
    }
Copy the code

A dialog box is displayed for biological verification

In an Activity or Fragment that requires a dialog, use the logic shown in the following code snippet to display the dialog (Samsung S8) :

    private Handler handler = new Handler();

    private Executor executor = new Executor() {
        @Override
        public void execute(Runnable command) { handler.post(command); }};@Override
    protected void onCreate(Bundle savedInstanceState) {
        // Click the button to pop up biometric verification
        Button biometricLoginButton = findViewById(R.id.biometric_login);
        biometricLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { showBiometricPrompt(); }}); }// Biometric setting
    private void showBiometricPrompt(a) {
        BiometricPrompt.PromptInfo promptInfo =
                new BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric login for my app") // Set the header
                .setSubtitle("Log in using your biometric credential") // Set the prompt under the heading
                .setNegativeButtonText("Cancel") // Set the cancel button
                .build();
                
    // The argument to be supplied, callback
    BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this,
                executor, new BiometricPrompt.AuthenticationCallback() {
            // All kinds of exception callback
            @Override
            public void onAuthenticationError(int errorCode,
                    @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(getApplicationContext(),
                    "Authentication error: " + errString, Toast.LENGTH_SHORT)
                    .show();
            }

            // Successful authentication callback
            @Override
            public void onAuthenticationSucceeded(
                    @NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                BiometricPrompt.CryptoObject authenticatedCryptoObject =
                        result.getCryptoObject();
                // User has verified the signature, cipher, or message
                // authentication code (MAC) associated with the crypto object,
                // so you can use it in your app's crypto-driven workflows.
            }

            // Callback for authentication failure
            @Override
            public void onAuthenticationFailed(a) {
                super.onAuthenticationFailed();
                Toast.makeText(getApplicationContext(), "Authentication failed", Toast.LENGTH_SHORT) .show(); }});// Displays the authentication dialog box
        biometricPrompt.authenticate(promptInfo);
    }
    
Copy the code

Allow the use of password, pattern, PIN code and other non-biometric authentication

Set to be called setDeviceCredentialAllowed PromptInfo (true), PIN allows use of equipment, design, or a password for authentication. Code:

BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric login for my app")
            .setSubtitle("Log in using your biometric credential")
            .setDeviceCredentialAllowed(true)
            .build();
            
Copy the code


The official document at the same time use the Cancel button setDeviceCredentialAllowed (true) and setNegativeButtonText non-biological certification (” Cancel “) The measured complains Can ‘t have to both negative Button Behavior and Device Credential Enabled so no cancel button is added, problem to be determined

Refer to the Android developer documentation

Display biometric authentication dialog box