How to check if device has fingerprint feature and then use it in Android

Zohab Ali picture Zohab Ali · Dec 14, 2018 · Viewed 7.9k times · Source

I am using this permission

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

to check if device has fingerprint or not. Thats how I check it in kotlin

val fingerSensorManager = FingerSensorManager(this)
fingerSensorManager.isFingerPrintAvailable()

It works fine but in Android 8 and above it throws exception.

How can I check fingerprint and also use it in all versions? I know there is another method for Android 8 and above but I am confuse that how to implement it so that it works in every device

Answer

MrMins picture MrMins · Dec 14, 2018

NEWEST FOR API 28

AndroidManifest.xml

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

Now, you're able to use:

val executor = activity.mainExecutor
val cancelListener = DialogInterface.OnClickListener { _, _ -> })
val biometricPrompt = BiometricPrompt.Builder(context)
  .setTitle("Title")
  .setSubtitle("Subtitle")
  .setDescription("Description")
  .setNegativeButton("Cancel", executor, cancelListener)
  .build()

THE NEXT CODE IS FOR VERSION LOWER THAN API 28

This class was deprecated in API level 28. See BiometricPrompt which shows a system-provided dialog upon starting authentication. In a world where devices may have different types of biometric authentication, it's much more realistic to have a system-provided authentication dialog since the method may vary by vendor/device.

I recommend you read this thread:

How to add fingerprint authentication to your Android app

Also, follow this other thread:

How to check device compatibility for finger print authentication in android

You have to add the dependency to your ´gradle´ file

compile "com.android.support:support-v4:23.0.0"

Basically, if I understood correctly your question, you want to know if the device has the fingerprint hardware and features. Then you might use:

// Check if we're running on Android 6.0 (M) or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    //Fingerprint API only available on from Android 6.0 (M)
    FingerprintManager fingerprintManager = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
    if (!fingerprintManager.isHardwareDetected()) { 
        // Device doesn't support fingerprint authentication     
    } else if (!fingerprintManager.hasEnrolledFingerprints()) { 
        // User hasn't enrolled any fingerprints to authenticate with 
    } else { 
        // Everything is ready for fingerprint authentication 
    }
}

Don't forget to add permission to access fingerprint functions in AndroidManifest. Since API 28:

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

Before API28:

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

Hope it help you.