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指纹的采集流程图:
源码如下:
FingerPrintEnrollBase:
- /*
- * 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);
- }