Android6.0指纹识别开发案例

Android6.0指纹识别开发案例,第1张

概述AndroidM指纹的资料太少,经过一段时间阅读原生Android代码,写了以下例子,贡献出来给需要帮助的人。

AndroID M指纹的资料太少,经过一段时间阅读原生AndroID代码,写了以下例子,贡献出来给需要帮助的人。
以下内容基于64位的高通cpu,搭载fpc1020芯片,此部分代码在原生androID上做了更改,以应付工厂指纹的测试。原生androID指纹录入需要采集10次(因不同的芯片而定)。

代码简单说明:

1. FingerPrintEnrollBase类:
重要的是

public interface Listener {   voID onEnrollmentHelp(CharSequence helpString);   voID onEnrollmentError(int errMsgid,CharSequence errString);   voID onEnrollmentProgressChange(int steps,int remaining); } 

这个接口。
  1)onEnrollmentHelp函数:是下层返回的指纹 *** 作帮助信息。
  2)onEnrollmentError函数是下层返回的错误信息,errMsgid是错误信息类型,其中1表示指纹硬件不可用,3表示超时,5表示 *** 作已取消。errString是errMsgid对应的文字信息,有需要的自己实验就知道了。
  3)onEnrollmentProgressChange()函数是显示当前指纹的采集情况,steps:总共需要录入多少次;remaining是剩余多少次,我在此函数中有打印Log。

2. FingerPrintActivity是主Activity类,FingerPrintEnrollBase是指纹Base类。
需要特别说明的是,在FingerPrintEnrollBase类中有一句:private byte[] mToken = new byte[69];这个数组正常情况是通过输入密码后经由framework层向hal层申请的,我这里为了省去验证那一块的代码,通过sizeof(hw_auth_token_t)计算出来的为69,需要的请依此计算。我在代码里实验过,此数组如果不是69(在64位cpu上),将直接导致程序crach!
fpc指纹的采集流程图:

/*  * copyright (C) 2015 The AndroID Open Source Project  *  * licensed under the Apache license,Version 2.0 (the "license");  * you may not use this file except in compliance with the license.  * You may obtain a copy of the license at  *  *  http://www.apache.org/licenses/liCENSE-2.0  *  * Unless required by applicable law or agreed to in writing,software  * distributed under the license is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implIEd.  * See the license for the specific language governing permissions and  * limitations under the license  */  package com.mediatek.test;  import androID.app.Activity; import androID.app.Fragment; import androID.content.Context; import androID.harDWare.fingerprint.FingerprintManager; import androID.os.Bundle; import androID.os.CancellationSignal; import androID.os.Handler; import androID.util.Log; /**  * local fragment to handle the state around fingerprint enrollment.  */ public class FingerPrintEnrollBase extends Fragment {   private int mEnrollmentSteps = -1;  private int mEnrollmentRemaining = 0;  private Listener mListener;  private boolean mEnrolling;  private CancellationSignal mEnrollmentCancel;  private Handler mHandler = new Handler();  private byte[] mToken = new byte[69];//if byte length != sizeof(hw_auth_token_t),throws Exception  private boolean mDone;  private static final String TAG = "FingerPrintEnrollBase";   @OverrIDe  public voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setRetainInstance(true);  }   @OverrIDe  public voID onAttach(Activity activity) {   super.onAttach(activity);  }   @OverrIDe  public voID onStart() {   super.onStart();   Log.d(TAG,"mToken length="+mToken.length);   if (!mEnrolling) {    FingerprintManager mfpM = (FingerprintManager) getActivity().getSystemService(Context.FINGERPRINT_SERVICE);    startEnrollment(mfpM);   }  }   @OverrIDe  public voID onStop() {   super.onStop();   if (!getActivity().isChangingConfigurations()) {    cancelEnrollment();   }  }   protected voID startEnrollment(FingerprintManager mfpM) {   mHandler.removeCallbacks(mTimeoutRunnable);   mEnrollmentSteps = -1;   mEnrollmentCancel = new CancellationSignal();   if(mfpM != null){    mfpM.enroll(mToken,mEnrollmentCancel,mEnrollmentCallback);   }   mEnrolling = true;  }   protected voID cancelEnrollment() {   mHandler.removeCallbacks(mTimeoutRunnable);   if (mEnrolling) {    mEnrollmentCancel.cancel();    mEnrolling = false;    mEnrollmentSteps = -1;   }  }   public voID setListener(Listener Listener) {   mListener = Listener;  }   public int getEnrollmentSteps() {   return mEnrollmentSteps;  }   public int getEnrollmentRemaining() {   return mEnrollmentRemaining;  }   public boolean isDone() {   return mDone;  }   private FingerprintManager.EnrollmentCallback mEnrollmentCallback    = new FingerprintManager.EnrollmentCallback() {    @OverrIDe   public voID onEnrollmentProgress(int remaining) {    if (mEnrollmentSteps == -1) {     mEnrollmentSteps = remaining;    }    mEnrollmentRemaining = remaining;    mDone = remaining == 0;    if (mListener != null) {     mListener.onEnrollmentProgressChange(mEnrollmentSteps,remaining);    }   }    @OverrIDe   public voID onEnrollmentHelp(int helpMsgid,CharSequence helpString) {    if (mListener != null) {     mListener.onEnrollmentHelp(helpString);    }   }    @OverrIDe   public voID onEnrollmentError(int errMsgid,CharSequence errString) {    if (mListener != null) {     mListener.onEnrollmentError(errMsgid,errString);    }   }  };   private final Runnable mTimeoutRunnable = new Runnable() {   @OverrIDe   public voID run() {    cancelEnrollment();   }  };   public interface Listener {   voID onEnrollmentHelp(CharSequence helpString);   voID onEnrollmentError(int errMsgid,int remaining);  } } 

 FingerPrintActivity:

package com.mediatek.test;  import java.io.fileinputStream; import java.io.IOException; import java.util.List; import java.util.Timer; import java.util.TimerTask;  import androID.R.color; import androID.app.Activity; import androID.app.AlertDialog; import androID.content.Context; import androID.content.DialogInterface; import androID.content.Intent; import androID.graphics.color; import androID.os.Bundle; import androID.os.CancellationSignal; import androID.os.Handler; import androID.os.Message; import androID.text.Spannable; import androID.text.style.ForegroundcolorSpan; import androID.util.Log; import androID.vIEw.KeyEvent; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.ImageVIEw; import androID.Widget.Progressbar; import androID.Widget.tableRow; import androID.Widget.TextVIEw; import androID.Widget.Toast;  import com.mediatek.test.R; import androID.harDWare.fingerprint.Fingerprint; import androID.harDWare.fingerprint.FingerprintManager;  public class FingerPrintActivity extends Activity implements FingerPrintEnrollBase.Listener{  private static final String TAG = "FingerPrintActivity";  private MyHandler mHandler = new MyHandler();  private TextVIEw fingerPrintAcquisitionTextVIEw,fingerPrintEnrollmentErrorVIEw,fingerPrintEnrollmentHelpVIEw;  private FingerPrintEnrollBase fpeb;  private FingerprintManager mFingerprintManager;    @OverrIDe  protected int setTitleID() {   return R.string.tittle_fingerprint;  }   @OverrIDe  protected Activity getActivity() {   return this;  }    @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.fingerprint);   fingerPrintAcquisitionTextVIEw = (TextVIEw)findVIEwByID(R.ID.fingerprint_message);   fingerPrintEnrollmentErrorVIEw= (TextVIEw)findVIEwByID(R.ID.fingerprint_enrollment_error);   fingerPrintEnrollmentHelpVIEw= (TextVIEw)findVIEwByID(R.ID.fingerprint_enrollment_help);    mFingerprintManager = (FingerprintManager) getActivity().getSystemService(     Context.FINGERPRINT_SERVICE);         }    private voID sendMsgToHandler(int msgType,String remark){   Message msg = Message.obtain();   msg.what = msgType;   Bundle bundle = new Bundle();   bundle.putString("result",remark);   msg.setData(bundle);   mHandler.sendMessage(msg);  }    @OverrIDe  public voID onEnrollmentHelp(CharSequence helpString) {   Log.d(TAG,"==onEnrollmentHelp="+helpString.toString());   sendMsgToHandler(3,helpString.toString());  }   @OverrIDe  public voID onEnrollmentError(int errMsgid,CharSequence errString) {   Log.d(TAG,"==onEnrollmentError errMsgid="+errMsgid+" errString="+errString.toString());   if(errMsgid == 3){//time out    postEnrollment();   }else if(errMsgid == 1){//harDWare lose efficacy    startEnrollment();   }else if(errMsgid == 5){//operation cancel   }else{    sendMsgToHandler(4,errString.toString());   }  }   @OverrIDe  public voID onEnrollmentProgressChange(int steps,int remaining) {   Log.d(TAG,"===============onEnrollmentProgressChange=======");   Log.d(TAG,"steps" + steps + "||remaining=" + remaining);   fingerPrintEnrollmentHelpVIEw.setText("");   fingerPrintEnrollmentErrorVIEw.setText("");   sendMsgToHandler(2,getString(R.string.fingerprint_valIDate_success));   buttonS.setEnabled(true);   stopFingerprint();  }    private voID startEnrollment(){   if (fpeb == null) {    fpeb = new FingerPrintEnrollBase();   }   fpeb.setListener(this);   fpeb.startEnrollment(mFingerprintManager);  }  private voID postEnrollment(){   if(mFingerprintManager != null){    mFingerprintManager.postEnroll();   }  }    private voID stopFingerprint() {   if (fpeb != null){    fpeb.cancelEnrollment();    onStop();   }  }    @OverrIDe  protected voID onStart() {   super.onStart();  }    @OverrIDe  protected voID onStop() {   super.onStop();   if(fpeb != null){    fpeb.setListener(null);   }  }   @OverrIDe  protected voID onResume() {   super.onResume();   if(mFingerprintManager == null){    mFingerprintManager = (FingerprintManager) getActivity().getSystemService(     Context.FINGERPRINT_SERVICE);   }      startEnrollment();  }    @OverrIDe  protected voID onPause() {   super.onPause();   postEnrollment();   mFingerprintManager = null;   stopFingerprint();   fpeb = null;  }     class MyHandler extends Handler{    @OverrIDe   public voID handleMessage(Message msg) {    Bundle bundle = null;    super.handleMessage(msg);    switch (msg.what) {    case 0:     removeMessages(0);     bundle = msg.getData();     String result = bundle.getString("result");     selfTestVIEw.setText(result);     selfTestVIEw.setTextcolor(result.contains("PASS")?color.GREEN:color.RED);     break;    case 1:     removeMessages(1);     bundle = msg.getData();     String rs = bundle.getString("result");     checkbroadVIEw.setText(bundle.getString("result"));     checkbroadVIEw.setTextcolor(rs.contains("PASS")?color.GREEN:color.RED);     break;    case 2:     removeMessages(2);     bundle = msg.getData();     String fingerprint = bundle.getString("result");     fingerPrintAcquisitionTextVIEw.setText(bundle.getString("result"));     fingerPrintAcquisitionTextVIEw.setTextcolor(fingerprint.contains("PASS")?color.GREEN:color.WHITE);     mbuttonSelftest.setEnabled(true);     mbuttonCheckerboardTest.setEnabled(true);     break;    case 3:     removeMessages(3);     bundle = msg.getData();     String enrollmentHelp = bundle.getString("result");     fingerPrintEnrollmentHelpVIEw.setText(bundle.getString("result"));     break;    case 4:     removeMessages(4);     bundle = msg.getData();     String enrollmentError = bundle.getString("result");     fingerPrintEnrollmentErrorVIEw.setText(bundle.getString("result"));     fingerPrintAcquisitionTextVIEw.setText("");     break;    default:     break;    }   }  }  } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android6.0指纹识别开发案例全部内容,希望文章能够帮你解决Android6.0指纹识别开发案例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148374.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存