检查设备是否已启用 AndroidX 生物识别提示功能。

8
在 Android 中,BiometricPrompt 取代了已弃用的FingerprintManager。 FingerPrintManager 有两个函数 hasEnrolledFingerprints()isHardwareDetected() 用于检查设备是否支持指纹和用户是否已经注册了指纹认证。
使用新的 BiometricPrompt ,似乎没有直接获取此信息的函数,唯一的方法是尝试通过 BiometricPrompt 进行身份验证。BiometricPrompt.AuthenticationCallback.onAuthenticationError( 会被调用,其中包含一个错误代码,指示设备是否支持生物识别以及用户是否已经注册生物识别认证。
因此,只能在尝试进行身份验证时,才可以获得此信息。有没有一种方式可以在不尝试提示进行身份验证的情况下检查设备是否支持生物识别技术并且用户已注册?

这是最近添加到androidx中的一个beta01或beta02版本,我忘记了。 - nAndroid
6个回答

11

AndroidX生物识别 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及以上版本中,它将支持任何生物识别技术(例如面部、虹膜)。


2
canAuthenticate()已被弃用,请使用canAuthenticate(int)代替。 - Ganesh MB

3
如果您使用 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

FingerPrintManager 只涉及指纹验证数据,因此它有 hasEnrolledFringers()。但是 BiometricPrompt 用于面部解锁、指纹和虹膜识别。它就像一个通用的管理类。 Google 已经在 Android Q 中添加了 canAuthenticate 支持。 但是您可以使用以下方法检查低版本 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

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

on `AuthenticationCallback'

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

you can check the error codes with the ones

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

2
我使用 androidx.biometric:biometric:1.0.0-alpha04,但似乎没有提前检查的方式。 - Stephan
是的,这个方法可以检查设备上是否有指纹识别器,但无法检查用户是否已经注册了指纹。 - Stephan
@EarlOfEgo 如果至少注册了一个指纹,此提示将显示。 - Manoj Perumarath
是的,那是正确的,但我的问题是在不显示提示的情况下检查它。 - Stephan
@EarlOfEgo 你可以使用回调函数,其中包含错误类型,就这些,参见更新的答案。 - Manoj Perumarath

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
    }

0
以下是截至今天的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
    }

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

{{link1:Android BiometricPrompt.Builder.authenticate()未显示任何对话框}}


0

2
这个功能是在Android Q中添加的,有没有办法在早期版本中检查,最好使用androidx版本? - Stephan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接