当前位置:   article > 正文

Android 指纹识别

Android 指纹识别

| BIOMETRIC_ERROR_HW_UNAVAILABLE(value:1) | The user can’t authenticate because the hardware is unavailable. Try again later (传感器当前不可用,清稍后再试) |

| BIOMETRIC_ERROR_NONE_ENROLLED(value:11) | The user can’t authenticate because no biometric or device credential is enrolled.(信息没有录入,比如还没录入指纹) |

| BIOMETRIC_ERROR_NO_HARDWARE(value:12) | The user can’t authenticate because there is no suitable hardware (e.g. no biometric sensor or no keyguard).(没有合适的传感器或者没设置密码,例如手机没有指纹传感器) |

| BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED(value:15) | The user can’t authenticate because a security vulnerability has been discovered with one or more hardware sensors. The affected sensor(s) are unavailable until a security update has addressed the issue.(传感器存在已知的漏洞,在更新修复漏洞前,传感器不可用) |

| BIOMETRIC_ERROR_UNSUPPORTED(value:-2) | The user can’t authenticate because the specified options are incompatible with the current Android version.(设置的一些验证条件,当前手机的Android版本无法满足) |

| BIOMETRIC_STATUS_UNKNOWN(value:-1) | Unable to determine whether the user can authenticate(不知道是否可以进行验证。通常在旧版本的Android手机上出现,当出现这个错误是,仍然可以尝试进行验证) |

| BIOMETRIC_SUCCESS(value:0) | The user can successfully authenticate.(可以进行验证) |

  • 方法:

| 方法名 | 作用 | 返回值 |

| — | — | — |

| canAuthenticate()(已废弃)推荐使用canAuthenticate(int) | 检查传感器是否可用。 | 是否可用的状态码 |

| canAuthenticate (int authenticators) | 检查传感器是否可用。 | 是否可用的状态码 |

| from(Context context)(静态方法) | 创建BiometricManager实例 | BiometricManager实例 |

canAuthenticate (int authenticators)中 authenticators取值为:

  • BIOMETRIC_STRONG:满足第三类要求的生物识别传感器

  • BIOMETRIC_WEAK:满足第二类要求的生物识别传感器

  • DEVICE_CREDENTIAL:满足安全设备的要求 (PIN, pattern, or password)

一般来说级别越高,安全性越高。详情:声明您的应用支持的身份验证类型

一般采用:BIOMETRIC_WEAK

注意:Android 10(API 级别 29)及更低版本不支持以下身份验证器类型组合:DEVICE_CREDENTIAL 和 BIOMETRIC_STRONG | DEVICE_CREDENTIAL。如需检查 Android 10 及更低版本中是否存在 PIN 码、解锁图案或密码,请使用 KeyguardManager.isDeviceSecure()方法

2.BiometricPrompt属性和方法

验证的结果常用错误码(不全):

| 常用错误码 | 描述 |

| — | — |

| ERROR_CANCELED( Value:5) | 取消验证 |

| ERROR_HW_UNAVAILABLE(value:1) | 目前不可用,稍后再试 |

| ERROR_LOCKOUT(value:7) | 验证失败了5次,等到30秒后再试 |

| ERROR_LOCKOUT_PERMANENT(value:9) | 触发了ERROR_LOCKOUT太多次,生物验证锁定,在使用设备验证(例如:密码,图案)解锁前,不能再使用生物验证 |

| ERROR_NEGATIVE_BUTTON(value:13) | 点击了negative button |

| ERROR_NO_SPACE(value:4) | 设备可用存储空间不足 |

| ERROR_TIMEOUT(value:3) | 验证超时。超时时间与设备和传感器类型有关 |

| ERROR_USER_CANCELED(value:10) | 用户取消了验证 |

  • 方法:

| 方法名 | 功能 |

| — | — |

| authenticate (BiometricPrompt.PromptInfo info, BiometricPrompt.CryptoObject crypto) | 展示验证对话框,调用基于加密的身份验证。ps:与第二类生物验证和Android 11之前的设备验证不兼容 |

| authenticate (BiometricPrompt.PromptInfo info) | 展示验证对话框,调用身份验证 |

| cancelAuthentication () | 取消身份验证,隐藏验证对话框。ps:在Android 10(API 29)之前的版本中,当用户使用设备凭据进行身份验证时调用此方法无效 |

四、指纹使用实战


4.1 第一步:引入支持库

implementation "androidx.biometric:biometric:1.1.0"

4.2 第二步:检查指纹硬件是否可用


/**

*返回值见上文的“是否可用的状态码”

*/

public int isFingerprintAvailable(Context context){

    BiometricManager manager = BiometricManager.from(context);

    return manager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK);

}





  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4.3 第三步:开始验证


/**

     * 开始验证

     *

     * @param activity

     * @param callBack 验证结果回调

     */

    public void authenticate(FragmentActivity activity, BiometricPrompt.AuthenticationCallback callBack) {

        BiometricPrompt.PromptInfo promptInfo = createUi();

        BiometricPrompt prompt = new BiometricPrompt(activity, ContextCompat.getMainExecutor(activity), callBack);

        prompt.authenticate(promptInfo);

    }



    private BiometricPrompt.PromptInfo createUi() {

        return new BiometricPrompt.PromptInfo.Builder()

                .setTitle("Register Fingerprint")

                .setSubtitle("Pls Touch the sensor")

                .setNegativeButtonText("Use App Password")

                .build()

    }





  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

4.4 第四步:获取验证结果


authenticate(mView, new BiometricPrompt.AuthenticationCallback() {



            /**

             * 验证过程中发生了错误

             * @param errorCode

             * @param errString

             */

            @Override

            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {

                switch (errorCode) {

                    case ERROR_USER_CANCELED:

                        UIUtils.toast("取消了指纹识别");

                        break;

                    case ERROR_LOCKOUT:

                        UIUtils.toast("失败5次,已锁定,请30秒后在试");

                        break;

                    case ERROR_LOCKOUT_PERMANENT:

                        UIUtils.toast("失败次数太多,指纹验证已锁定,请改用密码,图案等方式解锁");

                    case ERROR_NEGATIVE_BUTTON:

                        UIUtils.toast("点击了negative button");

                        break;

                    case ERROR_NO_DEVICE_CREDENTIAL:

                        UIUtils.toast("尚未设置密码,图案等解锁方式");

                        break;

                    case ERROR_NO_SPACE:

                        UIUtils.toast("可用空间不足");

                        break;

                    case ERROR_TIMEOUT:

                        UIUtils.toast("验证超时");

                        break;

                }

            }



            @Override

            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {

                UIUtils.toast("验证成功");



            }



            /**

             * 验证失败

             * @param

             */

            @Override
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/749663
推荐阅读
相关标签
  

闽ICP备14008679号