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

Check if devices has biometric enabled with androidx biometric prompt

在AndroidBiometricPrompt prompt has replaced the deprecated FingerprintManager。 FingerPrintManager有两个函数hasEnrolledFingerprints()isHardwareDetected()来检查设备是否支持指纹以及用户是否注册了任何指纹认证。

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

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

使用BiometricManager它有一个方法

canAuthenticate()

它returns

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

FingerPrintManager只有指纹认证相关的数据,所以有hasEnrolledFringers()。但是 BiometricPrompt 用于面部解锁、finerprint、虹膜。就像一个普通的经理class。 Google 添加了 canAuthenticate 支持来自 Android 问。 但是您可以使用

检查它是否较低 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;

如果您使用的是 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"

AndroidX Biometric beta01 added BiometricManager.canAuthenticate(int) (formerly BiometricManager.canAuthenticate())

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

implementation 'androidx.biometric:biometric:1.1.0'

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

BiometricManager.from(context).canAuthenticate(int) == BiometricManager.BIOMETRIC_SUCCESS

在Android 6 到9 这只支持指纹。在 10 及更高版本上,它将支持任何生物识别(例如面部、虹膜)。

以下是截至今天在 Kotlin 中使用生物识别身份验证的最新实现:

第 1 步:在 build.gradle

中添加以下依赖项
implementation "androidx.biometric:biometric:1.1.0"

第 2 步:在 AndroidManifest.xml

中添加以下权限
<uses-permission android:name="android.permission.USE_BIOMETRIC" />

第 3 步:添加以下方法来检查是否启用了生物识别:

    /**
     * To check if the devices supports biometric authentication
     */
    fun isBioMetricEnabled(ctx: Context) : Boolean    {
        val biometricManager = BiometricManager.from(ctx)
        return biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK) ==
                BiometricManager.BIOMETRIC_SUCCESS
    }

有关完整的生物特征认证实施,请参阅:

 /**
   * Check For Biometrics Support
   * --> Fingerprint don't support in this device
   * --> Fingerprint not enable in this device
  */

    fun checkForBiometricsSupport(context: Context): Boolean {
        val status = BiometricManager.from(context).canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK)
        return status == BiometricManager.BIOMETRIC_SUCCESS
    }