android 5.0 NFC支持

263 篇文章 2 订阅
164 篇文章 0 订阅
1.  Near Field Communications (NFC)

Android 2.3 includes an NFC stack and framework API that lets developers read NDEF tags that are discovered as a user touches an NFC-enabled device to tag elements embedded in stickers, smart posters, and even other devices.

The platform provides the underlying NFC services that work with the device hardware to discover tags when they come into range. On discovering a tag, the platform notifies applications by broadcasting an Intent, appending the tag's NDEF messages to the Intent as extras. Applications can create Intent filters to recognize and handle targeted tags and messages. For example, after receiving a tag by Intent, applications extract the NDEF messages, store them, alert the user, or handle them in other ways.

The NFC API is available in the android.nfc package. The key classes are:

  • NfcAdapter, which represents the NFC hardware on the device.
  • NdefMessage, which represents an NDEF data message, the standard format in which "records" carrying data are transmitted between devices and tags. Applications can receive these messages from ACTION_TAG_DISCOVERED Intents.
  • NdefRecord, delivered in an NdefMessage, which describes the type of data being shared and carries the data itself.

NFC communication relies on wireless technology in the device hardware, so support for the platform's NFC features on specific devices is determined by their manufacturers. To determine the NFC support on the current device, applications can call isEnabled() to query the NfcAdapter. The NFC API is always present, however, regardless of underlying hardware support.

To use the NFC API, applications must request permission from the user by declaring <uses-permission android:name="android.permission.NFC"> in their manifest files.

Additionally, developers can request filtering on Android Market, such that their applications are not discoverable to users whose devices do not support NFC. To request filtering, add <uses-feature android:name="android.hardware.nfc" android:required="true"> to the application's manifest.


NFC的应用场景有很多,但Android 2.3目前API只提供了电子标签阅读器的功能。相信随着Android的版本升级,后续会增加很多应用场景和API。

功能是少一点,但从无到有的第一步总是不容易的。从架构上看,至少有以下几部分的改动:

  • Android Framework层,为应用开发增加了相关API(即英文中的framework API)。
  • 协议层,增加NFC的协议栈(即英文中的NFC Stack)。
  • 硬件适配层,为硬件生产商提供适配接口,此层应该与NFC硬件的Linux驱动通讯(实现方式待验证)。

工作流程是当支持NFC功能的手机或其他电子终端(后面简称手机)在用户开启NFC功能的时候,如果手机内置的NFC扫描器(相当于类NfcAdapter的功能)扫描到电子标签后,就会向相关用户程序发送ACTION_TAG_DISCOVERED的Intent,Itent的extras架构中会包含NDEF(NFC Data Exchange Format)消息。如何处理此NDEF消息,就是用户程序的事情了。

NFC的API在android.nfc的包中提供,这个包主要提供三个大类,其中:NfcAdapter描述的就是手机中的NFC硬件,Android 2.3中可以暂时理解为电子标签扫描器。电子标签和扫描器中的消息通过NdefMessage来表示,这个类很简单,只是封装了NdefRecord。每个NdefMessage中可以包含多个NdefRecord,通过类NdefMessage的方法getRecords()可以查询到消息的所有NdefRecord。NdefRecord才是信息的真正载体,正确理解这个类是理解NFC技术的一个重点。NFC的技术规范http://www.nfc-forum.org/specs/spec_list/是理解这个类不可或缺的资料。

应用程序的编程思路是:

  1. 通过android.nfc.NfcAdapter.getDefaultAdapter()取得手机的objNfcAdapter
  2. 通过objNfcAdapter.isEnabled()查询该手机是否支持NFC
  3. 如果手机支持NFC,就申请接收ACTION_TAG_DISCOVERED的Intent
  4. 如果接收到ACTION_TAG_DISCOVERED,就提取NdefMessage,并在此基础上进而提取NdefRecord,整个是一个消息解析过程

2.Android中对NFC的实现代码分布在如下几个地方:

./frameworks/base/core/java/android/nfc/ -实现了NFC的应用接口

./external/libnfc-nci  -HAL shared libary

./external/libnfc-nxp -是用C语言编写的函数库,编译生成libnfc和libnfc_ndef两个函数库。libnfc 是一个主要的库,实现了NFC Stack的大部分功能,主要供NFC的服务进程调用。libnfc_ndef是一个很小的函数库,主要是实现NDEF消息的解析,供Framework 调用。

./packages/apps/nfc - 是一个类似于电话本的应用程序,但这个程序在手机开机后自动启动,一直运行,是作为NFC的服务进程存在的。是NFC架构中的核心。

图示如下:

Android NFC架构

附录:

NFC Service的说明

NFC: Move NFC service implementation out of system_server.

NFC service is now an application service in packages/apps/Nfc.

NFC service is registered through ServiceManager.addService(), and the proxy
object NfcAdapter obtains a handle to it through ServiceManager.getService().

**Important** Had to add new symbols AID_NFC / NFC_UID / android.uid.nfc and
modify service_manager.c, Process.java and PackageManagerService.java in order
to force the com.android.nfc process to take a fixed uid, so that it can use
ServiceManager.addService().

Most of the JNI has moved to packages/apps/Nfc/jni. However NdefRecord and
NdefMessage require some in-process native code, so android_com_NdefMessage.cpp
and android_com_NdefRecord.cpp stay in frameworks/base/core/jni. They link to
a very small library libnfc_ndef.so that implements NDEF message parsing. This
has been added to core.mk so all devices (even without NFC hardware) can work
with NDEF data.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值