Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 544
Zohab Ali Avatar asked Mar 04 '23 21:03

Zohab Ali


2 Answers

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.

like image 146
Benjamin RD Avatar answered Apr 30 '23 12:04

Benjamin RD


o new androidX library it simple

BiometricManager.from(activity).canAuthenticate()

as documetation says

Returns BIOMETRIC_ERROR_NONE_ENROLLED if the user does not have any enrolled, or BIOMETRIC_ERROR_HW_UNAVAILABLE if none are currently supported/enabled. Returns BIOMETRIC_SUCCESS if a biometric can currently be used (enrolled and available). Value is BIOMETRIC_SUCCESS, BIOMETRIC_ERROR_HW_UNAVAILABLE, BIOMETRIC_ERROR_NONE_ENROLLED, or BIOMETRIC_ERROR_NO_HARDWARE

like image 35
Antonis Radz Avatar answered Apr 30 '23 13:04

Antonis Radz