Check whether Android supports fingerprint recognition and whether fingerprint has been entered

Android M starts, and fingerprint related functions are added to the system
The main classes used are: FingerprintManager
Only three methods are available:

Return value Method signature Explain
void authenticate(FingerprintManager.CryptoObject crypto, CancellationSignal cancel, int flags, FingerprintManager.AuthenticationCallback callback, Handler handler) For fingerprint verification
boolean hasEnrolledFingerprints() Determine if at least one fingerprint is registered.
boolean isHardwareDetected() Determine if the fingerprint hardware is present and functioning properly.

With the above methods, it is easy to determine whether the mobile phone supports fingerprint and whether there is fingerprint entry. The following is the code implementation:

/**
     * Check fingerprints
     *
     * @param context
     * @return
     */
    public static rx.Observable<java.lang.Boolean> checkFingerprint(final Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            return //Request fingerprint permission
                    new RxPermissions(context)
                            .request(Manifest.permission.USE_FINGERPRINT)
                            .map(new Func1<Boolean, Boolean>() {
                                @TargetApi(Build.VERSION_CODES.M)
                                @Override
                                public Boolean call(Boolean aBoolean) {
                                    if (aBoolean) {
                                        FingerprintManager fingerprintManager =
                                                (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
                                        //Determine if fingerprint hardware exists and functions
                                        return fingerprintManager.isHardwareDetected() && fingerprintManager.hasEnrolledFingerprints();
                                    }
                                    return false;
                                }
                            });
        } else {
            return rx.Observable.create(new Observable.OnSubscribe<Boolean>() {
                @Override
                public void call(Subscriber<? super Boolean> subscriber) {
                    subscriber.onNext(false);
                }
            });
        }
    }

Tags: Android Mobile Java

Posted on Sun, 03 May 2020 15:50:13 -0400 by pernest