Android应用程序中的指纹感应与外部指纹感应设备

时间:2017-07-19 05:53:43

标签: android sqlite authentication fingerprint

我是android studio的初学者。我们有一个外部指纹感应设备。我们必须在我们的Android应用程序(支持所有Android版本)中检测到指纹。如果我正在下载指纹sdk,是否会支持所有外部设备?我不知道从哪里开始。有人可以帮我这么做吗?

1 个答案:

答案 0 :(得分:0)

使用以下内容。 public class FingerprintUtils {

private static final String KEY_NAME = "PS";
KeyguardManager keyguardManager;
FingerprintManager fingerprintManager;
Context context;
private KeyStore keyStore;
private Cipher cipher;


public FingerprintUtils(Context context) {
    this.context = context;

}


public void checkFingerprintValidations(FPValidationCallback callback) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        fingerprintManager = (FingerprintManager) context.getSystemService(FINGERPRINT_SERVICE);
        if (!fingerprintManager.isHardwareDetected()) {
            //Log.e("Test", "Your Device does not have a Fingerprint Sensor");
            callback.isHardwareAvailFalse();
            return;
        }
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            //Log.e("Test", "Fingerprint authentication permission not enabled");
            callback.isPermissionPermittedFalse();
            return;
        }
        if (!fingerprintManager.hasEnrolledFingerprints()) {
            //Log.e("Test", "Register at least one fingerprint in Settings");
            callback.isFingerRegisteredFalse();
            return;
        }
        if (!keyguardManager.isKeyguardSecure()) {
            //Log.e("Test", "Lock screen security not enabled in Settings");
            callback.isLockScreenSecurityFalse();
            return;
        }

        callback.continueOnFingerprint();
    } else {
        callback.isDevice23False();
    }


}

public void matchFingerPrintsNow(FPResultCallback FPResultCallback) {

    generateKey();

    if (cipherInit()) {
        FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
        FingerprintHandler helper = new FingerprintHandler(context);

        helper.startAuth(fingerprintManager, cryptoObject, FPResultCallback);
    }

}


//--------------------------------------Used in this class private access------------------
private void generateKey() {
    try {
        keyStore = KeyStore.getInstance("AndroidKeyStore");
    } catch (Exception e) {
        e.printStackTrace();
    }

    KeyGenerator keyGenerator;
    try {
        keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw new RuntimeException("Failed to get KeyGenerator instance", e);
    }

    try {
        keyStore.load(null);
        keyGenerator.init(new
                KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(
                        KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        keyGenerator.generateKey();
    } catch (NoSuchAlgorithmException |
            InvalidAlgorithmParameterException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }
}


private boolean cipherInit() {
    try {
        cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get Cipher", e);
    }

    try {
        keyStore.load(null);
        SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
                null);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {
        return false;
    } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
    }
}

}

    fingerprintUtils.checkFingerprintValidations(new FPValidationCallback() {
        @Override
        public void isHardwareAvailFalse() {
            updateText("isHardwareAvailFalse");
        }

        @Override
        public void isPermissionPermittedFalse() {
            updateText("isPermissionPermittedFalse");
        }

        @Override
        public void isFingerRegisteredFalse() {
            updateText("isFingerRegisteredFalse");
        }

        @Override
        public void isLockScreenSecurityFalse() {
            updateText("isLockScreenSecurityFalse");
        }

        @Override
        public void isDevice23False() {
            updateText("isDevice23False");
        }

        @Override
        public void continueOnFingerprint() {
            fingerprintUtils.matchFingerPrintsNow(new FPResultCallback() {
                @Override
                public void onFingerPrintError(int errMsgId, CharSequence errString) {
                    updateText("Error " /*+ errMsgId + " "*/ + errString.toString());
                }

                @Override
                public void onFingerPrintHelp(int helpMsgId, CharSequence helpString) {
                    updateText("onFingerPrintHelp "/* + helpMsgId + " "*/ + helpString.toString());
                }

                @Override
                public void onFingerPrintFailed() {
                    updateText("FingerPrintFailed");
                }

                @Override
                public void onFingerPrintSucceeded(FingerprintManager.AuthenticationResult result) {
                    statusTV.setText("Authentication Successful!");
                    Intent in = new Intent();
                    setResult(RESULT_OK, in);
                    finish();
                }
            });
        }
    });

公共接口FPValidationCallback {

void isHardwareAvailFalse();
void isPermissionPermittedFalse();
void isFingerRegisteredFalse();
void isLockScreenSecurityFalse();
void isDevice23False();`
void continueOnFingerprint();

}