Okay so my application is to be used on a device that does not have a fingerprint sensor but has a face unlock option. I tested the following code with my phone (Xiaomi Redmi note 8 pro) which has both fingerprint and face unlock options. the fingerprint unlock is working fine. But if I remove my fingerprints from the phone settings, the face unlock is not working.
private fun setUpBiometric() {
val biometricManager = BiometricManager.from(this)
when (biometricManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK or BiometricManager.Authenticators.DEVICE_CREDENTIAL)) {
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
activityBinding.biometricCheck.visibility = View.GONE
activityBinding.words.text = "No biometric features available on this device."
}
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
activityBinding.biometricCheck.visibility = View.GONE
activityBinding.words.text = "Biometric features are currently unavailable."
}
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
// Prompts the user to create credentials that your app accepts.
val enrollIntent = Intent(Settings.ACTION_BIOMETRIC_ENROLL).apply {
putExtra(
Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
android.hardware.biometrics.BiometricManager.Authenticators.BIOMETRIC_STRONG or android.hardware.biometrics.BiometricManager.Authenticators.DEVICE_CREDENTIAL
)
}
this.startActivityForResult(enrollIntent, 1)
}
}
executor = ContextCompat.getMainExecutor(this)
biometricPrompt = BiometricPrompt(this, executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(
errorCode: Int,
errString: CharSequence
) {
super.onAuthenticationError(errorCode, errString)
Toast.makeText(
applicationContext,
"Authentication error: $errString", Toast.LENGTH_SHORT
)
.show()
}
override fun onAuthenticationSucceeded(
result: BiometricPrompt.AuthenticationResult
) {
super.onAuthenticationSucceeded(result)
Log.d("TAG", "onAuthenticationSucceeded: "+result.authenticationType)
val prefs = SharedPrefsRepository(this@LoginActivity)
CoroutineScope(Dispatchers.IO).launch {
prefs.saveUser(LoginResponse())
startActivity(Intent(this@LoginActivity, HomeActivity::class.java))
finish()
}
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Toast.makeText(
applicationContext, getString(R.string.authetication_failed),
Toast.LENGTH_SHORT
)
.show()
}
})
promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(getString(R.string.biometric_login))
.setSubtitle(getString(R.string.use_biometric_for_login))
.setNegativeButtonText(getString(R.string.cancel))
.build()
}
this is called on a click listener.
biometricPrompt.authenticate(promptInfo)
can someone please tell me what to do for face unlock?