gpt4 book ai didi

java - 如何使用 BiometricManager 或 BiometricPrompt 检查生物识别硬件是否可用?

转载 作者:行者123 更新时间:2023-11-30 12:03:31 32 4
gpt4 key购买 nike

我正在使用 BiometricPrompt 类实现生物识别身份验证。

  1. 如何在调用BiometricPrompt#authenticate 方法之前检查硬件是否可用?
  2. 如何查看已注册的生物特征?

如何调用BiometricManager#canAuthenticate方法?我无法在 kotlin 中为 BiometricManager 类创建对象

我当前的实现如下。

    val executor = Executors.newSingleThreadExecutor()

val biometricPrompt = BiometricPrompt(this, executor, object : BiometricPrompt.AuthenticationCallback() {

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d("BIOMETRIC", "$errString $errorCode")

if (BiometricPrompt.ERROR_HW_NOT_PRESENT == errorCode || BiometricPrompt.ERROR_NO_BIOMETRICS == errorCode)
PreferenceHandler.setBiometricAvailable(this@LockActivity, false)
else
PreferenceHandler.setBiometricAvailable(this@LockActivity, true)
}

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
}

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Log.d("BIOMETRIC", "FAILED")
}
})

val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("App title")
.setSubtitle("")
.setDescription("Identify yourself by Biometrics")
.setNegativeButtonText("Use Password")
.build()

biometricPrompt.authenticate(promptInfo)

最佳答案

问题终于解决了

BiometricManager androidx 库中缺少类

androidx.biometric:biometric:1.0.0-alpha04

更新库版本为

androidx.biometric:biometric:1.0.0-beta01

现在你可以导入androidx.biometric.BiometricManager

您可以检查生物识别硬件的可用性,并通过以下代码注册生物识别:

object BiometricUtil {

fun isHardwareAvailable(context: Context): Boolean{
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
val bm = BiometricManager.from(context)
val canAuthenticate = bm.canAuthenticate()
!(canAuthenticate == BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE || canAuthenticate == BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE)

} else {
false
}
}

fun hasBiometricEnrolled(context: Context): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
val bm = BiometricManager.from(context)
val canAuthenticate = bm.canAuthenticate()
(canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS)

} else {
false
}
}
}

关于java - 如何使用 BiometricManager 或 BiometricPrompt 检查生物识别硬件是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57727545/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com