美文网首页Android
Android 指纹识别

Android 指纹识别

作者: 程序员Android1 | 来源:发表于2021-08-23 18:45 被阅读0次

    和你一起终身学习,这里是程序员 Android

    经典好文推荐,通过阅读本文,您将收获以下知识点:

    一、指纹类概述
    二、指纹类权限
    三、指纹类详细介绍
    四、指纹使用实战

    官方的指纹识别在Android 6.0引入,Android 6.0之前指纹识别由厂商自己定义。所以Android 6.0之前的指纹识别碎片化严重。

    一、指纹类概述

    1.FingerprintManager:Android 6.0引入,Android 9.0 废弃。使用时需加入权限:permission USE_FINGERPRINT

    注:包含检查是否支持指纹,指纹比对是否成功等。支持包中的FingerprintManagerCompat类对其作了包装和兼容处理。不做详细介绍

    2.BiometricManager:Android 9.0引入。其不仅包含指纹识别,还包含人脸识别等其他验证方式。
    其主要功能为:检查当前设备是否具有指纹识别的条件,例如:设备是否具有指纹识别的硬件,指纹是否已经录入,硬件是否可用等。
    需要配合权限permission USE_BIOMETRIC使用

    注:目前,该类只有指纹识别的相关API,以后可能会加入人脸识别等其他生物识别的相关API。

    推荐使用AndroidX支持库中的类,其已经对Android 6.0-Android 11做了兼容性处理:

    • 在Android 9 版本和之前版本中会调用FingerprintManagerCompat
    • 在Android 10 版本开始会调用框架层BiometricManager

    3.BiometricPrompt:
    其主要功能为:发起指纹验证
    需要配合权限permission USE_BIOMETRIC使用

    4.BiometricPrompt.PromptInfo:
    其主要功能为:验证对话框

    通过其内部类BiometricPrompt.PromptInfo.Builder,采用创造者模式配置对话框。仅能配置少量参数,例如:标题,副标题等。这意味着:
    1.不分Android版本,对话框的样式统一
    2.无法自定义对话款样式

    二、指纹类权限

    1. permission USE_FINGERPRINT:使用FingerprintManagerFingerprintManagerCompat类进行指纹识别时,需获取此权限。

    此权限为:Normal Permission

    1. permission USE_BIOMETRIC:使用BiometricManager等类进行指纹识别时,需获取此权限。

      当使用AndroidX支持库中的BiometricManager时候不需要声明此权限,因为支持库中已经添加此权限

    三、指纹类详细介绍

    1.BiometricManager属性和方法

    是否可用的状态码

    属性 含义
    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_CREDENTIALBIOMETRIC_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);
    }
    
    

    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()
        }
    
    

    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
                public void onAuthenticationFailed() {
                    super.onAuthenticationFailed();
                    UIUtils.toast("验证失败,请重试");
                }
            });
    
    

    4.5 实现效果

    屏上指纹 屏下指纹

    参考链接:https://www.jianshu.com/p/530c9fa31786

    至此,本篇已结束。转载网络的文章,小编觉得很优秀,欢迎点击阅读原文,支持原创作者,如有侵权,恳请联系小编删除,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

    相关文章

      网友评论

        本文标题:Android 指纹识别

        本文链接:https://www.haomeiwen.com/subject/hfbliltx.html