zocada
fingerprint_auth

Android – Integrating Fingerprint authentication in your apps

Fingerprint authentication has been a huge leap towards a better user experience in smartphones. Having this golden feature in your user’s phone and trying to integrate this feature in your applications to increase security and the bonus of better user experience is worth a shot. Let’s see how we can quickly integrate a simple FingerPrint authentication into an android app.

While integrating Fingerprint auth in our applications, we need to take care of quite a lot of things concerning the hardware of the device our users are using. We know that only devices running android Marshmallow and above have support for Fingerprint scanners. Additionally to use the Android’s built-in Fingerprint features the user needs to have enabled fingerprint security in the device and have at least one fingerprint registered in the device. Let’s keep these points in mind and we need to check these things before dispatching a listener for fingerprint authentication.
So, Let’s get started!

Permission for Fingerprint use

We need our application’s manifest file to describe that our application needs permission to use Fingerprint. Let’s add the following line inside the manifest tag inside the AndroidManifest.xml file.

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



Checking Fingerprint availability

As I mentioned above, we need to verify that the user runs appropriate Android version, the device has the necessary hardware for the Fingerprint scanner and the user has at least one fingerprint already registered in the device.
Write down the following method in your activity’s class or create a separate java file and drop this method in there.

Creating a Fingerprint Helper class

Next, we need to create a helper class by extending the FingerprintManager.AuthenticationCallback and we’ll create an interface inside this class for the authentication listner.

@RequiresApi(api = Build.VERSION_CODES.M)
public class FingerprintHelper extends AuthenticationCallback{

    private FingerprintHelperListener listener;

    public FingerprintHelper(FingerprintHelperListener listener) {
        this.listener = listener;
    }

    //interface for the listner
    interface FingerprintHelperListener {
        void authenticationFailed(String error);
        void authenticationSuccess(FingerprintManager.AuthenticationResult result);
    }

}

Additionally, we need to create methods to start authentication, cancel the listner and implement the methods in the AuthenticationCallback to interact with our listner. Now our FilgerprintHelper will look like this.

Implementing the listener in Activity

Once we have created the FingerprintHelper.java and the FingerprintHelperListener inside it, we can implement the listner in our Activity.

public class LoginActivity extends AppCompatActivity implements FingerprintHelper.FingerprintHelperListener {

   //Your onCreate and other pieces of code goes here
   ...
   ...
   //You class should implement the listener and override the below two methods
    @Override
    public void authenticationFailed(String error) {
        //Authentication failed. ie user tried an invalid fingerprint
        Toast.makeText(this, "Invalid Fingeprint", Toast.LENGTH_LONG).show();
    }

    @Override
    public void authenticationSuccess(FingerprintManager.AuthenticationResult result) {
        //Yaay! we have authenticated the user using Fingerprint.
        ...
        //add your logic to continue here.
    }
}

We need to create variables of FingerprintManager and FingeprintHelper in our activity.

private FingerprintHelper fingerprintHelper;
private FingerprintManager fingerprintManager;


Next, we’ll check for the Fingerprint settings using the function checkFingerprintSettings we discussed above in this post and start listneing when the Activity starts and cancel the listening when the activity is paused ie. app went to background. So add the following line of code inside your activity by overriding the onResume() and the onPause() method.

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onResume() {
    super.onResume();
     //Check for the fingerprint permission and listen for fingerprint
     //add additional checks along with this condition based on your logic
    if (checkFingerPrintSettings(this)) {
        //Fingerprint is available, update the UI to ask user for Fingerprint auth
        //start listening for Fingerprint
        fingerprintHelper = new FingerprintHelper(this);
        fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
        fingerprintHelper.startAuth(fingerprintManager, null);
     } else {
        Log.d(TAG, "Finger print disabled due to No Login credentials or no Fingerprint");
     }
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onPause() {
   super.onPause();
   if (fingerprintHelper != null)
       fingerprintHelper.cancel();
}

That’s it! You’re ready to roll with the new feature on your app. Now your Activity class should look something like this.



Also published on Medium.

haxzie

Community Lead with a demonstrated history of working in a non-profit organization. Skilled in Python, Public Speaking, Graphic Design, Writing, and Android Development. Strong community and social services professional with a Bachelor's degree focused in Computer Science from Sahyadri College of Engineering and Management.