Android6.0指紋識別開發

來源:互聯網
上載者:User

標籤:over   oct   sig   停止   hand   port   引入   系統調用   function   

近期在做android指紋相關的功能,Google在android6.0及以上版本號碼對指紋識別進行了官方支援。當時在FingerprintManager和FingerprintManagerCompat這兩個之間糾結。當中使用FingerprintManager要引入com.android.support:appcompat-v7包。考慮到包的大小,決定使用v4相容包FingerprintManagerCompat來實現。主要實現的工具類FingerprintUtil:驗證手機是否支援指紋識別方法callFingerPrintVerify(),主要驗證手機硬體是否支援(6.0及以上),有沒有錄入指紋,然後有沒有開啟鎖屏password。開始驗證對於識別成功,失敗能夠進行對應的回調處理。
 public class FingerprintUtil{    private FingerprintManagerCompat mFingerprintManager;    private KeyguardManager mKeyManager;    private CancellationSignal mCancellationSignal;    private Activity mActivity;    public FingerprintUtil(Context ctx) {        mActivity = (Activity) ctx;        mFingerprintManager = FingerprintManagerCompat.from(mActivity);        mKeyManager = (KeyguardManager) mActivity.getSystemService(Context.KEYGUARD_SERVICE);    }    public void callFingerPrintVerify(final IFingerprintResultListener listener) {        if (!isHardwareDetected()) {            return;        }        if (!isHasEnrolledFingerprints()) {            if (listener != null) {                listener.onNoEnroll();            }            return;        }        if (!isKeyguardSecure()) {            if (listener != null) {                listener.onInSecurity();            }            return;        }        if (listener != null) {            listener.onSupport();        }        if (listener != null) {            listener.onAuthenticateStart();        }        if (mCancellationSignal == null) {            mCancellationSignal = new CancellationSignal();        }        try {            mFingerprintManager.authenticate(null, 0, mCancellationSignal, new FingerprintManagerCompat.AuthenticationCallback() {                //多次嘗試都失敗會走onAuthenticationError。會停止回應一段時間。提示嘗試次數過多。請稍後再試。

@Override public void onAuthenticationError(int errMsgId, CharSequence errString) { if (listener != null) listener.onAuthenticateError(errMsgId, errString); } //指紋驗證失敗走此方法,比如小米前4次驗證失敗走onAuthenticationFailed,第5次走onAuthenticationError @Override public void onAuthenticationFailed() { if (listener != null) listener.onAuthenticateFailed(); } @Override public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { if (listener != null) listener.onAuthenticateHelp(helpMsgId, helpString); } //當驗證的指紋成功時會回調此函數。然後不再監聽指紋sensor @Override public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) { if (listener != null) listener.onAuthenticateSucceeded(result); } }, null); } catch (Exception e) { e.printStackTrace(); } } /** * 是否錄入指紋,有些裝置上即使錄入了指紋,可是沒有開啟鎖屏password的話此方法還是返回false * * @return */ private boolean isHasEnrolledFingerprints() { try { return mFingerprintManager.hasEnrolledFingerprints(); } catch (Exception e) { return false; } } /** * 是否有指紋識別硬體支援 * * @return */ public boolean isHardwareDetected() { try { return mFingerprintManager.isHardwareDetected(); } catch (Exception e) { return false; } } /** * 推斷是否開啟鎖屏password * * @return */ private boolean isKeyguardSecure() { try { return mKeyManager.isKeyguardSecure(); } catch (Exception e) { return false; } } /** * 指紋識別回調介面 */ public interface IFingerprintResultListener { void onInSecurity(); void onNoEnroll(); void onSupport(); void onAuthenticateStart(); void onAuthenticateError(int errMsgId, CharSequence errString); void onAuthenticateFailed(); void onAuthenticateHelp(int helpMsgId, CharSequence helpString); void onAuthenticateSucceeded(FingerprintManagerCompat.AuthenticationResult result); } public void cancelAuthenticate() { if (mCancellationSignal != null) { mCancellationSignal.cancel(); mCancellationSignal = null; } } public void onDestroy() { cancelAuthenticate(); mKeyManager = null; mFingerprintManager = null; }

參考了一些資料,做了一些驗證。得到一些結論:1、當指紋識別失敗後,會調用onAuthenticationFailed()方法,這時候指紋感應器並沒有關閉,Google原生系統給了我們5次重試機會,也就是說,連續調用了4次onAuthenticationFailed()方法後,第5次會調用onAuthenticateError(int errMsgId, CharSequence errString)方法,此時errMsgId==7。

2、每次又一次授權,哪怕不去校正。取消時會走onAuthenticateError(int errMsgId, CharSequence errString) 方法,當中errMsgId==5,3、當系統調用了onAuthenticationError()和onAuthenticationSucceeded()後,感應器會關閉,僅僅有我們又一次授權。再次調用authenticate()方法後才幹繼續使用指紋識別功能。

4、相容android6.0下面系統的話,不要使用FingerprintManagerCompat, 低於M的系統版本號碼。FingerprintManagerCompat不管手機是否有指紋識別模組,均覺得沒有指紋識別,能夠用FingerprintManager來做。5、考慮到安全因素,最好authenticate(CryptoObject crypto, CancellationSignal cancel, int flags, AuthenticationCallback callback, Handler handler)時增加CryptoObject 。crypto這是一個加密類的對象,指紋掃描器會使用這個對象來推斷認證結果的合法性。

這個對象能夠是null,可是這種話。就意味著app無條件信任認證的結果,這個過程可能被攻擊。資料能夠被篡改。這是app在這種情況下必須承擔的風險。

因此。建議這個參數不要置為null。這個類的執行個體化有點麻煩,主要使用javax的security介面實現。

Android6.0指紋識別開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.