使用androidx生物识别提示检查设备是否启用了生物识别

时间:2019-05-28 08:19:03

标签: android android-biometric-prompt

在Android BiometricPrompt中,提示已替换了已弃用的FingerprintManager。 FingerPrintManager具有两个功能hasEnrolledFingerprints()isHardwareDetected(),用于检查设备是否支持指纹以及用户是否注册了任何指纹认证。

对于新的BiometricPrompt,似乎没有任何功能可以在不尝试提示BiometricPrompt的情况下进行检查。有一个BiometricPrompt.AuthenticationCallback.onAuthenticationError(会被调用,并带有错误代码,指示该设备是否支持生物识别以及用户是否已注册生物识别。

因此,只有在尝试从用户进行身份验证时,我才能获得此信息。有没有一种方法可以在不尝试提示身份验证的情况下进行检查,以检查设备是否支持生物识别并且用户已注册生物识别?

5 个答案:

答案 0 :(得分:6)

AndroidX Biometric beta01添加了BiometricManager.canAuthenticate()

在应用模块的build.gradle文件中使用以下依赖项行。

path\to\your\chrome\installation\chrome.exe --allow-file-access-from-files

然后,您可以执行以下操作以检查是否可以在设备上使用任何生物识别技术。

implementation 'androidx.biometric:biometric:1.0.0-beta01'

在Android 6至9上,该功能仅支持指纹。 10或更高版本将支持任何生物特征识别(例如面部,虹膜)。

答案 1 :(得分:2)

如果使用的是compileSdkVersion 29和buildToolsVersion“ 29.0.1”。 您可以使用本机检查方法。

我为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
}

此外,您还必须在应用gradle文件上实现依赖:

implementation 'androidx.biometric:biometric:1.0.0-alpha04'

并使用最新的构建工具:

compileSdkVersion 29
buildToolsVersion "29.0.1"

答案 2 :(得分:1)

FingerPrintManager仅具有有关指纹验证的数据,因此具有hasEnrolledFringers()。但是BiometricPrompt用于面部解锁,精细打印,虹膜。这就像一个普通的经理班。 Google已添加canAuthenticate,它受Android Q支持。 但是您可以使用

检查它是否具有较低的API
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
       val hasBiometricFeature :Boolean = context.packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)

无论如何,Google还将其添加到androidx组件androidx.biometric:biometric

implementation 'androidx.biometric:biometric:1.0.0-alpha04'

使用权限

<uses-permission android:name="android.permission.USE_BIOMETRIC" />

在“ AuthenticationCallback”上

public void onAuthenticationError(int errorCode, CharSequence errString) {}

您可以使用这些代码检查错误代码

/**
 * The user does not have any biometrics enrolled.
 */
int BIOMETRIC_ERROR_NO_BIOMETRICS = 11;

答案 3 :(得分:0)

使用BiometricManager它具有方法

canAuthenticate()

它返回

BIOMETRIC_ERROR_NONE_ENROLLED if the user does not have any enrolled
BIOMETRIC_ERROR_HW_UNAVAILABLE if none are currently supported/enabled
BIOMETRIC_SUCCESS if a biometric can currently be used (enrolled and available)
BIOMETRIC_ERROR_NO_HARDWARE

查看官方文档https://developer.android.com/reference/android/hardware/biometrics/BiometricManager.html

答案 4 :(得分:0)

此功能最近已添加到beta01或beta02中的androidx版本中,我忘记了

相关问题