How to detect fingerprint hardware in android 11
Asked Answered
E

2

5

How to detect that the phone has fingerprint hardware or not. I want a code that detects the fingerprint hardware.

I used this code but this code is showing an error on "isHardwareDetected()" this method.

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 
    }
}

Edda answered 22/5, 2021 at 7:48 Comment(2)
FingerprintManager was deprecated in API level 28, use BiometricPrompt and BiometricManager insteadUnfeigned
or use AndroidX Biometric LibraryUnfeigned
E
5

I made a minor change in the question code and now it is working fine.

But that class "FingerprintManagerCompat" is deprecated

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val manager = FingerprintManagerCompat.from(this)
            if (!manager.isHardwareDetected) {
                Log.e("tag","Fingerprint hardware not detected.")
            } else if (!manager.hasEnrolledFingerprints()) {
                Log.e("tag","No fingerprint is set")
            } else {
                Log.e("tag","Fingerprint is set")
            }
        }
Edda answered 25/5, 2021 at 8:35 Comment(2)
This is useful but please mention the alternative for the deprecated class.Colotomy
Soon I'll update this answer with the alternative classesEdda
A
1

Add the following code inside AndroidManifest.xml :

<uses-feature android:name="android.hardware.fingerprint" android:required="true" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />

Use this where you require to detect the hardware:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
            fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
    
            if (!fingerprintManager.isHardwareDetected()) {
                Toast.makeText(getApplicationContext(), "Your device doesn't support fingerprint authentication", Toast.LENGTH_SHORT).show();
            }
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Please enable the fingerprint permission", Toast.LENGTH_SHORT).show();
    
        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.USE_FINGERPRINT}, FingerprintHandler.FINGERPRINT_PERMISSION);
    
        }
    
        if (!fingerprintManager.hasEnrolledFingerprints()) {
            Toast.makeText(getApplicationContext(), "Your Device has no registered Fingerprints! Please register atleast one in your Device settings", Toast.LENGTH_LONG).show();
        }
}
Alcalde answered 8/3, 2022 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.