Add Android biometric authentication seems really difficult...
-
Hi all
Maybe there is a problem about how Qt work on Android. I would to add the biometric authentication to my app by using java code as explained here. However the use of BiometricPrompt android class require to have a androidx.fragment.app.Fragment and a corresponding FragmentActivity or AppCompatActivity. However the base android Qt java class derive from standard Activity (returned by QNativeInterface::QAndroidApplication::context()) and, as consequence, is not possible to use BiometricPrompt and a lot of other API call working with Fragment. I'm thinking for a solution to create a FragmentActivity over the QML android window but is it the correct way to do or someone have a better and more simpler suggestion to follow?
Thank you -
Hi there!
It is indeed a little tricky, but - you don't actually need the Fragment-based component. I have a solution for this to utilize biometrics in the most basic way (merely as an "authenticated? yes/no" method, no payload) by returning the result as a string to a JNI method.
You can construct and display the Biometric prompt by using the packages
androidx.biometric.BiometricManager
and
android.hardware.biometrics.BiometricPrompt
The second one is the important one here. It's the standard, non-AndroidX package for the prompt. That one does not require a Fragment.
Then, you construct your BiometricManager from the app context. (Remember to set a theme via style ID for the context first!)
The BiometricManager is useful for checking whether the device even offers biometrics or whether they are set up in the first place.
Afterwards, you can construct a BiometricPrompt using BiometricPrompt.Builder() by passing the context to it.Finally, you call authenticate() on your BiometricPrompt, which will trigger the display of the prompt. By using callbacks, the result can be transmitted to the app using a JNI method.
The examples you can find in the official Android documentation for these packages should give enough info to implement this. But if they don't, let me know here and I'll gladly try to help further.
-
I really thank you for the suggestion. I didn't know a (legacy?) version of BiometricPrompt existed. I'll try to use this package and report here the result.
-
Thanks to your suggestion I finally was able to add the biometric authentication to my library. In case you will need it feel free to use. You can find it here:
-