由于工作关系,需要做智能卡(JavaCard Applet)和Android NFC方面的开发。

NFC主要有3种模式:读卡器模式, 卡模拟模式, 点到点模式。

本文主要参考了Android官方CardEmulation例子,描述如何实现HCE(主机卡模拟)。

开发前提条件:需要1部支持NFC的手机和一部非接读写器

首先使用AS创建一个工程.

在AndroidManifest.xml文件中添加以下代码

描述所需硬件特性

<uses-feature android:name="android.hardware.nfc.hce" android:required="true" />
描述应用所需的权限
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.INTERNET"/>

添加Service

<service android:name=".CardService"
    android:exported="true"
    android:permission="android.permission.BIND_NFC_SERVICE"><!-- Intent filter indicating that we support card emulation. -->
    <intent-filter><action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE"/><category android:name="android.intent.category.DEFAULT"/></intent-filter><!-- Required XML configuration file, listing the AIDs that we are emulating cards
         for. This defines what protocols our card emulation service supports. -->
    <meta-data android:name="android.nfc.cardemulation.host_apdu_service"
        android:resource="@xml/aid_list"/>
</service>

aid_list.xml文件

<host-apdu-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/service_name"
    android:requireDeviceUnlock="false"><!--
    If category="payment" is used for any aid-groups, you must also add an android:apduServiceBanner
    attribute above, like so:

    android:apduServiceBanner="@drawable/settings_banner"

     apduServiceBanner should be 260x96 dp. In pixels, that works out to...
       - drawable-xxhdpi: 780x288 px
       - drawable-xhdpi:  520x192 px
       - drawable-hdpi:   390x144 px
       - drawable-mdpi:   260x96  px

    The apduServiceBanner is displayed in the "Tap & Pay" menu in the system Settings app, and
    is only displayed for apps which implement the "payment" AID category.

    Since this sample is implementing a non-standard card type (a loyalty card, specifically), we
    do not need to define a banner.

    Important: category="payment" should only be used for industry-standard payment cards. If you are
        implementing a closed-loop payment system (e.g. stored value cards for a specific merchant
        or transit system), use category="other". This is because only one "payment" card may be
        active at a time, whereas all "other" cards are active simultaneously (subject to AID
        dispatch).
    -->

    <aid-group android:description="@string/card_title" android:category="other"><aid-filter android:name="A00000000386980700"/></aid-group></host-apdu-service>
service代码类
public class CardService extends HostApduService {
/**
 * Called if the connection to the NFC card is lost, in order to let the application know the
 * cause for the disconnection (either a lost link, or another AID being selected by the
 * reader).
 *
 * @param reason Either DEACTIVATION_LINK_LOSS or DEACTIVATION_DESELECTED
 */
@Override
public void onDeactivated(int reason) {Logger.i("onDeactivated reason:" + reason);
}
/**
 * This method will be called when a command APDU has been received from a remote device. A
 * response APDU can be provided directly by returning a byte-array in this method. In general
 * response APDUs must be sent as quickly as possible, given the fact that the user is likely
 * holding his device over an NFC reader when this method is called.
 *
 * <p class="note">If there are multiple services that have registered for the same AIDs in
 * their meta-data entry, you will only get called if the user has explicitly selected your
 * service, either as a default or just for the next tap.
 *
 * <p class="note">This method is running on the main thread of your application. If you
 * cannot return a response APDU immediately, return null and use the {@link
 * #sendResponseApdu(byte[])} method later.
 *
 * @param commandApdu The APDU that received from the remote device
 * @param extras A bundle containing extra data. May be null.
 * @return a byte-array containing the response APDU, or null if no response APDU can be sent
 * at this point.
 */
// BEGIN_INCLUDE(processCommandApdu)
@Override
public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {Logger.i("Received APDU: " + ByteArrayToHexString(commandApdu));byte cla = commandApdu[0];byte ins = commandApdu[1];//
    switch (ins){case (byte)0xA4:select(commandApdu);break;case (byte)0xCA:getMessage(commandApdu);break;case (byte)0xB0:readBinary(commandApdu);break;case (byte)0x5c:getBalance(commandApdu);break;case (byte)0x50:purchaseInit(commandApdu);break;case (byte)0xDC:updateRecord(commandApdu);break;case (byte)0x54:purchase(commandApdu);break;default:}//要么返回GetResponse

    Logger.d("processCommandApdu return null");return null;
}
返回apdu使用方法
sendResponseApdu(responseApdu);
由于迁涉到交通卡结构内容、密钥处理等保密协议,无法公开整个工程。但是整个流程还是比较简单的,无法就是接收到请求,然后调用远程或本地服务进行业务处理,最后返回处理结果的APDU.
使用HCE的最大好处就是不需要使用NFC-SIM或者eSE,不会受制于移动运营商和手机厂商。

Android NFC开发相关推荐

  1. Android NFC开发实战详解

    Android NFC开发实战详解 Android开发实战详解NFC国内第一本AndroidNFC开发书籍带你开启AndroidNFC开发的神秘之旅大综合案例帮助读者快速进入实战角色:WiFi快速连接 ...

  2. android nfc ndef mifareclassic,Android NFC开发-实践篇

    Android NFC开发-实践篇 https://blog..net/_GYG/article/details/72899417 在Android NFC开发-理论篇中,我们了解了在Android中 ...

  3. Android NFC开发-实践篇

    Android NFC开发-实践篇 在Android NFC开发-理论篇中,我们了解了在Android中开发NFC的一些理论知识,这篇我们继续应用我们上一篇学到的知识,实现对NDEF格式标签和Mifa ...

  4. Android NFC开发-理论篇

    Android NFC开发-理论篇 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/CSDN_GYG/article/details/72884849 ...

  5. Android NFC开发详细总结

    Android NFC开发详细总结 Near Field Communication (NFC) 为一短距离无线通信技术,通常有效通讯距离为4厘米以内.NFC工作频率为13.65 兆赫兹,通信速率为1 ...

  6. Android NFC开发概述

    NFC手机相比普通手机来说,有以下3个附加功能:  1.可以当成POS机来用,也就是"读取"模式   2.可以当成一张卡来刷,也就是NFC技术最核心的移动支付功能  3.可以像蓝牙 ...

  7. 《Android NFC开发实战详解》——6.4节Android NFC P2P开发进阶

    本节书摘来自异步社区<Android NFC开发实战详解>一书中的第6章,第6.4节Android NFC P2P开发进阶,作者 赵波,更多章节内容可以访问云栖社区"异步社区&q ...

  8. Android NFC开发实战详解PDF

    最近找书做毕设,有的还要加会员注册什么的,下面是<Android NFC开发详解>和<第一行代码>,自取,不用谢,来波点赞呗 链接:https://pan.baidu.com/ ...

  9. android NFC 开发学习笔记(1)

    由于工作需求,最近在研究android nfc开发,借鉴了很对大神的文章在此记录自己的学习过程: 大家学习android开发建议首选android开发文档,该文档在你下载的sdk中,路径:/sdk/d ...

  10. Android NFC 开发实例

    http://blog.csdn.net/pku_android/article/details/7430788 类: Android应用开发系列教程 Android应用开发技巧2012-04-06 ...

最新文章

  1. sql如何让计算出来的结果百分数显示_图解面试题:如何交换数据?
  2. wallpaper怎么改后缀_腾讯微信视频号怎么引流?腾讯视频号引流有哪些方法?
  3. WinRAR最新版V3.93 破解方法
  4. 美国进入“非结构化”数据分析新时代
  5. C语言的头文件和库文件(函数库)
  6. 一个介绍SAP git-enabled CTS的视频
  7. 数据库怎么选择?终于有人讲明白了
  8. 为什么需要 AtomicInteger 原子操作类?
  9. SAP License:FI-CO集成
  10. golang中字符串的查找方法小结
  11. JavaScript的组成
  12. JavaEE学习14(应用)--用户自动登陆
  13. 今天,你绿坝了没有?
  14. 全网最全诊断梳理《UDS/OBD诊断诊断描述文件CDD》总目录
  15. 将SpringBoot项目打包并部署到服务器
  16. python编程else是什么意思_Python 中的 else详解
  17. 事实表与维度表的区别与关系
  18. GitLab 邮件发送不成功原因
  19. Go语言 -- 关于nil的幺蛾子
  20. 项目管理绝版秘籍——IT项目管理全套127个表格文档

热门文章

  1. find命令的技巧和一些例子
  2. C#调C++生成的dll报0x800736B1错误
  3. 洛谷 - P3810 【模板】三维偏序(陌上花开)(CDQ分治套树状数组)
  4. HihoCoder - 1457 后缀自动机四·重复旋律7(后缀自动机)
  5. 回溯法之避免无用判断 UVA129 Krypton Factor困难的串
  6. Windows下Anaconda3安装及使用教程
  7. HDU4357(数学思维题)
  8. POJ3020深度解析(二分图--最小路径覆盖)
  9. 玩转Google开源C++单元测试框架Google Test系列(gtest)之七 - 深入解析gtest
  10. 高级数据结构与算法 | AVL树 (高度平衡树)