Android6.0指纹识别开发案例
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, 0, 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, charsequence errstring); void onenrollmentprogresschange(int steps, 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; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java实现支付宝退款功能
下一篇: java实现微信点餐申请微信退款