Marshmallow Fingerprint Scanner Hardware

I want to get started with the Marshmallow fingerprint authentication API. I understand that to request permission, I have to use the following method:

ContextCompat.checkSelfPermission(getContext(), Manifest.permission.USE_FINGERPRINT); 

And I have to check if the device works at API level 23 or higher. But before I ask for permission, I would like to check if the device really has a fingerprint scanner. For this check, I found the following two methods:

 FingerprintManager manager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE); manager.isHardwareDetected(); manager.hasEnrolledFingerprints(); 

But both methods require USE_FINGERPRINT permission to call at all. Why do I want to ask permission to use a fingerprint scanner that I donโ€™t even know? Are there other ways to find out if a scanner exists? Or is this the only way to request permission first?

+5
source share
3 answers

I just found the FingerprintManagerCompat class that does exactly what you expect:

A class that coordinates access to fingerprint equipment.

On platforms up to M, this class behaves as if there were no available equipment for fingerprints.

The same methods from the FingerprintManager in this class do not require USE_FINGERPRINT permission, allowing you to call them before requesting USE_FINGERPRINT permission.

 FingerprintManagerCompat manager = FingerprintManagerCompat.from(mContext); manager.isHardwareDetected(); manager.hasEnrolledFingerprints(); 

These methods will also produce the expected false results on devices with the Marshmallow provisional pointer.

+10
source

Try hasSystemFeature(PackageManager.FEATURE_FINGERPRINT) on the PackageManager instance (you can get it from calling getPackageManager() on any convenient Context ).

+4
source

FingerprintManager class supports Android devices running on API 23 or higher and throws an exception on devices with earlier versions of Android.

FingerprintManagerCompat class returns the backward compatibility of the isHardwareDetected method in the lower version of Android, but always returns false for API 23 or higher

I chose the best one and created this method to test the hardware support of the FingerPrint Sensor in all versions of Android .

  private boolean isSensorAvialable() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return ActivityCompat.checkSelfPermission(AppContext, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED && AppContext.getSystemService(FingerprintManager.class).isHardwareDetected(); } else { return FingerprintManagerCompat.from(AppContext).isHardwareDetected(); } } 
+1
source

Source: https://habr.com/ru/post/1242652/


All Articles