繁体   English   中英

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

[英]Check if devices has biometric enabled with androidx biometric prompt

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

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

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

AndroidX Biometric beta01 添加了BiometricManager.canAuthenticate(int) (以前称为BiometricManager.canAuthenticate()

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

implementation 'androidx.biometric:biometric:1.1.0'

然后,您可以执行以下操作来检查是否有任何生物特征可以在设备上使用。

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

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

FingerPrintManager有关于仅fingerpint认证数据,因此它具有hasEnrolledFringers() 但是BiometricPrompt用于面部解锁、指纹、虹膜。 这就像一个普通的经理类。 谷歌已经添加了支持 Android Q 的canAuthenticate 。但是你可以使用下面的 API 检查它

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
       val hasBiometricFeature :Boolean = context.packageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)

无论如何,谷歌也将其添加到 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"
 /**
   * 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
    }

使用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

这最近已添加到 beta01 或 beta02 中的 androidx one,我忘记了哪个

以下是 Kotlin 中截至今天的生物识别认证的最新实施:

第一步:在build.gradle添加如下依赖

implementation "androidx.biometric:biometric:1.1.0"

第二步:在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
    }

有关完整的生物识别身份验证实施,请参阅:

Android BiometricPrompt.Builder.authenticate() 未显示任何对话框

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM