Determine if biometric hardware is present and the user has enrolled biometrics on Android P

前端 未结 8 1285
半阙折子戏
半阙折子戏 2020-12-13 05:50

I\'m asked to show certain UI elements depending on the presence of biometric hardware. For Android 23-27 I use FingerprintManager#isHardwareDetected() and

8条回答
  •  佛祖请我去吃肉
    2020-12-13 06:42

    In my biometrics, I used these and a few more checks to make sure that the device was capable and the fingerprint was enabled. In Kotlin I created an Object class and called it utilities.

    object BiometricUtilities {
        fun isBiometricPromptEnabled(): Boolean {
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
        }
        fun isSdkVersionSupported(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        }
        fun isHardwareSupported(context: Context): Boolean {
            val fingerprintManager = FingerprintManagerCompat.from(context)
            return fingerprintManager.isHardwareDetected
        }
        fun isFingerprintAvailable(context: Context): Boolean {
            val fingerprintManager = FingerprintManagerCompat.from(context)
            return fingerprintManager.hasEnrolledFingerprints()
        }
    }
    

    Then in my bioManager class, I placed the conditional statements under a function called authenticate that implements BiometricCallback

    fun authenticate(biometricCallback: BiometricCallback) {
    
        if (!BiometricUtilities.isHardwareSupported(context)) {
            biometricCallback.onBiometricAuthenticationNotSupported()
            return
        }
    
        if (!BiometricUtilities.isFingerprintAvailable(context)) {
            val intent = Intent(Settings.ACTION_SECURITY_SETTINGS)
               biometricCallback.onBiometricAuthenticationNotAvailable()
            return
        }
    
        displayBiometricDialog(biometricCallback)
    }
    

    This way you can check for availability of hardware and presence of fingerprint on the device and let the OS help you to display a prompt or not

提交回复
热议问题