【NFC在android中的应用API】
16lz
2021-01-23
Android 2.3 NFC简介
Android 2.3加入了NFC(近场通讯)的支持。官网developer.android.com的英文介绍如下: 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:
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。 功能是少一点,但从无到有的第一步总是不容易的。从架构上看,至少有以下几部分的改动:
NFC的API在android.nfc的包中提供,这个包主要提供三个大类,其中:NfcAdapter描述的就是手机中的NFC硬件,Android 2.3中可以暂时理解为电子标签扫描器。电子标签和扫描器中的消息通过NdefMessage来表示,这个类很简单,只是封装了NdefRecord。每个NdefMessage中可以包含多个NdefRecord,通过类NdefMessage的方法getRecords() 可以查询到消息的所有NdefRecord。NdefRecord才是信息的真正载体,正确理解这个类是理解NFC技术的一个重点。NFC的技术规范[url=link.php?url=http://www.nfc-forum.org%2Fspecs%2Fspec_list%2F]http://www.nfc-forum.org/specs/spec_list/[/url]是理解这个类不可或缺的资料。 应用程序的编程思路是:
|
更多相关文章
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- Android下实现手机验证码
- android消息处理机制学习(一)-Handler,Message,MessageQueue,Loope
- Android获得手机信息
- android设定手机的显示模式,横竖屏,是否全屏
- Android复制手机号码到剪切板并调起打电话功能
- android 的分享功能
- Android 实现自动接听和挂断电话功能