2

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)
AvisSiva
  • 707
  • 1
  • 9
  • 17
  • _"I can't create object for BiometricManager class in kotlin"_ You're not supposed to create an instance. You should call `getSystemService(Context.BIOMETRIC_SERVICE)` – Michael Aug 30 '19 at 13:28
  • @Michael getSystemService(Context.BIOMETRIC_SERVICE) also showing error 'WrongConstant' – AvisSiva Aug 30 '19 at 13:44
  • It's available on API level 29. If you're using the androidX support library to support older API levels then use `BiometricManager.from(someContext)` instead. – Michael Aug 30 '19 at 13:49
  • Yes I was used androidx support library `androidx.biometric:biometric:1.0.0-alpha04` in that library, I can't find the `BiometricManager`. I think bug with that version. today they released `androidx.biometric:biometric:1.0.0-beta01`. Now I can import `androidx.biometric.BiometricManager`. – AvisSiva Aug 30 '19 at 17:04
  • Use [BiometricManager.canAuthenticate](https://developer.android.com/reference/androidx/biometric/BiometricManager.html#canAuthenticate()) – Gabriele Mariotti Aug 31 '19 at 10:52
  • @Gabriele yes got it already bro – AvisSiva Aug 31 '19 at 13:45

1 Answers1

4

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
    }
}
}
AvisSiva
  • 707
  • 1
  • 9
  • 17
  • 2
    There should be no need to use `FingerprintManagerCompat` for lower API levels. `BiometricManager` does that internally for you (at least the androidX support library version). – Michael Aug 30 '19 at 17:50