BiometricAuth
This library brings the new Android P BiometricPrompt for fingerprint authentication to Android SDK 23, using RxJava2 (and Kotlin).
| Android 23..27 (>= Marshmallow) | Android 28 (Pie) |
|---|---|
![]() |
![]() |
Setup
To use this library your minSdkVersion must be >= 15 (Note that the dialog however will only show starting Android SDK 23).
allprojects {
repositories {
...
jcenter()
}
}
dependencies {
implementation 'com.tailoredapps:biometricauth:1.0.0'
}Usage
Create a BiometricAuth instance:
val biometricAuth = BiometricAuth.create(this); // where this is an (AppCompat-)Activityif(!biometricAuth.hasFingerprintHardware) {
//The devices does not support fingerprint authentication (i.e. has no fingerprint hardware)
Toast.makeText(this, "Device does not support fingerprint", Toast.LENGTH_SHORT).show()
} else if(!biometricAuth.hasFingerprintsEnrolled) {
//The user has not enrolled any fingerprints (i.e. fingerprint authentication is not activated by the user)
Toast.makeText(this, "User has not enrolled any fingerprints", Toast.LENGTH_SHORT).show()
} else {
biometricAuth
.authenticate(
title = "Please authenticate",
subtitle = "'Awesome Feature' requires your authentication",
description = "'Awesome Feature' exposes data private to you, which is why you need to authenticate.",
negativeButtonText = "Cancel",
prompt = "Touch the fingerprint sensor",
notRecognizedErrorText = "Not recognized"
)
.subscribe(
{ Log.d("BiometricAuth", "User authentication successful.") },
{ throwable ->
if(throwable is BiometricAuthenticationCancelledException) {
Log.d("BiometricAuth", "User cancelled the operation")
} else if(throwable is BiometricAuthenticationException) {
Log.d("BiometricAuth", "Unrecoverable authentication error")
} else {
Log.d("BiometricAuth", "Error during user authentication.", it)
}
}
)
}The authenticate() function returns a Completable, which either:
- completes, which indicates an authentication success, or
- emits an error:
BiometricAuthenticationCancelledException, which signals that the operation has been cancelled (most likely triggered by the user).BiometricAuthenticationException, which signals an unrecoverable error during biometric authentication (e.g. too many invalid attempts). The exception contains the localized error-message provided by the system.- any other unexpected error during authentication (Not any of the internal fingerprint errors like "not detected", as they will be handled internally).
What's inside?
On Android P (SDK 28) devices, the new BiometricPrompt API is used.
On devices running Android Marshmallow to Oreo (SDK 23..27), the FingerprintManagerCompat API is used in combination with a custom UI, which imitates the AndroidPie BiometricPrompt Bottom-Sheet.
On older devices, where Fingerprint Authentication is not supported by native Android SDKs, calling hasFingerprintHardware or hasFingerprintsEnrolled will always return false.
License
Copyright 2018 Tailored Media GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

