Recently with the release of Android 6.0 (M) last year, there has been a significant amount of changes to the APIs, one of them is Fingerprint Authentication. With the release of new APIs, authenticating users with help of fingerprint sensors on various devices is possible.
Following tutorial example shows how to implement Fingerprint Authentication in your application.
To authenticate users using the fingerprint sensor, you need to get an instance of the newly implemented FingerprintManager class and call the authenticate() method. However your app must be running on a compatible device which includes a fingerprint sensor. Moreover you must implement the user interface for the fingerprint authentication flow on your app, and use the standard Android fingerprint icon in your UI. Note that If you are developing multiple apps that use fingerprint authentication, each app must authenticate the user’s fingerprint separately.
Advantages of Using Fingerprint Authentication
1. Doesn’t matter how sick you are or unable to recollect things, your fingerprint still stays faultless as your identity and can never be misplaced.
2. Fast , Convenient and Reliable to use.
3. Unique fingerprints assure that it’s unlocked just by you.
4. With the help of Fingerprint authentication, online transactions become more convenient, hence your just a tap away from getting verified.
Here is the final app we are about to build.
1. Creating New Project
1. Create a new project in Android Studio from File ⇒ New Project and set the minimum SDK version to Android 6.0 (API 23).
2. Since we are going to work with Fingerprint authentication, we need to add USE_FINGERPRINT permission to AndroidManifest.xml file
3. Open colors.xml file located under res ⇒ values and update the file.
4. Open strings.xml file located under res ⇒ values and update the file.
5. Create the fingerprint icon with the help of “Android Image Assets”. To do so, Right click on the drawable folder and Create a New ⇒ Image Asset named ic_action_fingerprint
2. Creating Fingerprint Activity
6. Create a layout xml file named activity_fingerprint.xml and place the below code.
7. Create an Android Activity class and name it FingeprintActivity.java. The following class includes various methods and functions like, onCreate() method that inflates activity_fingerprint.xml.
> generateKey() function which generates an encryption key which is then stored securely on the device.
> cipherInit() function that initializes the cipher that will be used to create the encrypted FingerprintManager.
> CryptoObject instance and various other checks before initiating the authentication process which is implemented inside onCreate() method.
Add the following final code of Finagerprint Actvity to the FinagerprintActvity.java file.
3. Creating Home Activity
8. Create the Activity by right-clicking your Project, New ⇒ Activity ⇒ Basic Activity and update the following files as shown below.
4. Creating Fingerprint Authentication Handler Class
9. Create a class and name it FingerprintHandler.java. The Handler class extends FingerprintManager.AuthenticationCallback and includes some additional modules. Replace the following set of codes with any existing codes on FingerprintHandler.java file.
5. Testing the Project
Testing on a Physical Device
With the project now complete, debug the app on a physical Android device by placing your fingertip on the fingerprint scanner. If Fingerprint Authentication is succeed, user will be taken to the Home Activity as shown below.
Testing on an Emulator
You could also test the app on an emulator:
> Install Android SDK Tools Revision 24.3, if you have not done so.
> Enroll a new fingerprint in the emulator by going to Settings ⇒ Security ⇒ Fingerprint, then follow the enrollment instructions.
> Use an emulator to emulate fingerprint touch events with the following command. Use the same command to emulate fingerprint touch events on the lockscreen or in your app.
On Windows, you may have to run telnet 127.0.0.1
Reference from AndroidHive