我正在使用生物特征认证对话框,但是我的cryptoObject始终为null。
我有一个 fragment ,但我也直接从 Activity 中尝试过。

这是我的代码,

私有(private)处理程序biometricPromptHandler = new Handler();

private Executor executor = command -> biometricPromptHandler.post(command);
private void showBiometricPrompt( String title, String description,
                                 BiometricsCompatCallback compatCallback) {

    BiometricPrompt.PromptInfo promptInfo =
            new BiometricPrompt.PromptInfo.Builder()
                    .setTitle(title)
                    .setSubtitle(description)
                    .setNegativeButtonText("Cancel")
                    .build();

    BiometricPrompt biometricPrompt = new BiometricPrompt((FragmentActivity) context,
            executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode,
                                          @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            compatCallback.onAuthenticationError(errorCode, errString);
            Log.d("onAuthenticationError", ": ");
        }

        @Override
        public void onAuthenticationSucceeded(
                @NonNull BiometricPrompt.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            Log.d("result", ": "+(result.getCryptoObject()));
            BiometricPrompt.CryptoObject authenticatedCryptoObject =
                    result.getCryptoObject();
            Log.d("onAuthentionSucceeded", ": "+(authenticatedCryptoObject==null));
            if (authenticatedCryptoObject != null) {
                 cipher = authenticatedCryptoObject.getCipher();
                Log.d("onAuthentionSucceeded", ": ");
                compatCallback.onAuthenticationSuccessful(cipher);
            }else {
                Log.d("cipher", "onAuthenticationSucceeded: ");
            }

        }

        @Override
        public void onAuthenticationFailed() {
            Log.d("onAuthenticationFailed", ": ");
            super.onAuthenticationFailed();
            compatCallback.onAuthenticationFailed();
        }
    });


        biometricPrompt.authenticate(promptInfo);



}

有人知道我在做什么错吗?

最佳答案

要从CryptoObject获取AuthenticationResult,您必须在调用CryptoObject时首先传递authenticate(),如下所示:

// this example uses a Cipher but your code can use signature or mac
biometricPrompt.authenticate(promptInfo, BiometricPrompt.CryptoObject(cipher))

如果在调用authenticate()时未传递CryptoObject,则该API没有返回给您的API。

checkout this blog post

09-25 20:25