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?
One method of protecting sensitive information or premium content within your app is to request biometric authentication, such as using face recognition or fingerprint recognition.
The Android framework includes support for face and fingerprint biometric authentication. Android can be customized to support other biometric modalities (such as Iris). However, biometric integration will depend on biometric security, not modality.
Google finally solved this problem with Android Q
The android.hardware.biometrics.BiometricManager#canAuthenticate() method can be used to determine if biometrics can be used.
The method can be used to determine if biometric hardware is present and if the user is enrolled or not.
Returns BIOMETRIC_ERROR_NONE_ENROLLED if the user does not have any enrolled, or BIOMETRIC_ERROR_HW_UNAVAILABLE if none are currently supported/enabled. Returns BIOMETRIC_SUCCESS if a biometric can currently be used (enrolled and available).
Hopefully this is added to the androidx.biometric:biometric
library, so it can be used on all devices.
Until then the solution by @algrid works to determine biometrics enrollment.
And the following can be used to determine, if a fingerprint reader is present.
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With