javaandroidkotlinandroid-fingerprint-apiandroid-biometric-prompt

How to check biometric hardware is available using BiometricManager or BiometricPrompt?


I am implementing Biometric authentication using BiometricPrompt class.

  1. how to check is hardware is available or not before calling BiometricPrompt#authenticate method?
  2. how to check has enrolled biometric?

How to call BiometricManager#canAuthenticate method? I can't create object for BiometricManager class in kotlin

My current implementation follows.

    val executor = Executors.newSingleThreadExecutor()

    val biometricPrompt = BiometricPrompt(this, executor, object : BiometricPrompt.AuthenticationCallback() {

        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            Log.d("BIOMETRIC", "$errString $errorCode")

            if (BiometricPrompt.ERROR_HW_NOT_PRESENT == errorCode || BiometricPrompt.ERROR_NO_BIOMETRICS == errorCode)
                PreferenceHandler.setBiometricAvailable(this@LockActivity, false)
            else
                PreferenceHandler.setBiometricAvailable(this@LockActivity, true)
        }

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            Log.d("BIOMETRIC", "FAILED")
        }
    })

    val promptInfo = BiometricPrompt.PromptInfo.Builder()
            .setTitle("App title")
            .setSubtitle("")
            .setDescription("Identify yourself by Biometrics")
            .setNegativeButtonText("Use Password")
            .build()

    biometricPrompt.authenticate(promptInfo)

Solution

  • Finally issue fixed

    BiometricManager class was missed in the androidx library

    androidx.biometric:biometric:1.0.0-alpha04
    

    Update the library version to

    androidx.biometric:biometric:1.0.0-beta01
    

    Now you can import androidx.biometric.BiometricManager

    You can check biometric hardware availability and has biometric enrolled by following code:

    object BiometricUtil {
    
    fun isHardwareAvailable(context: Context): Boolean{
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            val bm = BiometricManager.from(context)
            val canAuthenticate = bm.canAuthenticate()
            !(canAuthenticate == BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE || canAuthenticate == BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE)
    
        } else {
            false
        }
    }
    
    fun hasBiometricEnrolled(context: Context): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            val bm = BiometricManager.from(context)
            val canAuthenticate = bm.canAuthenticate()
            (canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS)
    
        } else {
            false
        }
    }
    }