Search code examples
androidauthenticationfingerprintandroid-fingerprint-api

How to add fingerprint authentication on under marshmallow devices


Sir, i am trying to add fingerprint authentication in my app. I have Coolpad note 3 lite mobile which support fingerprint authentication and it is running on Android version 5.1 Lollipop. But the problem is how can i authenticate fingerprint in it. I have seen Android Developers Documentation they said it is possible only on devices running on Marshmallow or above. So how can i do it on my Device..

That Code i Found on Internet is this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        //Get an instance of KeyguardManager and FingerprintManager//
        keyguardManager =
                (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        fingerprintManager =
                (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);

        textView = (TextView) findViewById(R.id.textview);

        //Check whether the device has a fingerprint sensor//
        if (!fingerprintManager.isHardwareDetected()) {
            // If a fingerprint sensor isn’t available, then inform the user that they’ll be unable to use your app’s fingerprint functionality//
            textView.setText("Your device doesn't support fingerprint authentication");
        }
        //Check whether the user has granted your app the USE_FINGERPRINT permission//
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            // If your app doesn't have this permission, then display the following text//
            textView.setText("Please enable the fingerprint permission");
        }

        //Check that the user has registered at least one fingerprint//
        if (!fingerprintManager.hasEnrolledFingerprints()) {
            // If the user hasn’t configured any fingerprints, then display the following message//
            textView.setText("No fingerprint configured. Please register at least one fingerprint in your device's Settings");
        }

        //Check that the lockscreen is secured//
        if (!keyguardManager.isKeyguardSecure()) {
            // If the user hasn’t secured their lockscreen with a PIN password or pattern, then display the following text//
            textView.setText("Please enable lockscreen security in your device's Settings");
        } else {
            try { generateKey();
            } catch (FingerprintException e) {
                e.printStackTrace();
            }

            if (initCipher()) {
                //If the cipher is initialized successfully, then create a CryptoObject instance//
                cryptoObject = new FingerprintManager.CryptoObject(cipher);

                // Here, I’m referencing the FingerprintHandler class that we’ll create in the next section. This class will be responsible
                // for starting the authentication process (via the startAuth method) and processing the authentication process events//
                FingerprintHandler helper = new FingerprintHandler(this);
                helper.startAuth(fingerprintManager, cryptoObject);
            }
        }
    }

Solution

  • Fingerprint scanner is not a feature in below Marshmallow (Now available on Android M).

    So each company as Samsung, Motorola, HTC create is own API and SDK to access to fingerprint sensor.

    here is the StackOverflow thread

    Or you can use FingerprintManagerCompat class of Support Library v4 to support minSdk less than 23.

    find more on Developer Doc

    github sample

    As per @Michael comment

    Note that FingerprintManagerCompat doesn't give you access to any fingerprint sensor that might exist on pre-Marshmallow devices. It only saves you the trouble of checking the API level yourself.