In Android BiometricPrompt prompt has replaced the deprecated FingerprintManager.
FingerPrintManager has two functions hasEnrolledFingerprints()
and isHardwareDetected()
to check if the device supports fingerprints and if the user has enrolled any fingerprint authentication.
With the new BiometricPrompt it seems there are no functions to check this without trying to prompt the BiometricPrompt. There is a BiometricPrompt.AuthenticationCallback.onAuthenticationError(
that is called with an error code indicating if the device supports biometric and if the user has biometric authentication enrolled.
So I can only get this information if I try to authenticate from the user. Is there a way to check without trying to prompt the authentication to check if the device supports biometrics and the user has enrolled them?
For testing such a scenario, open App Live and the Biometric Authentication Capability that can be set after uploading the APP to test and go to the settings before selecting the device and switching on the capability by toggling on in the menu.
So, if you're not sure how to do this, go into your settings and click on the biometrics or security section, as shown below on an Android device. From here, go to your fingerprint category, and you'll be shown all the fingerprints currently registered to your device.
public class BiometricPrompt. A class that manages a system-provided biometric prompt. On devices running Android 9.0 (API 28) and above, this will show a system-provided authentication prompt, using one of the device's supported biometric modalities (fingerprint, iris, face, etc).
To check whether biometric authentication is supported on a device, use BiometricsManager. canAuthenticate() . This call returns a success flag when the device has available biometric hardware, and the user has a biometric enrolled on the device.
AndroidX Biometric beta01 added BiometricManager.canAuthenticate(int)
(formerly BiometricManager.canAuthenticate()
)
Use the following dependency line in your app module's build.gradle file.
implementation 'androidx.biometric:biometric:1.1.0'
Then you can do the following to check if any biometrics are ready for use on the device.
BiometricManager.from(context).canAuthenticate(int) == BiometricManager.BIOMETRIC_SUCCESS
On Android 6 to 9 this only supports fingerprints. On 10 and above it will support any biometric (eg. face, iris).
If you are using compileSdkVersion 29 and buildToolsVersion "29.0.1". You can use a native check method.
I wrote this function for Kotlin:
fun checkForBiometrics() : Boolean {
Log.d(TAG, "checkForBiometrics started")
var canAuthenticate = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT < 29) {
val keyguardManager : KeyguardManager = applicationContext.getSystemService(KEYGUARD_SERVICE) as KeyguardManager
val packageManager : PackageManager = applicationContext.packageManager
if(!packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
Log.w(TAG, "checkForBiometrics, Fingerprint Sensor not supported")
canAuthenticate = false
}
if (!keyguardManager.isKeyguardSecure) {
Log.w(TAG, "checkForBiometrics, Lock screen security not enabled in Settings")
canAuthenticate = false
}
} else {
val biometricManager : BiometricManager = this.getSystemService(BiometricManager::class.java)
if(biometricManager.canAuthenticate() != BiometricManager.BIOMETRIC_SUCCESS){
Log.w(TAG, "checkForBiometrics, biometrics not supported")
canAuthenticate = false
}
}
}else{
canAuthenticate = false
}
Log.d(TAG, "checkForBiometrics ended, canAuthenticate=$canAuthenticate ")
return canAuthenticate
}
Additional, you have to implement on you app gradle file as dependecy:
implementation 'androidx.biometric:biometric:1.0.0-alpha04'
and also use the newest build tools:
compileSdkVersion 29
buildToolsVersion "29.0.1"
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