How to check device compatibility for finger print authentication in android

核能气质少年 提交于 2019-11-27 18:30:18

You have to use method isHardwareDetected on FingerprintManager class.

Determine if fingerprint hardware is present and functional. Returns true if hardware is present and functional, false otherwise.

// 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" />

With Support Library

If you don't want to check Build.VERSION, it's also possible to check on device lower than Android 6.0 with Support Library

Add dependency:

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

And use FingerprintManagerCompat class as this:

FingerprintManagerCompat fingerprintManagerCompat = FingerprintManagerCompat.from(context);

if (!fingerprintManagerCompat.isHardwareDetected()) { 
    // Device doesn't support fingerprint authentication     
} else if (!fingerprintManagerCompat.hasEnrolledFingerprints()) { 
    // User hasn't enrolled any fingerprints to authenticate with 
} else { 
    // Everything is ready for fingerprint authentication 
}

Just a follow up to LaurentY's answer. You need a permission to access the finger print scanner. And for that you need to add this to your Manifest.

API 27 and before

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

API 28 and later

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

This method Works for all Android versions and also check for permission

 private boolean isSensorAvialable() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return ActivityCompat.checkSelfPermission(AppContext, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED &&
                        AppContext.getSystemService(FingerprintManager.class).isHardwareDetected();
            } else {
                return FingerprintManagerCompat.from(AppContext).isHardwareDetected();
            }
        }

implementing BiometricPrompt no permission is necessary:

    FingerprintManagerCompat fingerprintManagerCompat = FingerprintManagerCompat.from (context);

if (! fingerprintManagerCompat.isHardwareDetected ()) {
     // Device doesn't support fingerprint authentication
} else if (! fingerprintManagerCompat.hasEnrolledFingerprints ()) {
     // User hasn't enrolled any fingerprints to authenticate with
} else {
     // Everything is ready for fingerprint authentication
}
    [BiometricPrompt][1]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!