如何在不打开授权菜单的情况下检查生物传感器的状态



我需要在生物识别被提出之前找出它是否被阻止。在android文档中有一个方法biometricManager.canAuthenticate();

它返回:

BIOMETRIC_ERROR_NO_HARDWARE
BIOMETRIC_ERROR_HW_UNAVAILABLE
BIOMETRIC_ERROR_NONE_ENROLLED
BIOMETRIC_ERROR_HW_UNAVAILABLE
BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED
BIOMETRIC_SUCCESS

没有关于传感器锁定的信息,但是如果传感器突然被锁定,那么当试图提高生物识别-biometricPrompt.authenticate(...)时,我们将得到ERROR_LOCKOUTERROR_LOCKOUT_PERMANENT错误

问题-是否有办法检查传感器的状态之前,生物识别提出?

可能的解决方案如下:

override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
//Sensor locked for 30 seconds
//Store timestamp when lockuot happens and check for 30 seconds timeout
BiometricCodes.BIOMETRIC_ERROR_LOCKOUT -> {
BiometricStorage.storeLockoutTs() 
}
//Sensor locked permanently
//Device lock/unlock OR device restart required
BiometricCodes.BIOMETRIC_ERROR_LOCKOUT_PERMANENT -> {
BiometricStorage.setBiometricSensorPermanentlyLocked()
}
}
}

此外,您需要为android.intent.action.BOOT_COMPLETED和/或android.intent.action.USER_PRESENT注册BroadcastReceiver。

当设备启动或解锁时,该接收器应该捕获事件,您必须重置BiometricSensorPermanentlyLocked状态。

请看下面的例子:https://github.com/Salat-Cx65/AdvancedBiometricPromptCompat/blob/main/biometric/src/main/java/dev/skomlach/biometric/compat/utils/DeviceUnlockedReceiver.kt

相关内容

最新更新