下面是步骤:
1.获取管理权限:这个开发包里已经集成了,只需要调用一句代码就可以搞定,这个还是挺方便的。
PermissionUtil.grantNeedPermission(this);
2.打开NFC,并监听NFC事件,这段如果不懂,直接在网上搜安卓使用NFC就可以看到介绍
private NfcAdapter mAdapter = null; // 定义NFC适配器
mAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());
pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);//.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
mTechLists = new String[][]{new String[]{NfcB.class.getName()}, new String[]{NfcA.class.getName()}};
if (mAdapter != null && !mAdapter.isEnabled()) {Toast.makeText(this, "NFC尚未开启", Toast.LENGTH_SHORT).show();
}
3.因为我的NFC事件是在子线程中的,所以new了一个handle
private static class MyHandler extends Handler {private final WeakReference<IDCardScannerActivity> mActivity;public MyHandler(IDCardScannerActivity activity) {mActivity = new WeakReference<IDCardScannerActivity>(activity);}@Overridepublic void handleMessage(Message msg) {System.out.println(msg);if (mActivity.get() == null) {return;}if (msg.what == 1002)throw new RuntimeException();else {try {mActivity.get().todo(msg);} catch (FileNotFoundException e) {e.printStackTrace();}}}}
4.初始化类:
private OTGReadCardAPI ReadCardAPI; // 定义读取类ReadCardAPI = new OTGReadCardAPI(getApplicationContext(), this, false); // 初始化类,第三个是回调地址,第三个直接传false就可以了// 设置服务器地址及端口
ArrayList<Serverinfo> twoCardServerlist = new ArrayList<Serverinfo>();
twoCardServerlist.add(new Serverinfo("id.yzfuture.cn", 8848));
ReadCardAPI.setServerInfo(twoCardServerlist, null, bTestServer);
5.当手机检测到芯片靠近的时候,会回调onNewIntent,在这个里面直接 读取即可。但是为避免出现问题,我直接发送消息到 Handle里面处理。
@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);inintent = intent;mHandler.sendEmptyMessageDelayed(1, 0);}
6.在MyHandler 中会调到todo里面,这里面是具体的操作
public void todo(Message msg) throws FileNotFoundException {int tt = 0;m_szProcess = "";process.setText("");m_szAppKey = sharedPreferences.getString("AppKey", "");int nnum = ReadCardAPI.GetAppKeyUseNum(m_szAppKey);if (nnum <= 0) nnum = 0;numtxt.setText(String.valueOf(nnum));if (m_szAppKey.isEmpty() || nnum<=0){regeditAppkey(true);}appkeyTxt.setText(m_szAppKey);if (!m_szAppKey.isEmpty() && nnum>0){if (msg.what == 1 || msg.what==1005){if (msg.what == 1005){inintent = null;}tt = ReadCardAPI.NfcReadCard(m_szAppKey, null, inintent, eCardType.eTwoGeneralCard, "", m_bAuthon);}if (m_bshow){userInfo.setText(m_szUserInfo);}if (tt == 41){if (m_berror){setdialog("读卡失败!" + ReadCardAPI.GetErrorInfo());}else{int nerr = ReadCardAPI.GetErrorCode();if (nerr==-24997){setdialog("此设备没有解码权限!");}else setdialog("读卡失败!");}edtype.setText("");edname.setText("");edenname.setText("");edid.setText("");addTrue.setText("");sexTrue.setText("");mzTrue.setText("");jgTrue.setText("");birthTrue.setText("");yxqTrue.setText("");gjTrue.setText("");process.setText("");}if (tt == 90){if (ReadCardAPI.GetTwoCardInfo().arrTwoIdPhoto != null) {idimg.setBackground(new BitmapDrawable(Bytes2Bimap(ReadCardAPI.GetTwoCardInfo().arrTwoIdPhoto)));}if (ReadCardAPI.GetTwoCardInfo().szTwoType.equals("J")) {edtype.setText("港澳居民居住证");othernoTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoOtherNO);signTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoSignNum);} else {edtype.setText("居民二代证");othernoTrue.setText("");signTrue.setText("");}edid.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdNo.trim());edname.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdName.trim());addTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdAddress.trim());sexTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdSex.trim());mzTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdNation.trim() + "族");gjTrue.setText("中国");jgTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdSignedDepartment.trim());birthTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(6, 8));String startDate = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(6, 8);String endDate = "";int nlen = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.indexOf("长期");if (nlen != -1) {endDate = "长期";} else {endDate = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(6, 8);}yxqTrue.setText(startDate + "~" + endDate);//process.setText("100");ReadCardAPI.release();}}}
到此就读取完了,当然 了第一次使用可以调用开发包自带的接口进行注册,注册也简单,只需要填写昵称和手机号,然后填入验证码就可以了。
下面是我工程的完整代码链接,我打包全部上传到网盘里面了,直接 下载下来用android studio打开就可以用了。刚开始学习,有不好的地方请见谅。
https://www.yzfuture.cn/views/service/index.html?ap=download-center
https://github.com/yzfuture/yzfuture
由于CSDN积分规则,需要的下载分越来越贵,特地上传到了上面两个地方可以下载到windows/linux/android三个平台的SDK及demo和源码,免费送大家分享,欢迎一起学习探讨