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

Ghayas
  • 1,266
  • 10
  • 17
  • 1
    `FingerprintManager ` was deprecated in API level 28, use `BiometricPrompt` and `BiometricManager` instead – atyc May 22 '21 at 08:51
  • or use [AndroidX Biometric Library](https://android-developers.googleblog.com/2019/10/one-biometric-api-over-all-android.html) – atyc May 22 '21 at 08:53

2 Answers2

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")
            }
        }
Ghayas
  • 1,266
  • 10
  • 17
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();
        }
}
Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
Fiza
  • 21
  • 4