起步软件技术论坛
搜索
 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2359|回复: 3

[分享] android端指纹验证初体验

  [复制链接]
发表于 2017-2-22 11:06:18 | 显示全部楼层 |阅读模式
本帖最后由 暗夜的忧伤 于 2017-2-22 11:06 编辑

由于项目中要用到指纹验证,但是官方提供的插件中只有ios版的,所以只能自食其力了;查了网上相关的资料(http://www.cnblogs.com/Fndroid/p/5204986.html)和官方提供的插件开发文档(http://docs.wex5.com/cordova-plugin-voicemanager-android/),有了一点小成绩,现做下记录:

第一步:在UI2下新建应用(touchID),并在Native中新建本地APP(指纹验证)
QQ20170222-102528@2x.png

第二步:打开androidStudio开发工具,打开已存在的应用(详见http://docs.wex5.com/cordova-plugin-voicemanager-android/)
QQ20170222-102838@2x.png


第三步:在java目录下新建touchID.java文件

QQ20170222-103100@2x.png

内容如下:
  1. package com.eking.cordova.android.plugin.touchID;

  2. import org.apache.cordova.CallbackContext;
  3. import org.apache.cordova.CordovaInterface;
  4. import org.apache.cordova.CordovaPlugin;
  5. import org.apache.cordova.CordovaWebView;
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;

  9. import android.Manifest;
  10. import android.app.Activity;

  11. import android.content.pm.PackageManager;
  12. import android.hardware.fingerprint.FingerprintManager;

  13. import android.os.CancellationSignal;
  14. import android.support.v4.app.ActivityCompat;
  15. import android.support.v4.content.PermissionChecker;
  16. import android.util.Log;

  17. import static android.content.Context.FINGERPRINT_SERVICE;

  18. /**
  19. * Created by chy-lv on 2017/2/7.
  20. */

  21. public class TouchID extends CordovaPlugin {
  22.     private int checkFlag = -1;
  23.     private CancellationSignal mCancellationSignal = new CancellationSignal();
  24.     private boolean HAS_FINGERPRINT_API = true;

  25.     @Override
  26.     public void initialize(CordovaInterface cordova, CordovaWebView webView) {
  27.         try {
  28.             Class.forName("android.hardware.fingerprint.FingerprintManager");//是否有需要的类
  29.             HAS_FINGERPRINT_API = true;
  30.         } catch (ClassNotFoundException e) {
  31.             HAS_FINGERPRINT_API = false;
  32.             e.printStackTrace();
  33.         }
  34.         super.initialize(cordova, webView);
  35.     }

  36.     @Override
  37.     public boolean execute(String action, JSONArray args,
  38.                            CallbackContext callbackContext) throws JSONException {
  39.         if ("checkAuthenticate".endsWith(action)) {//判断手机是否支持指纹识别
  40.             checkAuthenticate(callbackContext);
  41.         } else if ("getAuthenticate".endsWith(action)) {// 调用指纹模块
  42.             getAuthenticate(callbackContext);
  43.         } else if ("checkTouchId".endsWith(action)) {// 判断指纹是否验证通过
  44.             callbackContext.success(checkFlag);
  45.         } else if ("stopCheckTouchID".endsWith(action)) {// 取消指纹验证
  46.             mCancellationSignal.cancel();
  47.         }
  48.         return super.execute(action, args, callbackContext);
  49.     }

  50.     public void checkAuthenticate(CallbackContext callbackContext) throws JSONException {//判断手机是否支持指纹识别
  51.         if (HAS_FINGERPRINT_API) {
  52.             Activity activty = cordova.getActivity();
  53.             FingerprintManager manager = (FingerprintManager) activty
  54.                     .getSystemService(FINGERPRINT_SERVICE);
  55.             int hasWriteContactsPermission = PermissionChecker.checkSelfPermission(
  56.                     activty, Manifest.permission.USE_FINGERPRINT);
  57.             if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {// 判断是否拥有Manifest.permission.USE_FINGERPRINT系统权限
  58.                 callbackContext.error("该机型指纹功能朕无能为力");
  59.             } else {
  60.                 JSONObject json = new JSONObject();
  61.                 if (manager.isHardwareDetected()
  62.                         && manager.hasEnrolledFingerprints()) {// 有硬件支持并设置了指纹解锁
  63.                     json.put("code",1);
  64.                     json.put("msg","指纹功能可以使用");
  65.                 } else {
  66.                     json.put("code", 0);
  67.                     json.put("msg", "请在手机系统设置中录入至少一个指纹");

  68.                 }
  69.                 callbackContext.success(json);
  70.             }
  71.         } else {
  72.             callbackContext.error("手机不支持指纹识别功能");
  73.         }
  74.     }

  75.     public void getAuthenticate(CallbackContext callbackContext) {
  76.         Activity activty = cordova.getActivity();
  77.         FingerprintManager manager = (FingerprintManager) activty
  78.                 .getSystemService(FINGERPRINT_SERVICE);
  79.         int hasWriteContactsPermission = PermissionChecker.checkSelfPermission(
  80.                 activty, Manifest.permission.USE_FINGERPRINT);
  81.         if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {// 判断是否拥有android.permission.USE_FINGERPRINT系统权限
  82.             callbackContext.error("没有系统权限");
  83.         } else {
  84.             if (manager.isHardwareDetected()
  85.                     && manager.hasEnrolledFingerprints()) {// 有硬件支持并设置了指纹解锁
  86.                 touchCallBack callback = new touchCallBack();
  87.                 if (mCancellationSignal.isCanceled()) {
  88.                     mCancellationSignal = new CancellationSignal();
  89.                 }
  90.                 manager.authenticate(null, mCancellationSignal, 0, callback,
  91.                         null);
  92.                 checkFlag = -1;
  93.                 callbackContext.success("指纹模块调用成功");
  94.             } else {
  95.                 callbackContext.error("手机不支持");
  96.             }
  97.         }

  98.     }

  99.     public class touchCallBack extends
  100.             FingerprintManager.AuthenticationCallback {

  101.         // 当出现错误的时候回调此函数,比如多次尝试都失败了的时候,errString是错误信息
  102.         @Override
  103.         public void onAuthenticationError(int errMsgId, CharSequence errString) {
  104.             checkFlag = 2;
  105.         }

  106.         // 当指纹验证失败的时候会回调此函数,失败之后允许多次尝试,失败次数过多会停止响应一段时间然后再停止sensor的工作
  107.         @Override
  108.         public void onAuthenticationFailed() {
  109.             checkFlag = 0;
  110.         }

  111.         @Override
  112.         public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
  113.             Log.d("touchID", "onAuthenticationHelp: " + helpString);
  114.         }

  115.         // 当验证的指纹成功时会回调此函数,然后不再监听指纹sensor
  116.         @Override
  117.         public void onAuthenticationSucceeded(
  118.                 FingerprintManager.AuthenticationResult result) {
  119.             checkFlag = 1;
  120.         }
  121.     }
  122. }
复制代码




评分

参与人数 2威望 +70 收起 理由
dzq + 10 很给力!
liangyongfei + 60 很给力!

查看全部评分

 楼主| 发表于 2017-2-22 11:06:20 | 显示全部楼层
本帖最后由 暗夜的忧伤 于 2017-2-22 11:10 编辑

看效果 Screenshot_2017-02-22-10-56-54-730_com.eking.touc.png Screenshot_2017-02-22-10-56-59-040_com.eking.touc.png Screenshot_2017-02-22-10-57-04-431_com.eking.touc.png Screenshot_2017-02-22-10-57-08-681_com.eking.touc.png

index.w和index.js文件内容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <div xmlns="http://www.w3.org/1999/xhtml" component="$UI/system/components/justep/window/window" design="device:m;" xid="window" class="window">  
  3.   <div component="$UI/system/components/justep/model/model" xid="model" style="top:138px;left:24px;height:auto;">
  4.   </div>  
  5.   <div component="$UI/system/components/justep/panel/panel"
  6.     class="x-panel x-full" xid="panel1">
  7.       <div class="x-panel-top" xid="top1">
  8.         <div component="$UI/system/components/justep/titleBar/titleBar" title="标题"
  9.           class="x-titlebar">
  10.           <div class="x-titlebar-left">
  11.             </div>  
  12.           <div class="x-titlebar-title">标题</div>  
  13.           <div class="x-titlebar-right reverse">
  14.           </div>
  15.         </div>
  16.       </div>  
  17.     <div class="x-panel-content" xid="content1"><a component="$UI/system/components/justep/button/button" class="btn btn-default" label="启动指纹验证" xid="button1" onClick="button1Click">
  18.    <i xid="i1"></i>
  19.    <span xid="span1">启动指纹验证</span></a>
  20.   <p xid="p1" class="tips"></p></div>
  21.   </div>
  22. </div>
复制代码
  1. define(function(require) {
  2.         var $ = require("jquery");
  3.         var justep = require("$UI/system/lib/justep");
  4.         require("cordova!com.eking.cordova.android.plugin.touchID");
  5.         var Model = function() {
  6.                 this.callParent();
  7.         };

  8.         Model.prototype.button1Click = function(event) {
  9.                 var me = this;
  10.                 function success(result) {
  11.                         if (result.code == 1) {
  12.                                 me.getAuthenticate();
  13.                         } else {
  14.                                 $(me.getElementByXid("p1")).text(result.msg);
  15.                         }
  16.                 }
  17.                 function fail(msg) {
  18.                         $(me.getElementByXid("p1")).text(msg);
  19.                 }
  20.                 navigator.TouchID.checkAuthenticate(success, fail);
  21.         };

  22.         Model.prototype.getAuthenticate = function() {
  23.                 var me = this;
  24.                 function success() {
  25.                         $(me.getElementByXid("p1")).text("请验证指纹");
  26.                         me.checkTouchID();
  27.                 }
  28.                 function fail(msg) {
  29.                         $(me.getElementByXid("p1")).text(msg);
  30.                 }
  31.                 navigator.TouchID.getAuthenticate(success, fail);
  32.         };
  33.         Model.prototype.checkTouchID = function() {
  34.                 function success(result) {
  35.                         if (result == -1) {
  36.                                 $(".tips").text("请验证指纹");
  37.                                 setTimeout(function() {
  38.                                         navigator.TouchID.checkTouchId(success, fali);
  39.                                 }, 100);
  40.                         } else if (result == 0) {
  41.                                 $(".tips").text("验证失败");
  42.                                 setTimeout(function() {
  43.                                         navigator.TouchID.checkTouchId(success, fali);
  44.                                 }, 100);
  45.                         } else if (result == 2) {
  46.                                 $(".tips").text("指纹验证错误次数过多,请稍后重新验证");
  47.                         } else if (result == 1) {
  48.                                 $(".tips").text("验证成功");
  49.                         }

  50.                 }
  51.                 function fali() {
  52.                         alert("调用验证失败");
  53.                 }
  54.                 navigator.TouchID.checkTouchId(success, fali);
  55.         };

  56.         return Model;
  57. });
复制代码

最后当然是整合成wex5用的插件了,具体步骤请参考官方文档:
com.eking.cordova.android.plugin.touchID.zip (4.3 KB, 下载次数: 93)
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-2-22 11:06:19 | 显示全部楼层
本帖最后由 暗夜的忧伤 于 2017-2-22 10:57 编辑

第四步:新建touch.js文件
QQ20170222-103515@2x.png

js内容如下
  1. cordova.define("com.eking.cordova.android.plugin.touchID.touchID", function(require, exports, module) { var cordova = require('cordova'), exec = require('cordova/exec');
  2. module.exports = {
  3.         checkAuthenticate : function(onSuccess, onError) {
  4.                 exec(onSuccess, onError, "touchID", "checkAuthenticate", []);
  5.         },
  6.         checkTouchId : function(onSuccess, onError) {
  7.                 exec(onSuccess, onError, "touchID", "checkTouchId", []);
  8.         },
  9.         getAuthenticate : function(onSuccess, onError) {
  10.                 exec(onSuccess, onError, "touchID", "getAuthenticate", []);
  11.         }
  12. };

  13. });
复制代码
第五步:修cordova_plugins.js
module.exports中添加
  1. {
  2.         "file": "plugins/com.eking.cordova.android.plugin.touchID/www/touch.js",
  3.         "id": "com.eking.cordova.android.plugin.touchID.touchID",
  4.         "pluginId": "com.eking.cordova.android.plugin.touchID",
  5.         "clobbers": [
  6.             "navigator.TouchID"
  7.         ]
  8.     }
复制代码
并在module.exports.metadata中添加
  1. "com.eking.cordova.android.plugin.touchID": "1.0.0"
复制代码

第五步:修改config.xml
QQ20170222-104114@2x.png

增加配置:
  1.   <feature name="touchID">
  2.         <param name="android-package" value="com.eking.cordova.android.plugin.touchID.TouchID" />
  3.     </feature>
复制代码
到此基本开发完毕,但是还有最重要的一点就是android官方是从6.0开始支持指纹识别的,所以我们需要将编译环境修改为6.0以上的版本,修改build.gradle QQ20170222-104817@2x.png

回复 支持 反对

使用道具 举报

50

主题

179

帖子

1088

积分

金牌会员

Rank: 6Rank: 6

积分
1088
QQ
发表于 2017-5-14 18:07:10 | 显示全部楼层
你升级wex5自带的cordova没有?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|X3技术论坛|Justep Inc.    

GMT+8, 2024-5-19 00:29 , Processed in 0.068973 second(s), 31 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表