Search code examples
androidxamarin.formsxamarin.androidfingerprint

How to find if user has enrolled any fingerprints


I need to identify if the user has any fingerprint register on their device. However, I got an error using this line

  FingerprintManagerCompat  fpm = (FingerprintManagerCompat)CrossCurrentActivity.Current.Activity.GetSystemService(Context.FingerprintService);

``` error ``

{System.InvalidCastException: Specified cast is not valid.
  at app.Droid.lyA.FingerPrint.HasEnrolledFingerprints () [0x00002] in /../../../appmobile/../Droid/lyA/FingerPrint.cs:19 }

This is what I need to return.

here is the cast


Solution

  • Converting my comment to an answer-

    It seems that there is an error with the casting you did at

    FingerprintManagerCompat fpm = (FingerprintManagerCompat)CrossCurrentActivity.Current.Activity.GetSystemService(Context.FingerprintService);
    

    It seems that whatever is being returned here cannot be cast to FingerprintManagerCompat.

    As mentioned in another answer, the correct way to use FingerprintManagerCompat is actually -

    FingerprintManagerCompat fpm= FingerprintManagerCompat.From(this);
    

    I would suggest changing to this and checking if it solves the issue.