Android Fingerprint Identification Development example _android

Source: Internet
Author: User

Android M fingerprint too little information, after a period of time to read the original Android code, wrote the following examples, to contribute to the people in need of help.

The following is based on a 64-bit Qualcomm CPU with a fpc1020 chip that has been changed on native Android to handle factory fingerprint tests. Native Android fingerprint entries need to be collected 10 times (depending on the chip).

Simple code Description:

 1. Fingerprintenrollbase class:

The important thing is

Java code

Public interface Listener { 
  void Onenrollmenthelp (Charsequence helpstring); 
  void Onenrollmenterror (int errmsgid, charsequence errstring); 
  void Onenrollmentprogresschange (int steps, int remaining); 
} 

This interface.

1) onenrollmenthelp function: is the lower level of the returned fingerprint operation Help information.

2 The Onenrollmenterror function is the error message returned by the lower level, Errmsgid is the error message type, where 1 indicates that the fingerprint hardware is unavailable, 3 indicates the timeout, and 5 indicates the operation has been canceled. Errstring is errmsgid corresponding to the text information, there is a need for their own experiments to know.

3 Onenrollmentprogresschange () function is to display the current fingerprint collection, steps: How many times the total need to enter; remaining is the number of times left, I have print log in this function.

 2. Fingerprintactivity is the main activity class, Fingerprintenrollbase is the fingerprint base class.

In particular, there is one sentence in the Fingerprintenrollbase class: Private byte[] Mtoken = new byte[69]; This array is normally applied through the framework layer to the HAL layer after entering the password. I am here to save the verification of that piece of code, through the sizeof (hw_auth_token_t) calculated for 69, the need for this calculation. I experimented in the code and this array, if not 69 (on a 64-bit CPU), would directly result in the program crach!

FPC Fingerprint Collection Flowchart:

The source code is as follows:

Fingerprintenrollbase:

Java code

* * Copyright (C) 2015 the Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "Li 
 Cense "); 
 * You could not use this file, except in compliance with the License. * You may obtain a copy of the License in * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by a Pplicable or agreed to in writing, software * Distributed under the License be distributed on ' as is ' basis, * 
 Without warranties or CONDITIONS of any KIND, either express or implied. * The License for the specific language governing permissions and * Limitations under the License/package Co 
 
M.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); 
  @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 (menrollmen 
   Tsteps = = 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:

Java code

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, "===============one 
  nrollmentprogresschange======= "); 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 (Conte Xt. 
  Fingerprint_service); 
 } startenrollment (); 
  } @Override protected void OnPause () {super.onpause (); 
  Postenrollment (); 
  Mfingerprintmanager = null; 
  Stopfingerprint (); 
 FPEB = null; Class MyHandler extends handler{@Override public void Handlemessage (msg) {Bundle Bundle = n 
   ull; 
   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; 
 } 
  } 
 } 
 
}

Above is the Android fingerprint data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.