Exploring Fingerprint Api in Android M or 6.0

Hello developers google recently launched there latest Android version which was called Marshmallow in which it has added various new features.

One of them is Fingerprint api which allows developers to add an extra layer to security for apps related to money or storing there secure data.

I haven’t myself explored much but some basic methods i have tried and they worked quite well.

First of all for this you will need latest sdk i.e. Android api level 23.

First we need to initialize FingerPrint Manager like this

private FingerprintManagerCompat fingerprintManagerCompat;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);
    fingerprintManagerCompat = FingerprintManagerCompat.from(this);
    if(fingerprintManagerCompat.isHardwareDetected()){
        Toast.makeText(this,"Successful",Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(this,"UnSuccessful",Toast.LENGTH_SHORT).show();
    }
}

The above method detects whether system have hardware feature for fingerprint and if yes it is functional or not.

fingerprintManagerCompat.hasEnrolledFingerprints()

This one works to determine if there is at least one fingerprint enrolled and returns true if at least one fingerprint is present.

/**
 * Request authentication of a crypto object. This call warms up the fingerprint hardware
 * and starts scanning for a fingerprint. It terminates when
 * {@link AuthenticationCallback#onAuthenticationError(int, CharSequence)} or
 * {@link AuthenticationCallback#onAuthenticationSucceeded(AuthenticationResult) is called, at
 * which point the object is no longer valid. The operation can be canceled by using the
 * provided cancel object.
 *
 * @param crypto object associated with the call or null if none required.
 * @param flags optional flags; should be 0
 * @param cancel an object that can be used to cancel authentication
 * @param callback an object to receive authentication events
 * @param handler an optional handler for events
 */
public void authenticate(@Nullable CryptoObject crypto, int flags,
        @Nullable CancellationSignal cancel, @NonNull AuthenticationCallback callback,
        @Nullable Handler handler) {
    IMPL.authenticate(mContext, crypto, flags, cancel, callback, handler);
}

This method is used for authentication and return results to call back.

Try this out in your supported phones, i will explore more and update you guys on this. Till then Happy Developing 🙂

Thanks for reading. 🙂