0

Hy, I am a newbie currently learning Mobile development. I am developing an application in which when the user presses the login button it will only give the option of Fingerprint authentication. but in my case, It gives options to the user to either uses Fingerprint or face authentication. How can I restrict the user to only Fingerprint authentication? Here I am using the following Code:

package com.example.fingerprintauth
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.biometric.BiometricPrompt

import android.widget.Button
import android.widget.Toast
import androidx.core.content.ContextCompat

import java.util.concurrent.Executor

class MainActivity : AppCompatActivity() {
    private lateinit var executor: Executor
    private lateinit var biometricPrompt: BiometricPrompt
    private lateinit var prompt: BiometricPrompt.PromptInfo
    //private var flag =0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        executor=ContextCompat.getMainExecutor(this)
        biometricPrompt= BiometricPrompt(this@MainActivity,executor,object:BiometricPrompt.AuthenticationCallback(){
            override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                //authStatusTv.text = "Authentication Error: $errString"
                Toast.makeText(this@MainActivity,"Authentication Error: $errString",Toast.LENGTH_SHORT).show()
            }

            override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                Toast.makeText(this@MainActivity,"Authentication Success",Toast.LENGTH_SHORT).show()
                startActivity(
                    Intent(
                        this@MainActivity, Home::class.java
                    )
                )
            }

            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                //authStatusTv.text = "Authentication Failed"
                Toast.makeText(this@MainActivity,"Authentication Failed",Toast.LENGTH_SHORT).show()
            }
        })
        val promptInfo=BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric Authentication")
            .setSubtitle("Login Using Fingerprint authentication")
            .setDeviceCredentialAllowed(true)
            //.setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
            .build()




        var authbtn = findViewById<Button>(R.id.authbtn)
        authbtn.setOnClickListener {
            biometricPrompt.authenticate(promptInfo)

        }
    }
}
  • You do not get that level of control. Through things like your commented-out `setAllowedAuthenticators()`, you can specify general criteria. How that maps to specific forms of authentication is up to Android and device manufacturers. – CommonsWare Sep 24 '21 at 12:40
  • setAllowedAuthenticators() gives error ( Unresolved reference: setAllowedAuthenticators) – Saad Mehmood Sep 24 '21 at 12:49
  • Regardless of that, the point still stands that the `BiometricPrompt` API unfortunately doesn't give you control over exactly which biometrics that can be used. If you want to allow fingerprint and nothing else, use the old `Fingerprintmanager` API (yes it's deprecated, but that doesn't mean it's being removed). – Michael Sep 24 '21 at 13:04
  • Set your `compileSdkVersion` to 30 or higher, as [`setAllowedAuthenticators()` exists](https://developer.android.com/reference/kotlin/android/hardware/biometrics/BiometricPrompt.Builder#setallowedauthenticators). – CommonsWare Sep 24 '21 at 13:32
  • setAllowedAuthenticators() gives error even compileSdkVersion = 31 – Imran Khan Saifi Mar 02 '23 at 06:27

0 Answers0