androidx.biometricの生体認証ライブラリ、以下2つのクラス

・BiometricManagerクラス

・BiometricPromptクラス

を利用して生体認証ログイン機能を実装します。

 

画面遷移イメージは以下です。

 

この機能はパスワードメモアプリのバージョン1.4で搭載していますので是非アプリでも動作を確認して見て下さい。

1.起動時に生体認証機能が実行できるか確認

onCreate時に生体認証用のButtonを登録し、BiometricManagerクラスを利用して生体認証機能が実行できるか確認します。

 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Button biometricloginbtn = (Button) findViewById(R.id.biometricLoginButton);
    biometricloginbtn.setOnClickListener(this);

    CheckBiometricSetting();
}

private void CheckBiometricSetting() {
    BiometricManager biometricManager = BiometricManager.from(this);
    switch (biometricManager.canAuthenticate()) {
        case BiometricManager.BIOMETRIC_SUCCESS:
            findViewById(R.id.biometricLoginButton).setVisibility(View.VISIBLE);
            findViewById(R.id.biometricLoginButton).setEnabled(true);
            break;
        case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
            findViewById(R.id.biometricLoginButton).setVisibility(View.INVISIBLE);
            break;
        case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
        case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
            findViewById(R.id.biometricLoginButton).setVisibility(View.VISIBLE);
            findViewById(R.id.biometricLoginButton).setEnabled(false);
            break;
    }
}

BIOMETRIC_SUCCESSの場合はボタンを有効に。

BIOMETROC_NO_HARDWAREの場合は生体認証機能が無いのでボタンを非表示に。

その他の場合は、指紋認証が登録されてない等、生体認証機能はあるが有効ではない状態なのでボタンを非活性にしています。

 

2.生体認証ダイアログを表示する

HandlerとExecutorをActivityクラスのメンバに定義し、onClickメソッドを以下のように実装する。


private Handler handler = new Handler();
private Executor executor = new Executor() {
    @Override
    public void execute(Runnable command) {
        handler.post(command);
    }
};

public void onClick(View v) {
    BiometricPrompt.PromptInfo promptInfo =
            new BiometricPrompt.PromptInfo.Builder()
                    .setTitle("生体認証ログイン")
                    .setSubtitle("生体認証情報を利用してログインします")
                    .setNegativeButtonText("取消")
                    .build();
    BiometricPrompt biometricPrompt = new BiometricPrompt(LoginActivity.this,
            executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            Toast.makeText(getApplicationContext(),
                    "認証エラー: " + errString, Toast.LENGTH_SHORT)
                    .show();
        }
        @Override
        public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            BiometricPrompt.CryptoObject authenticatedCryptoObject =
                    result.getCryptoObject();
            Intent intent = new Intent(LoginActivity.this, PasswordListActivity.class);
            startActivity(intent);
        }
        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Toast.makeText(getApplicationContext(), "認証失敗",
                    Toast.LENGTH_SHORT)
                    .show();
        }
    });
}

onAuthenticationSucceededのメソッドが呼ばれる場合が認証成功なので、次のActivityへの遷移となるIntentを生成しています。

以上で生体認証ライブラリを利用したログイン機能となります。