天天看點

Android指紋識别,相容Android 6.0 - Android 11前言概覽詳細介紹實戰效果參考:

目錄

  • 前言
  • 概覽
    • 1.類
    • 2.權限
  • 詳細介紹
    • 1.`BiometricManager`屬性和方法
      • 是否可用的狀态碼
      • 方法:
    • 2.`BiometricPrompt`屬性和方法
      • 驗證的結果常用錯誤碼(不全):
      • 方法:
  • 實戰
    • 第一步:引入支援庫
    • 第二步:檢查指紋硬體是否可用
    • 第三步:開始驗證
    • 第四步:擷取驗證結果
  • 效果
  • 參考:

寫作不易,轉載請注明出處:

https://blog.csdn.net/qq_34676644/article/details/118758483

or

https://www.jianshu.com/p/530c9fa31786

前言

官方的指紋識别在Android 6.0引入,Android 6.0之前指紋識别由廠商自己定義。是以Android 6.0之前的指紋識别碎片化嚴重。

概覽

1.類

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.無法自定義對話款樣式

2.權限

  1. permission USE_FINGERPRINT

    :使用

    FingerprintManager

    FingerprintManagerCompat

    類進行指紋識别時,需擷取此權限。
    此權限為:Normal Permission
  2. 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_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)之前的版本中,當使用者使用裝置憑據進行身份驗證時調用此方法無效

實戰

第一步:引入支援庫

implementation "androidx.biometric:biometric:1.1.0"

第二步:檢查指紋硬體是否可用

/**
*傳回值見上文的“是否可用的狀态碼”
*/
public int isFingerprintAvailable(Context context){
	BiometricManager manager = BiometricManager.from(context);
	return manager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK);
}

           

第三步:開始驗證

/**
     * 開始驗證
     *
     * @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()
    }
           

第四步:擷取驗證結果

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("驗證失敗,請重試");
            }
        });

           

效果

屏上指紋 屏下指紋
Android指紋識别,相容Android 6.0 - Android 11前言概覽詳細介紹實戰效果參考:
Android指紋識别,相容Android 6.0 - Android 11前言概覽詳細介紹實戰效果參考:

參考:

[1] BiometricManager

[2] BiometricPrompt

[3] Android Google源生生物識别(Biometric依賴庫)

[4] Android指紋識别,相容6.0以上所有版本,包括9.0适配

[5] 指紋登入 - FingerprintManager