Question: I'm asked to show certain UI elements depending on the presence of biometric hardware. For Android 23-27 I use `FingerprintManager#isHardwareDetected()` and `FingerprintManager#hasEnrolledFingerprints()`. Both of which are deprecated in Android 28.

I understand that I can get this information by using `BiometricPrompt#authenticate(...)` and receiving either `BiometricPrompt#BIOMETRIC_ERROR_HW_NOT_PRESENT` or `BiometricPrompt#BIOMETRIC_ERROR_NO_BIOMETRICS` in the `BiometricPrompt.AuthenticationCallback#onAuthenticationError(int errorCode, ...)` method. But this would lead to the `BiometricPrompt` being shown on supporting devices, which is undesirable. Using the `CancellationSignal` doesn't seem to be a solution either, since I wouldn't know when to cancel the prompt.

Is there any way to detect biometric hardware presence and user enrolment?

Answer: 
I wrote this method for Kotlin:

```
fun checkForBiometrics() : Boolean{
    Log.d(TAG, "checkForBiometrics started")
    var canAuthenticate = true
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (Build.VERSION.SDK_INT < 29) {
            val keyguardManager : KeyguardManager = applicationContext.getSystemService(KEYGUARD_SERVICE) as KeyguardManager
            val packageManager : PackageManager   = applicationContext.packageManager
            if(!packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
                Log.w(TAG, "checkForBiometrics, Fingerprint Sensor not supported")
                canAuthenticate = false
            }
            if (!keyguardManager.isKeyguardSecure) {
                Log.w(TAG, "checkForBiometrics, Lock screen security not enabled in Settings")
                canAuthenticate = false
            }
        } else {
            val biometricManager : BiometricManager = this.getSystemService(BiometricManager::class.java)
            if(biometricManager.canAuthenticate() != BiometricManager.BIOMETRIC_SUCCESS){
                Log.w(TAG, "checkForBiometrics, biometrics not supported")
                canAuthenticate = false
            }
        }
    }else{
        canAuthenticate = false
    }
    Log.d(TAG, "checkForBiometrics ended, canAuthenticate=$canAuthenticate ")
    return canAuthenticate
}

```

Additional, you have to implement on you app gradle file as dependecy:

```
implementation 'androidx.biometric:biometric:1.0.0-alpha04'

```

and also use the newest build tools:

```
compileSdkVersion 29
buildToolsVersion "29.0.1"

```
The method - checks that the user has biometric authentication permission enabled for the app before using the package manager to verify that fingerprint authentication is available on the device. And even it will check if the user is enrolled or not. 

implementation 'androidx.biometric:biometric:1.0.0-alpha03'

```
private Boolean checkBiometricSupport() {

    KeyguardManager keyguardManager =
            (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

    PackageManager packageManager = this.getPackageManager();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        notifyUser("This Android version does not support fingerprint authentication.");
        return false;
    }

    if(!packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT))
    {
        notifyUser("Fingerprint Sensor not supported");
        return false;
    }

    if (!keyguardManager.isKeyguardSecure()) {
        notifyUser("Lock screen security not enabled in Settings");

        return false;
    }

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.USE_BIOMETRIC) !=
            PackageManager.PERMISSION_GRANTED) {
        notifyUser("Fingerprint authentication permission not enabled");

        return false;
    }

    return true;
}

```