a***@lechuangnet.com
a***@lechuangnet.com
  • 发布:2017-11-17 15:04
  • 更新:2017-11-17 15:04
  • 阅读:2699

android 指纹识别插件 离线打包版

分类:5+ SDK

更新:HTML5+已经自带指纹识别。http://www.html5plus.org/doc/zh_cn/fingerprint.html。以下文档已过期。

安卓问识别插件,需要离线打包才能使用,下面贴上代码:
AndroidManifest.xml 文件

<!--指纹识别权限-->  
    <uses-permission android:name="android.permission.USE_FINGERPRINT"/>
package com.mall.trade.util;  

import android.annotation.TargetApi;  
import android.app.KeyguardManager;  
import android.os.Build;  
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;  
import android.support.v4.os.CancellationSignal;  

import com.mall.trade.activity.MainActivity;  

/**  
 * Created by Alen   on 2017/11/15 0015.  
 * 指纹识别模块  
 */  
public class FingerprintUtil {  
    public static CancellationSignal cancellationSignal;  
    public static final int DEVICENOTSUPPORTED = 1011;//设备不支持  
    public static final int DEVICENOTSAFEGUARD = 1012;//设备未处于安全保护中  
    public static final int DEVICENOTREGUIDFIN = 1013;//设备没有注册过指纹  
    public static final int SUCCESS = 1000;//支持指纹识别  
    public static final int FINGETSUCCESS = 1002;//指纹识别成功  
    public static final int ERROR = 1001;//指纹识别失败  

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  
    public static  void callFingerPrint(final OnCallBackListenr listener){  
        FingerprintManagerCompat managerCompat = FingerprintManagerCompat.from(MainActivity.getInstanse());  
        ResultObj res = checkFingerPrint(managerCompat);//检查指纹识别  
        if(res.getCode()!=SUCCESS){  
            if (listener != null){  
                if(res.getCode()==DEVICENOTSUPPORTED){//判断设备是否支持  
                    listener.onSupportFailed(res);  
                }else if(res.getCode()==DEVICENOTSAFEGUARD){//判断设备是否处于安全保护中  
                    listener.onInsecurity(res);  
                }else if(res.getCode()==DEVICENOTSAFEGUARD){//设备没有注册过指纹  
                    listener.onEnrollFailed(res);  
                }  
            }  
            return;  
        }  
        if (listener != null)  
            listener.onAuthenticationStart(res); //开始指纹识别  

        cancellationSignal  = new CancellationSignal(); //必须重新实例化,否则cancel 过一次就不能再使用了  
        managerCompat.authenticate(null,0,cancellationSignal,new FingerprintManagerCompat.AuthenticationCallback(){  
            // 当出现错误的时候回调此函数,比如多次尝试都失败了的时候,errString是错误信息,比如华为的提示就是:尝试次数过多,请稍后再试。  
            @Override  
            public void onAuthenticationError(int errMsgId, CharSequence errString) {  
                if (listener != null)  
                    listener.onAuthenticationError(errMsgId ,errString );  
            }  

            // 当指纹验证失败的时候会回调此函数,失败之后允许多次尝试,失败次数过多会停止响应一段时间然后再停止sensor的工作  
            @Override  
            public void onAuthenticationFailed() {  
                if (listener != null)  
                    listener.onAuthenticationFailed(new ResultObj(ERROR,"验证失败"));  
            }  

            @Override  
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {  
                if (listener != null)  
                    listener.onAuthenticationHelp(helpMsgId,helpString);  
            }  

            // 当验证的指纹成功时会回调此函数,然后不再监听指纹sensor  
            @Override  
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {  
                if (listener != null)  
                    listener.onAuthenticationSucceeded(result);  
            }  
            ;  
        },null);  

    }  

    /**  
     * 取消指纹识别  
     */  
    public static void cancelFingerPrint(){  
        if(cancellationSignal!=null){  
            cancellationSignal.cancel();  
        }  
    }  

    /**  
     * 判断当前设备是否支持指纹识别  
     * @return  
     */  
    public static ResultObj checkFingerPrint(){  
        FingerprintManagerCompat managerCompat = FingerprintManagerCompat.from(MainActivity.getInstanse());  
        return FingerprintUtil.checkFingerPrint(managerCompat);  
    }  

    /**  
     * 判断当前设备是否支持指纹识别  
     * @param managerCompat  
     * @return  
     */  
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)  
    public static ResultObj checkFingerPrint(FingerprintManagerCompat managerCompat){  
        if (!managerCompat.isHardwareDetected()){ //判断设备是否支持  
            return new ResultObj(FingerprintUtil.DEVICENOTSUPPORTED,"当前设备不支持指纹");  
        }  
        KeyguardManager keyguardManager =(KeyguardManager)MainActivity.getInstanse().getSystemService(MainActivity.getInstanse().KEYGUARD_SERVICE);  
        if (!keyguardManager.isKeyguardSecure()) {//判断设备是否处于安全保护中  
            return new ResultObj(FingerprintUtil.DEVICENOTSAFEGUARD,"当前设备没有设置密码保护");  
        }  
        if (!managerCompat.hasEnrolledFingerprints()){ //判断设备是否已经注册过指纹  
            return new ResultObj(FingerprintUtil.DEVICENOTREGUIDFIN,"还没有设置指纹");  
        }  
        return new ResultObj(FingerprintUtil.SUCCESS,"支持指纹识别");  
    }  

    public interface  OnCallBackListenr{  
        void onSupportFailed(ResultObj error);  
        void onInsecurity(ResultObj error);  
        void onEnrollFailed(ResultObj error);  
        void onAuthenticationStart(ResultObj error);  
        void onAuthenticationError(int errMsgId, CharSequence errString);  
        void onAuthenticationFailed(ResultObj error);  
        void onAuthenticationHelp(int helpMsgId, CharSequence helpString);  
        void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result);  
    }  

    public static class ResultObj{  
        private int code;  
        private String msg;  
        public ResultObj(int code,String msg){  
            this.code = code;  
            this.msg = msg;  
        }  

        public ResultObj getResult(){  
            return  this;  
        }  
        public int getCode(){  
            return  code;  
        }  
        public String getMsg(){  
            return  msg;  
        }  
    }  

    public static  void cancel(){  
        if (cancellationSignal != null)  
            cancellationSignal.cancel();  
    }  

}  

下面是调用代码:

/**  
     * 检查系统是否支持指纹识别  
     * @param pWebview  
     * @param array  
     */  
    public void checkFingerPrint(IWebview pWebview, JSONArray array){  
        String callbackId  = array.optString(0);  
        FingerprintUtil.ResultObj res = FingerprintUtil.checkFingerPrint();  
        if(res.getCode()==FingerprintUtil.SUCCESS){  
            String msg = toJSON(res.getCode(), res.getMsg());  
            Log.d("haiji", "FingerPrint " + res.getCode() + "  " + res.getMsg());  
            JSUtil.execCallback(pWebview,callbackId,msg, JSUtil.OK, false);//成功回调  
        }else{  
            Log.d("haiji", "FingerPrint "+res.getMsg());  
            String error = toJSON(res.getCode(), res.getMsg());  
            JSUtil.execCallback(pWebview,callbackId,error, JSUtil.ERROR, false);//失败回调  
        }  
    }  

    /**  
     * 开始进行指纹识别  
     * @param pWebview  
     * @param array  
     */  
    public void callFingerPrint(final IWebview pWebview, JSONArray array){  
        final String callbackId  = array.optString(0);  
        //开始指纹识别  
        FingerprintUtil.callFingerPrint(new FingerprintUtil.OnCallBackListenr() {  
            @Override  
            public void onSupportFailed(FingerprintUtil.ResultObj code) {  
                String error = toJSON(code.getCode(), code.getMsg());  
                Log.d("haiji", "FingerPrint "+code.getMsg());  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.ERROR, false);//失败回调  
//                JsObjectCommonMethod.showToast("当前设备不支持指纹");  
            }  

            @Override  
            public void onInsecurity(FingerprintUtil.ResultObj code) {  
                String error = toJSON(code.getCode(), code.getMsg());  
                Log.d("haiji", "FingerPrint "+code.getMsg());  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.ERROR, false);//失败回调  
//                JsObjectCommonMethod.showToast("当前设备没有设置密码保护");  
            }  

            @Override  
            public void onEnrollFailed(FingerprintUtil.ResultObj code) {  
                String error = toJSON(code.getCode(), code.getMsg());  
                Log.d("haiji", "FingerPrint "+code.getMsg());  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.ERROR, false);//失败回调  
            }  

            @Override  
            public void onAuthenticationStart(FingerprintUtil.ResultObj code) {  
                String error = toJSON(code.getCode(), code.getMsg());  
                Log.d("haiji", "FingerPrint "+code.getMsg());  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.OK, true);//成功回调  
            }  

            @Override  
            public void onAuthenticationError(int errMsgId, CharSequence errString) {  
                String error = toJSON(errMsgId, errString.toString());  
                Log.d("haiji", "FingerPrint "+error.toString());  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.ERROR, false);//失败回调  
            }  

            @Override  
            public void onAuthenticationFailed(FingerprintUtil.ResultObj code) {  
                String error = toJSON(code.getCode(), code.getMsg());  
                Log.d("haiji", "FingerPrint "+error);  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.ERROR, false);//失败回调  
            }  

            @Override  
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {  
                String error = toJSON(helpMsgId, helpString.toString());  
                Log.d("haiji", "FingerPrint "+error);  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.ERROR, false);//失败回调  
            }  

            @Override  
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {  
                String error = toJSON(FingerprintUtil.FINGETSUCCESS, "指纹识别成功");  
                Log.d("haiji", "FingerPrint "+error);  
                JSUtil.execCallback(pWebview, callbackId, error, JSUtil.OK, false);//成功回调  
            }  
        });  
    }  

    /**  
     * 取消指纹识别  
     * @param pWebview  
     * @param array  
     */  
    public void calcelFingerPrint(final IWebview pWebview, JSONArray array){  
        Log.d("haiji", "FingerPrint 取消指纹识别");  
        FingerprintUtil.cancelFingerPrint();  
    }

下面是js中的代码

/**  
             * 指纹识别检测  
             * @param param  
             */  
            checkFingerPrint:function(successCbk,errorCbk){  
                var success = typeof successCbk !== 'function'? null:function(args){  
                        successCbk(args);  
                    }  
                var fail = typeof errorCbk !== 'function'?null:function(code){  
                        errorCbk(code);  
                    }  
                callbackID = BRIGE.callbackId(success,fail);  
                BRIGE.exec(_CLASS_NAME, "checkFingerPrint", [callbackID,""]);  
            },  
            /**  
             * 开始进行指纹识别  
             * @param param  
             */  
            callFingerPrint:function(successCbk,errorCbk){  
                var success = typeof successCbk !== 'function'? null:function(args){  
                        successCbk(args);  
                    }  
                var fail = typeof errorCbk !== 'function'?null:function(code){  
                        errorCbk(code);  
                    }  
                callbackID = BRIGE.callbackId(success,fail);  
                BRIGE.exec(_CLASS_NAME, "callFingerPrint", [callbackID,""]);  
            },  
            /**  
             * 取消指纹识别  
             * @param param  
             */  
            calcelFingerPrint:function(){  
                BRIGE.exec(_CLASS_NAME, "calcelFingerPrint", []);  
            },
7 关注 分享
DCloud_heavensoft lhyh Trust 奔跑的蘑菇 1***@qq.com Neil_HL 7***@qq.com

要回复文章请先登录注册

江户川林柯南

江户川林柯南

回复 a***@lechuangnet.com :
红米note3有指纹识别模块,且也开启了指纹识别,不过目前已经确认是兼容性BUG,此代码只有在Android6.0以上的设备才有用,Android6.0以下的设备均是各大厂商自己封装的接口,不得不说,Google在Android在指纹这一块的进度被ios远远甩在了身后
2017-12-26 10:00
a***@lechuangnet.com

a***@lechuangnet.com (作者)

回复 江户川林柯南 :
指纹识别的前提是,有指纹识别模块,设备开启了指纹识别
2017-12-25 17:18
江户川林柯南

江户川林柯南

红米note 3测试了一下,提示:当前设备不支持指纹,指纹识别在安卓上面存在着兼容性的问题是吗?
2017-12-07 10:16