Created
May 15, 2021 18:14
-
-
Save Iltwats/3eb04f75f21b0c244025eab4cd23a27b to your computer and use it in GitHub Desktop.
Biometric Authentication Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void biometricPromptDialog() { | |
BiometricManager biometricManager = BiometricManager.from(this); | |
switch (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL)) { | |
case BiometricManager.BIOMETRIC_SUCCESS: | |
Log.d("MY_APP_TAG", "App can authenticate using biometrics."); | |
break; | |
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE: | |
Log.e("MY_APP_TAG", "No biometric features available on this device."); | |
b.biometricCardView.setVisibility(View.GONE); | |
break; | |
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE: | |
Log.e("MY_APP_TAG", "Biometric features are currently unavailable."); | |
b.biometricCardView.setVisibility(View.GONE); | |
break; | |
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED: | |
// Prompts the user to create credentials that your app accepts. | |
final Intent enrollIntent = new Intent(Settings.ACTION_BIOMETRIC_ENROLL); | |
enrollIntent.putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL); | |
startActivityForResult(enrollIntent, IntentIntegrator.REQUEST_CODE); | |
break; | |
} | |
Executor executor = ContextCompat.getMainExecutor(this); | |
BiometricPrompt biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() { | |
@Override | |
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { | |
super.onAuthenticationError(errorCode, errString); | |
viewEditAndSave(); | |
} | |
@Override | |
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) { | |
authComplete = true; | |
viewEditAndSave(); | |
super.onAuthenticationSucceeded(result); | |
} | |
@Override | |
public void onAuthenticationFailed() { | |
super.onAuthenticationFailed(); | |
} | |
}); | |
BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder() | |
.setTitle(getString(R.string.biometric_title)) | |
.setSubtitle(getString(R.string.biometric_subtitle)) | |
.setDescription(getString(R.string.biometric_desc)) | |
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK | BiometricManager.Authenticators.DEVICE_CREDENTIAL) | |
.setConfirmationRequired(false) | |
.build(); | |
if(biometricEnabled){ | |
b.biometricsSwitch.setChecked(true); | |
} | |
b.biometricsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if (isChecked) { | |
biometricEnabled = true; | |
} else { | |
biometricEnabled = false; | |
} | |
biometricPrompt.authenticate(promptInfo); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment