Home

Awesome

Biometrics Authentication in Android

Android-Studio Kotlin Version API

Biometrics API

About this project :

Finger Print UnlockFace Unlock

This application provides the below feature

How it works :

  1. Add the Gradle dependency to your app module
    def biometric_version=  '1.2.0-alpha04'
    implementation "androidx.biometric:biometric:$biometric_version"
  1. Check whether the device supports biometric authentication
    val biometricManager =  BiometricManager.from(context)
    if(biometricManager.canAuthenticate()  ==  BiometricManager.BIOMETRIC_SUCCESS){
	    // you can authenticate with biometrics
    }
  1. Create an instance of BiometricPrompt
    private fun instanceOfBiometricPrompt():  BiometricPrompt  {
        val executor =  ContextCompat.getmainExecutor(context)
        val callback =  object:BiometricPrompt.AuthenticationCallback()  {
            override fun onAuthenticationError(errorCode:  Int, errString:  CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                showMessage("$errorCode :: $errString")
            }
            override fun onAuthenticationFailed()  {
                super.onAuthenticationFailed()
                showMessage("Authentication failed for an unknown reason")
            }
            override fun onAuthenticationSucceeded(result:  BiometricPrompt.AuthenticationResult{
                super.onAuthenticationSucceeded(result)
                showMessage("Authentication was successful")
            }
        }
        val biometricPrompt =  BiometricPrompt(context, executor, callback)
        return biometricPrompt
    }
  1. Build a PromptInfo object
    promptInfo =  BiometricPrompt.PromptInfo.Builder()
	    .setTitle("Biometric login for my app")
	    .setSubtitle("Log in using your biometric credential")
	    // Can't call setNegativeButtonText() and  // setAllowedAuthenticators(... or DEVICE_CREDENTIAL) at the same time.//
	    //.setNegativeButtonText("Use account password") //
	    .setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
	    .build()
  1. Ask the user to authenticate
    biometricPrompt.authenticate(promptInfo)

For API level 21 -23 :

    keyguardManager = getSystemService(KEYGUARD_SERVICE) as KeyguardManager
    if (keyguardManager.isKeyguardSecure) {
        //Screen lock is enabled, do authentication.
    }
    startActivityForResult(Intent(Settings.ACTION_SECURITY_SETTINGS), REQUEST_CODE)

For more info go to Android Developers Biometric Blog

Find this example useful? :heart:

Support it by joining stargazers for this repository. :star:

Awesome Mobile Libraries

License

Copyright 2022 Simform Solutions

 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.