在《BiometricPrompt之一 - 简单用法》介绍过BiometricPrompt的简单用法,但是这里是否存在bug?
是的,存在bug.
我们通过两个场景来测试了下。
1.搭载Android Q真机, 未录入指纹(不支持人脸),设置和不设置密码;
2.搭载Android Q Go真机,硬件上不支持指纹(不支持人脸),设置和不设置密码;
得到了如下log:
真机1。
04-07 10:47:51.247 18056 18056 D xts : onResume: SettingNotFoundException
04-07 10:47:52.960 18056 18056 D xts : click btn...
04-07 10:47:52.979 18056 18056 D TAG : onAuthenticationError: 11, str: No fingerprints enrolled.
04-07 10:48:15.028 18056 18056 D xts : onResume: i:0
04-07 10:48:16.559 18056 18056 D xts : click btn...
04-07 10:48:16.561 18056 18056 D TAG : showLockScreen: no in scrue.... no password
//按下Negative button
04-07 10:48:51.612 18056 18056 D xts : onResume: i:1
04-07 10:48:53.037 18056 18056 D xts : click btn...
04-07 10:48:55.712 18056 18056 D TAG : showLockScreen: negative btn clicked, do nothing
//按下HOME键
04-07 11:13:55.195 18056 18056 D TAG : onAuthenticationError: 5, str: Authentication cancelled
04-07 11:13:56.824 18056 18056 D knealq : onResume: i:1
04-07 11:14:03.138 18056 18056 D knealq : click btn...
//按下MENU键
04-07 11:14:04.766 18056 18056 D TAG : onAuthenticationError: 5, str: Authentication cancelled
//按下BACK键
04-07 11:14:09.980 18056 18056 D knealq : onResume: i:1
04-07 11:14:10.547 18056 18056 D knealq : click btn...
04-07 11:14:11.976 18056 18056 D TAG : onAuthenticationError: 10, str: Authentication cancelled
真机2。
01-08 11:06:43.638 15056 15056 D xts : onResume: SettingNotFoundException
01-08 11:06:49.280 15056 15056 D xts : click btn...
01-08 11:06:49.283 15056 15056 D TAG : showLockScreen: no in scrue.... no password
01-08 11:07:12.414 15056 15056 D xts : onResume: SettingNotFoundException
01-08 11:07:13.455 15056 15056 D xts : click btn...
01-08 11:07:13.475 15056 15056 D TAG : onAuthenticationError: 12, str: Biometric hardware unavailable
根据《BiometricPrompt之一 - 简单用法》设置BiometricPrompt的前提是,手机设置过锁屏,没设置锁屏(PIN/Pattern/Password)将不走BiometricPrompt逻辑。
[1]
另外,当硬件支持指纹时,却没有录入过任何可用于验证的指纹,BiometricPrompt.AuthenticationCallback将回调上报ErrorCode 11, 表示没有录入任何可用指纹。
当弹出BiometricDialogView时,分别按下HOME/ MENU/ BACK几个按键,得到的ErrorCode似乎也有不一,这些行为要想到。
[2]
再者,当硬件不支持指纹时,BiometricPrompt.AuthenticationCallback将回调上报ErrorCode 12, 表示硬件不支持指纹。
这两种情况,帖《BiometricPrompt之一 - 简单用法》不曾给出任何异常捕获。
这将直接导致,例如:点击button本应该启动BiometricDialogView,却没有任何动作,无响应。
[附加]
SDK定义ErrorCode hide API.
//home/user/Android/Sdk/sources/android-29/android/hardware/biometrics/BiometricFingerprintConstants.java
/**
* Interface containing all of the fingerprint-specific constants.
*
* NOTE: The error messages must be consistent between BiometricConstants, Biometric*Constants,
* and the frameworks/support/biometric/.../BiometricConstants files.
*
* @hide
*/
public interface BiometricFingerprintConstants {
/**
* The operation was canceled because the fingerprint sensor is unavailable. For example,
* this may happen when the user is switched, the device is locked or another pending operation
* prevents or disables it.
*/
public static final int FINGERPRINT_ERROR_CANCELED = 5;
/**
* The user canceled the operation. Upon receiving this, applications should use alternate
* authentication (e.g. a password). The application should also provide the means to return
* to fingerprint authentication, such as a "use fingerprint" button.
*/
public static final int FINGERPRINT_ERROR_USER_CANCELED = 10;
/**
* The user does not have any fingerprints enrolled.
*/
public static final int FINGERPRINT_ERROR_NO_FINGERPRINTS = 11;
/**
* The device does not have a fingerprint sensor.
*/
public static final int FINGERPRINT_ERROR_HW_NOT_PRESENT = 12;
一个好的程序,是需要考虑到这些异常的。