I\'m asked to show certain UI elements depending on the presence of biometric hardware. For Android 23-27 I use FingerprintManager#isHardwareDetected()
and
I wrote this method 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"
For those who do not want to wait for support library released, you can use nightly build like this
repositories {
maven {
url "https://ci.android.com/builds/submitted/5795878/androidx_snapshot/latest/repository/"
}
}
implementation group: 'androidx.biometric', name: 'biometric', version: '1.0.0-SNAPSHOT'
get build version from here
https://ci.android.com/builds/branches/aosp-androidx-master-dev/
branch aosp-androidx-master-dev
show latest build from androidx-snapshot