I am currently using the biometric manager and biometric prompt to create a fingerprint authentication app on a google pixel 2. The code below is for that app. However, i was wondering if i changed to developing on a google pixel 4 instead could i still use the biometric manager / biometric prompt to do facial recognition for authentication instead of a fingerprint? The same premise in which the registered face to the device could be the only face to login. Any help or explanation would be great thanks!
// Building the fingerprint scanner
final Executor executor = Executors.newSingleThreadExecutor();
final BiometricPrompt biometricPrompt = new BiometricPrompt.Builder(this)
.setTitle("Biosecure")
.setSubtitle("Fingerprint Authentication")
.setDescription("Please scan your fingerprint")
.setNegativeButton("cancel", executor, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).build();
// Scanning fingerprint
btnScanFinger.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
biometricPrompt.authenticate(new CancellationSignal(), executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
@Override
public void onAuthenticationHelp(int helpCode, final CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
}
@Override
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
activity.runOnUiThread(new Runnable() {
@Override
public void run()
{
Toast.makeText(Login_Biometrics.this, "Fingerprint authenticated successfully", Toast.LENGTH_LONG).show();
fingerprintAuth = true;
please.setText("Please input your pin");
fingerprint.setVisibility(View.INVISIBLE);
btnScanFinger.setVisibility(View.INVISIBLE);
PinInput.setVisibility(View.VISIBLE);
}
});
}
Solution:
Use the same code altered for facial recognition with a device that uses face authentication to unlock . I used a Google Pixel 4. Remember to add permissions for using biometrics.
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
// Building the fingerprint scanner
final Executor executor = Executors.newSingleThreadExecutor();
final BiometricPrompt biometricPrompt = new BiometricPrompt.Builder(this)
.setTitle("Biosecure")
.setSubtitle("Face Authentication")
.setDescription("Please scan your face")
.setNegativeButton("cancel", executor, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).build();
btnScanFinger.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
biometricPrompt.authenticate(new CancellationSignal(), executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
@Override
public void onAuthenticationHelp(int helpCode, final CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
}
@Override
public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
activity.runOnUiThread(new Runnable() {
@Override
public void run()
{
Toast.makeText(MainActivity.this, "Face scanned succesfully", Toast.LENGTH_LONG).show();
fingerprintAuth = true;
}
});
}