Toptal acquires, enhancing custom software leadership

How to Use Android Fingerprint Security

24.04.2019 Aliaksei K.
How to Use Android Fingerprint Security

PIN authentication fades into the background because fingerprint authentication is more quickly and comfortable for application users. According to Androidauthority, over 70 percent of smartphones shipped in 2018 will have fingerprint sensors

You can see global smartphone fingerprint sensor penetration on the graphic below:


Credited from ‘androidauthority’ webpage

Android provides different ways of using fingerprint authenticate. The most popular of them is FingerprintManager added in Android Marshmallow (API level 23). But with the release of the Android Pie (API level 28) it was deprecated. FingerprintManager was replaced by BiometricPrompt. It’s a class that manages a system-provided biometric dialogue. For support device with versions less than API 28 android support library has BiometricPromtCompat, but sometimes it works incorrect (with bugs and application crash). For temporarily fix we can create your own realization and provide FingerprintManager (for 23 =< API version < 28) and BiometricPrompt (API version >= 28) with our own custom AuthManager.

So, let’s start to provide both of them into your project.

What provides Fingerprint AuthManager? 

It’ll be a simple manager with a provider which will allow using BiometricPrompt for devices with API version >= 28 and FingerprintManager for devices with API version < 28 (but not less than 23).

But previously let’s create and talk about fingerprint device states.

  • VERSION_DOES_NOT_ALLOW state: this device state assigned for all devices which have API version less than 23;
  • DEVICE_HARDWARE_DOES_NOT_ALLOW state: this device state assigned for devices which support API version for fingerprint but doesn’t have fingerprint feature;
  • NO_ENROLLED_FINGERPRINTS state: this device state assigned for devices than device has fingerprint feature but the system hasn’t enrolled fingerprints;
  • FINGERPRINT_ALLOW state: this state assigned for devices which support fingerprint feature and has enrolled fingerprints.
  • UNKNOWN_STATE: this state assigned when android can’t provide fingerprint system service.

So, according to states, we should create an enum class with context extension function for check Android fingerprint device state.

Enum class

enum class FingerprintState(val stateMessage: String) {
 FINGERPRINT_ALLOW("Everything will be nice :)"),
 VERSION_DOES_NOT_ALLOW("Your device version doesn't allow for fingerprint"),
 DEVICE_HARDWARE_DOES_NOT_ALLOW("Your device hasn't hardware for fingerprint"),
 NO_ENROLLED_FINGERPRINTS("Please, add fingerprint for device in security settings"),
 UNKNOWN_STATE("Ooooops, wtf");

Context extension method

fun Context.checkDeviceFingerprintState() =
 val fingerprintManager = this.getSystemService(Context.FINGERPRINT_SERVICE) as? FingerprintManager

 if (fingerprintManager == null) {
 } else if (!fingerprintManager.isHardwareDetected) {
 } else if (!fingerprintManager.hasEnrolledFingerprints()) {
 } else {
 } else {

Next step is creating AuthManager. It’ll be base cascade for biometric and fingerprint managers (look at the pseudo scheme below).


AuthManager it’s a simple Kotlin interface and Biometric, Fingerprint managers are realization according to API version.

For listen, user auth state AuthenticationListener should be created. There are three states when a user using a fingerprint feature:

  • Authenticate was successful;
  • Authenticate was failed;
  • Authenticate was cancelled.

AuthManager interface

interface AuthManager {
 fun authenticate()
 fun attachAuthListener(authenticationListener: AuthenticationListener)

Authentication listener interface

interface AuthenticationListener {
 fun onAuthSuccess()
 fun onAuthFailed()
 fun onAuthCancel()

Now let’s create realizations that implement AuthManager interface, but previously we must add permissions in the Android manifest file:

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

Biometric AuthManager

For creating Biometric manager, we should use BiometricPrompt. Builder which builds BiometricPrompt instance. As you remember, BiometricPrompt is a simple dialogue for authenticating with user fingerprint. So, you can build a biometric dialogue with your own title, subtitle, description, etc.

For call system touch sensor listen you should call authenticate() method from BiometricPrompt instance. You must set AuthenticationCallback and CancellationSignal. It’ll look like in the code below:

override fun authenticate() {
 val appName = context.getString(R.string.app_name)
 val biometricPrompt = BiometricPrompt.Builder(context)
 .setDescription(context.getString(R.string.biometric_dialog_description_text, appName))
 context.getString(R.string.cancel_button_text), context.mainExecutor,
 DialogInterface.OnClickListener { _, _ -> mAuthenticationListener?.onAuthCancel() })


 private fun getCancellationSignal(): CancellationSignal {
 // With this cancel signal, we can cancel biometric prompt operation
 val cancellationSignal = CancellationSignal()
 cancellationSignal.setOnCancelListener {
 return cancellationSignal

You can also set CryproObject for BiometricPrompt authenticate() method.

Using AuthenticationCallback for BiometricPrompt authenticate() method we can send events with our AuthenticationListener like in the code below:

private val mBiometricAuthCallback = object : BiometricPrompt.AuthenticationCallback() {

 override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {

 override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {

 override fun onAuthenticationFailed() {

Fingerprint AuthManager

For realization this manager we can create your own dialogue. We should start and stop listening for touch sensor (authenticate() method in FingerprintManager), and initialize FingerprintManager.CryptoObject for this (like in the code below).

private fun isCipherInitSuccess(): Boolean {
 return try {
 mKeyStore = KeyStore.getInstance(ANDROID_KEY_STORE)
 val cipher = Cipher.getInstance(ENCRYPTION_TRANSFORMATION)
 cipher.init(Cipher.ENCRYPT_MODE, mKeyStore.getKey(BIP_KEY_ALIAS, null))
 mCryptoObject = FingerprintManager.CryptoObject(cipher)
 } catch (e: Exception) {
 Log.d(TAG, "isCipherInitSuccess: ${e.message}")
private fun initKeyStoreAndKeyGenerator() {
 fun logException(e: Exception) {
 Log.d(TAG, "initKeyStoreAndKeyGenerator: ${e.message}")

 try {
 mKeyStore = KeyStore.getInstance(ANDROID_KEY_STORE)
 mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE)


 val keyProperties = KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT

 val builder = KeyGenParameterSpec.Builder(BIP_KEY_ALIAS, keyProperties)

 } catch (e: KeyStoreException) {
 } catch (e: IOException) {
 } catch (e: NoSuchAlgorithmException) {
 } catch (e: CertificateException) {
 } catch (e: InvalidAlgorithmParameterException) {

For creating separation between our own fingerprint dialogue logic and UI logic we can create Fingerprint UiHelper with Lottie animation realization. Also, we should listen to FingerprintManager.AuthenticationCallback() for change Lottie animation and send our events using our AuthenticationCallback.

P.S. Creating fingerprint authenticate dialogue is very useful for creating and customizing dialogue design that should match the design of your application.

AuthManager provider

For providing auth manager you are can use DI libraries. Or you can create a provider which throw our own exception with FingerprintState instance like this:

@RequiresApi(api = Build.VERSION_CODES.M)
class AuthManagerProvider {
 companion object {
 fun provideAuthManager(context: Context, authenticationListener: AuthenticationListener): AuthManager {
 val fingerprintState = context.checkDeviceFingerprintState()
 return if (fingerprintState == FingerprintState.FINGERPRINT_ALLOW) {
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && context.isBiometricSupported()) {
 BiometricAuthManager(context).apply {
 } else {
 FingerprintAuthManager(context).apply {
 } else {
 throw AuthManagerProvideException(fingerprintState)

After AuthManager providing, you can use authenticate() method for authenticating user, listen AuthenticationListener and create your own realization for listener methods.

You can check the full code in  GitHub.

Please, rate my article. I did my best!

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.00 out of 5)

5 responses to “How to Use Android Fingerprint Security”

  1. Martin Smith says:


    Good to know about this info, this helps a lot to keep our data secure. Thanks for sharing the informative article…

  2. sowmyasri says:


    Great share, good to know about this …

  3. marlene mcquillen says:


    I don’t ordinarily comment but I gotta say regards for the post on this amazing one :D.

  4. shaynahale says:


    Hi, just wanted to tell you, I liked this article. It was practical.

    Keep on posting!

  5. pearline.degillern says:


    J’épluche tous des articles du site toujours super écrit ne changez

Leave a Reply