[Android]指紋識別,兼容Android 6.0 - Android 11

寫作不易,轉載請注明出處:https://blog.csdn.net/qq_34676644/article/details/118758483
or
http://www.lxweimin.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:使用FingerprintManagerFingerprintManagerCompat類進行指紋識別時,需獲取此權限。

    此權限為: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_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)之前的版本中,當用戶使用設備憑據進行身份驗證時調用此方法無效

實戰

第一步:引入支持庫

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

效果

屏上指紋 屏下指紋
屏上指紋
屏下指紋

參考:

[1] BiometricManager
[2] BiometricPrompt
[3] Android Google源生生物識別(Biometric依賴庫)
[4] Android指紋識別,兼容6.0以上所有版本,包括9.0適配
[5] 指紋登錄 - FingerprintManager

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容