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?
I wrote this method for Kotlin:
Additional, you have to implement on you app gradle file as dependecy:
and also use the newest build tools:
AndroidX biometric library started providing this kind of information from version
1.0.0-beta01
(androidx.biometric:biometric:1.0.0-beta01
)Which returns one of
See changelog: https://developer.android.com/jetpack/androidx/releases/biometric#1.0.0-beta01
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.
Then in my bioManager class, I placed the conditional statements under a function called authenticate that implements 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
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'
For those who do not want to wait for support library released, you can use nightly build like this
get build version from here
branch aosp-androidx-master-dev
show latest build from androidx-snapshot
Sadly Google wouldn't solve this problem having changed the status of related issue to "Won't Fix (Intended behavior)". I prefer to use the old deprecated API for now.
But for those who want to use the newer API there's a hacky/ugly way to get a
hasEnrolledFingerprints()
analog (the code is for API23+):This is based on the following Android keystore docs statement:
See the "Require user authentication for key use" section here https://developer.android.com/training/articles/keystore