Class Biometrics
Entry point for biometric authentication (Touch ID, Face ID, fingerprint,
Android BiometricPrompt). Obtain the platform implementation via
getInstance(); the returned subclass is owned by the active port.
A typical unlock flow:
Biometrics b = Biometrics.getInstance();
if (!b.canAuthenticate()) {
// Fall back to password
return;
}
b.authenticate("Unlock your account").onResult((success, err) -> {
if (err != null) {
BiometricError code = ((BiometricException) err).getError();
// branch on code
} else {
// success
}
});
authenticate(AuthenticationOptions) returns an AsyncResource whose
failure path completes with a BiometricException so callers can branch
on the typed BiometricError.
Platform support
- iOS -- uses
LocalAuthentication.framework(LAContext). Touch ID and Face ID on supported devices. Add theios.NSFaceIDUsageDescriptionbuild hint when targeting Face ID hardware. - Android -- uses
BiometricPrompton API 29+ (Android 10) andFingerprintManagerCompaton API 23-28. Fingerprint, face, and iris modalities are reported perPackageManagerfeatures. The legacy path goes through the support library rather thanandroid.hardware.fingerprintdirectly, which Android removed in API 37, so an app compiled against any platform still authenticates on an API 23-28 device. - JavaSE simulator -- behaves as a real device with no enrolled
biometrics by default. The
Simulate -> Biometric Simulationsubmenu in the simulator lets you toggle hardware availability, enrolled modalities, and the outcome of the nextauthenticate(String)call. - All other platforms (desktop deploy, JavaScript, ...) -- this base
class is returned as-is and acts as a non-supporting fallback:
isSupported()/canAuthenticate()returnfalseandauthenticate(String)completes withBiometricError.NOT_AVAILABLE. Application code does not need platformifstatements -- always gate biometrics oncanAuthenticate()before invoking the prompt.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionPrompts the user to authenticate.authenticate(String reason) Convenience forauthenticate(new AuthenticationOptions().setReason(reason)).booleanReturnstruewhen the device is ready to authenticate right now: hardware present, at least one biometric enrolled, and not in a locked-out state.Lists the biometric modalities the device can authenticate with.static BiometricsReturns the platform-specific singleton owned by the current port.Answers what the application should call biometric authentication on this device: a fingerprint, the face, or biometrics in general when the device cannot say which.booleanReturnstruewhen biometric hardware exists on the device, regardless of whether the user has enrolled biometrics.protected static BiometricPromptTypepromptTypeOf(List<BiometricType> types) The answer ofgetPromptType()for a device that can authenticate with the given modalities.booleanCancels an in-flightauthenticate(AuthenticationOptions)call if one is running.
-
Constructor Details
-
Biometrics
protected Biometrics()Subclasses are constructed by the port. Application code obtains the active instance viagetInstance().
-
-
Method Details
-
getInstance
Returns the platform-specific singleton owned by the current port. On ports that do not implement biometrics this returns a baseBiometricsinstance whose methods report the device as unsupported, so calling code never needs anullcheck or a platform-specificif. -
isSupported
public boolean isSupported()Returnstruewhen biometric hardware exists on the device, regardless of whether the user has enrolled biometrics. Combine withcanAuthenticate()to gate UI affordances: show the "Use biometrics" toggle whenisSupported()is true, but only invokeauthenticate(AuthenticationOptions)whencanAuthenticate()is also true. Returnsfalseon the fallback base class. -
canAuthenticate
public boolean canAuthenticate()Returnstruewhen the device is ready to authenticate right now: hardware present, at least one biometric enrolled, and not in a locked-out state. Returnsfalseon the fallback base class. -
getAvailableBiometrics
Lists the biometric modalities the device can authenticate with. On iOS this is the enrolled one,
BiometricType.FINGERPRINTorBiometricType.FACE. On Android 10 and later it is every modality the device has hardware for, once any biometric is enrolled: Android does not report which one the user enrolled, and the list may containBiometricType.IRISthere. Windows and Linux do not learn the modality and reportBiometricType.STRONGalone.To choose the wording of the application's own UI use
getPromptType(), which reduces this list to one answer.Returns
an empty list when nothing is enrolled or the device is unsupported
-
getPromptType
Answers what the application should call biometric authentication on this device: a fingerprint, the face, or biometrics in general when the device cannot say which. Use it to choose the wording and icon of the application's own UI; it needs no platform or OS version check.
String label; switch (Biometrics.getInstance().getPromptType()) { case FINGERPRINT: label = "Sign in with fingerprint"; break; case FACE: label = "Sign in with face"; break; case BIOMETRICS: label = "Sign in with biometrics"; break; default: label = null; // NONE: offer the password only }A single modality is named, and anything else that can authenticate is
BiometricPromptType.BIOMETRICS:- iOS names Touch ID or Face ID; any other sensor is generic.
- Android reports that some biometric is enrolled and not which, so a device with more than one kind of biometric hardware is generic. A device with one kind is named.
- Windows and Linux are always generic: Windows Hello does not say what it verifies with, and that may be a PIN.
This asks the platform each time it is called, which on some ports waits for another thread or process; on Android,
canAuthenticate()andgetAvailableBiometrics()wait for the UI thread as well. Call it when building a screen and keep the answer, not once per paint.Returns
BiometricPromptType.NONEwhencanAuthenticate()isfalse -
promptTypeOf
The answer of
getPromptType()for a device that can authenticate with the given modalities. A port that learns both facts from one platform query overridesgetPromptType()and calls this, so the rule stays in one place.Parameters
types: whatgetAvailableBiometrics()reported, may benull
Returns
never
BiometricPromptType.NONE -
authenticate
Prompts the user to authenticate. The returned
AsyncResourcecompletes withtrueon success, or with aBiometricExceptionon failure (consultBiometricException.getError()for the typed code). On the fallback base class this completes immediately withBiometricError.NOT_AVAILABLEso callers don't need to platform- check before invoking.Parameters
opts: non-null configuration;AuthenticationOptions.setReason(String)should be set
-
authenticate
Convenience forauthenticate(new AuthenticationOptions().setReason(reason)). -
stopAuthentication
public boolean stopAuthentication()Cancels an in-flight
authenticate(AuthenticationOptions)call if one is running. The pendingAsyncResourcecompletes withBiometricError.USER_CANCELED.Returns
truewhen a call was cancelled;falsewhen no authentication was pending. Alwaysfalseon the fallback base class.
-