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

前端 未结 8 1171
半阙折子戏
半阙折子戏 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:49

    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"
    
    0 讨论(0)
  • 2020-12-13 06:50

    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

    0 讨论(0)
提交回复
热议问题