Skip to content Skip to sidebar Skip to footer

How To Use Fingerprint Scanner To Authenticate Users

I have been developing my personal android app to store my passwords. (Since lastpass is paid for mobile). I currently use simple password authentication, but i would love to be ab

Solution 1:

I create a custom handler class for fingerprint event :

import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;

publicclassFingerprintHandler {
    privateContext                                     mContext;
    privateFingerprintManager                          mFingerprintManager = null;
    privateCancellationSignal                          mCancellationSignal;
    privateFingerprintManager.AuthenticationCallback   mAuthenticationCallback;
    privateOnAuthenticationSucceededListener           mSucceedListener;
    privateOnAuthenticationErrorListener               mFailedListener;

    publicinterfaceOnAuthenticationSucceededListener {
        voidonAuthSucceeded();
    }

    publicinterfaceOnAuthenticationErrorListener {
        voidonAuthFailed();
    }

    publicvoid setOnAuthenticationSucceededListener (OnAuthenticationSucceededListener listener){
        mSucceedListener = listener;
    }

    publicvoidsetOnAuthenticationFailedListener(OnAuthenticationErrorListener listener) {
        mFailedListener = listener;
    }

    publicFingerprintHandler(Context context){
        mContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mFingerprintManager = context.getSystemService(FingerprintManager.class);
            mCancellationSignal = newCancellationSignal();

            mAuthenticationCallback = newFingerprintManager.AuthenticationCallback() {
                @OverridepublicvoidonAuthenticationError(int errorCode, CharSequence errString) {
                    super.onAuthenticationError(errorCode, errString);
                }

                @OverridepublicvoidonAuthenticationHelp(int helpCode, CharSequence helpString) {
                    super.onAuthenticationHelp(helpCode, helpString);
                }

                @OverridepublicvoidonAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                    super.onAuthenticationSucceeded(result);
                    if( mSucceedListener != null )
                        mSucceedListener.onAuthSucceeded();
                }

                @OverridepublicvoidonAuthenticationFailed() {
                    super.onAuthenticationFailed();
                    if (mFailedListener != null)
                        mFailedListener.onAuthFailed();
                }
            };
        }
    }

    publicvoidstartListening(){
        if (isFingerScannerAvailableAndSet() ) {
            try{
                mFingerprintManager.authenticate(null, mCancellationSignal, 0/* flags */, mAuthenticationCallback, null);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    publicvoidstopListening(){
        if ( isFingerScannerAvailableAndSet() ) {
            try {
                mCancellationSignal.cancel();
                mCancellationSignal = null;
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    publicbooleanisFingerScannerAvailableAndSet(){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
            returnfalse;
        if( mFingerprintManager == null )
            returnfalse;
        if( !mFingerprintManager.isHardwareDetected() )
            returnfalse;
        if( !mFingerprintManager.hasEnrolledFingerprints())
            returnfalse;

        returntrue;
    }
}

Then in your activity implement

FingerprintHandler.OnAuthenticationSucceededListener, FingerprintHandler.OnAuthenticationErrorListener

Create fingerprint parameter :

private FingerprintHandler mFingerprintHandler;

After that init this fingerprint handler in onCreate method :

mFingerprintHandler = new FingerprintHandler(this);
mFingerprintHandler.setOnAuthenticationSucceededListener(this);
mFingerprintHandler.setOnAuthenticationFailedListener(this);

You can check if fingerprint is avaivable and set in your activity with this :

if( mFingerprintHandler.isFingerScannerAvailableAndSet() ){
        // show image or text or do something 
    }

You can handle your fingerprint response in implemented methods :

@Override
public void onAuthSucceeded() {
     //fingerprint auth succeded go to next activity (or do something)
}


@Override
public void onAuthFailed() {
    //fingerpring auth failed, show error toast (or do something)
}

And you are ready ! :) Don't forget to stop and start listening the fingerprint in onPause and onResume methods :

@OverridepublicvoidonResume() {
    super.onResume();
    mFingerprintHandler.startListening();
}

@OverridepublicvoidonPause() {
    super.onPause();
    mFingerprintHandler.stopListening();
}

Happy codding :)))

Solution 2:

You can use this. It supports all the locking mechanisms (PIN, Pattern, Password, Fingerprint Scanner).

IntentcredentialsIntent=null;
KeyguardManagerkeyguardManager= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP)
    credentialsIntent = keyguardManager.createConfirmDeviceCredentialIntent("title", "desc"), context.getString(R.string.verification_desc));

//If phone lock is set, launch the unlock screenif (credentialsIntent != null) {
    ((Activity) context).startActivityForResult(credentialsIntent, CREDENTIALS_RESULT);
}
//Phone is not lockedelse {
    doTheWork();
}  

@OverridepublicvoidonActivityResult(int requestCode) {
    if (requestCode == CREDENTIALS_RESULT) {
        doTheWork;
    } 
    else 
        Log.e("TA", "Error");
}

Post a Comment for "How To Use Fingerprint Scanner To Authenticate Users"