Android fingerprint recognition

Keywords: Android SDK

Basic Introduction of Android Fingerprint Recognition

In recent projects, there is a need for fingerprint identification, no contact. Looking at API, we found that fingerprint identification was added from Android 6.0, that is, API version must be at least 23 or more. To verify user identity through fingerprint scanning, please get new. FingerprintManager Instances of classes and calls authenticate() Method. Your application must run on compatible devices with fingerprint sensors. You must implement the user interface of fingerprint authentication flow in your application, and The standard Android fingerprint icon is used in the UI.

1. The API version must be judged first, and the API version of the system must be obtained first.

minSdkVersion= Build.VERSION.SDK;  

2. Determine whether the device supports fingerprint privileges:

manager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE);  
keyManager= (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);  
//Does hardware support fingerprint unlocking  
        if (!manager.isHardwareDetected()) {  
            Toast.makeText(getBaseContext(), "The phone does not support fingerprint unlocking", Toast.LENGTH_SHORT).show();  
            return false;  
        }  


3. There are also permissions, which are added after 6.0:

<uses-permission
        android:name="android.permission.USE_FINGERPRINT" />

4. The next step is fingerprint identification.
The main callbacks are Fingerprint Manager.Authentication Callback:
 onAuthenticateSuccess();
 onAuthenticateFailed();
 onAuthenticateError();

manger.authenticate(cryptoObject, mCancellationSignal, 0, mAuthCallback, null);
The four parameters here are:
  1. The first parameter is an encrypted object. The CryptoObject object here is created using the Cipher object: new Fingerprint Manager. CryptoObject (cipher).
  2. The second parameter is a CancellationSignal object, which provides the ability to cancel operations. Creating this object is also simple, using new Cancellation Signal ().
  3. The third parameter is a flag, which defaults to 0.
  4. The fourth parameter is the AuthenticationCallback object, which itself is an abstract class in the FingerprintManager class. This class provides several callback methods for fingerprint identification, including the success and failure of fingerprint identification. We need to rewrite it.
  5. The last Handler, which can be used to handle callback events, can pass null.

Direct paste code, you can refer to:
import android.Manifest;  
import android.app.KeyguardManager;  
import android.content.Context;  
import android.content.Intent;  
import android.content.pm.PackageManager;  
import android.hardware.fingerprint.FingerprintManager;  
import android.os.Bundle;  
import android.os.CancellationSignal;  
import android.support.v4.app.ActivityCompat;  
import android.support.v4.app.FragmentActivity;  
import android.util.Log;  
import android.view.View;  
import android.widget.Button;  
import android.widget.Toast;  
  
  
  
public class MainActivity extends FragmentActivity {  
    FingerprintManager manager;  
    KeyguardManager mKeyManager;  
    private final static int REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS = 0;  
    private final static String TAG = "finger_log";  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        manager = (FingerprintManager) this.getSystemService(Context.FINGERPRINT_SERVICE);  
        mKeyManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);  
        Button btn_finger = (Button) findViewById(R.id.btn_activity_main_finger);  
        btn_finger.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                if (isFinger()) {  
                    Toast.makeText(MainActivity.this, "Please do fingerprint identification", Toast.LENGTH_LONG).show();  
                    Log(TAG, "keyi");  
                    startListening(null);  
                }  
            }  
        });  
  
    }  
  
    public boolean isFinger() {  
  
        //On android studio, no error will be reported.  
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {  
            Toast.makeText(this, "No fingerprint recognition privileges", Toast.LENGTH_SHORT).show();  
            return false;  
        }  
        Log(TAG, "Have fingerprint authority");  
        //Determining whether the hardware supports fingerprint recognition  
        if (!manager.isHardwareDetected()) {  
            Toast.makeText(this, "No fingerprint recognition module", Toast.LENGTH_SHORT).show();  
            return false;  
        }  
     Log(TAG, "Fingerprint module");  
        //Determine whether to open the lock screen password  
  
        if (!mKeyManager.isKeyguardSecure()) {  
            Toast.makeText(this, "No lock screen password opened", Toast.LENGTH_SHORT).show();  
            return false;  
        }  
        Log(TAG, "Lock screen password turned on");  
        //Determine whether there is fingerprint entry  
        if (!manager.hasEnrolledFingerprints()) {  
            Toast.makeText(this, "No fingerprints were entered", Toast.LENGTH_SHORT).show();  
            return false;  
        }  
       Log(TAG, "Input fingerprint");  
  
        return true;  
    }  
  
    CancellationSignal mCancellationSignal = new CancellationSignal();  
    //callback  
    FingerprintManager.AuthenticationCallback mSelfCancelled = new FingerprintManager.AuthenticationCallback() {  
        @Override  
        public void onAuthenticationError(int errorCode, CharSequence errString) {  
            //However, after many times of fingerprint password verification errors, it enters this method; moreover, fingerprint verification can not be called in a short time.  
            Toast.makeText(MainActivity.this, errString, Toast.LENGTH_SHORT).show();  
            showAuthenticationScreen();  
        }  
  
        @Override  
        public void onAuthenticationHelp(int helpCode, CharSequence helpString) {  
  
            Toast.makeText(MainActivity.this, helpString, Toast.LENGTH_SHORT).show();  
        }  
  
        @Override  
        public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {  
  
            Toast.makeText(MainActivity.this, "Successful fingerprint identification", Toast.LENGTH_SHORT).show();  
        }  
  
        @Override  
        public void onAuthenticationFailed() {  
            Toast.makeText(MainActivity.this, "Failure of fingerprint identification", Toast.LENGTH_SHORT).show();  
        }  
    };  
  
  
    public void startListening(FingerprintManager.CryptoObject cryptoObject) {  
        //On android studio, no error will be reported.  
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {  
            Toast.makeText(this, "No fingerprint recognition privileges", Toast.LENGTH_SHORT).show();  
            return;  
        }  
        manager.authenticate(cryptoObject, mCancellationSignal, 0, mSelfCancelled, null);  
  
  
    }  
  
    /**  
     * Lock screen password  
     */  
    private void showAuthenticationScreen() {  
  
        Intent intent = mKeyManager.createConfirmDeviceCredentialIntent("finger", "Test fingerprint recognition");  
        if (intent != null) {  
            startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);  
        }  
    }  
  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {  
            // Challenge completed, proceed with using cipher  
            if (resultCode == RESULT_OK) {  
                Toast.makeText(this, "Identify success", Toast.LENGTH_SHORT).show();  
            } else {  
                Toast.makeText(this, "Recognition failure", Toast.LENGTH_SHORT).show();  
            }  
        }  
    }  
  
    private void Log(String tag, String msg) {  
        Log.d(tag, msg);  
    }  
}  




Posted by steadyguy on Wed, 03 Apr 2019 19:48:31 -0700