- 浏览: 40368 次
- 性别:
- 来自: 北京
文章分类
最新评论
转自http://1679554191.iteye.com/blog/1738256
近距离无线通信(NFC)是一种简单的,非触控式的互联技术,可以让消费者简单直观的交换信息,访问内容和服务,在电子消费领域有着广泛的应用。NFC整合非接触式读卡器、非接触式智能卡和点对点(Peer-to-Peer)通信功能,为消费者开创全新便捷生活方式。
1.NFC技术
NFC终端有三种工作模式:
1)主动模式,NFC终端作为一个读卡器,主动发出自己的射频场去识别和读/写别的NFC设备;
2)被动模式,NFC终端可以模拟成一个智能卡被读写,它只能在其它设备发出的射频场中被动响应;
3)双向模式,双方都主动发出射频场来建立点对点的通信。
2.Android NFC架构
android系统为了支持NFC功能,允许应用程序读取标签中的数据,并以NDEF(NFC Data Exchange Format)消息格式进行交互。标签还可以是另外一个设备,即NFC设备工作在卡模拟模式。
NFC软件架构中定义的数据结构:
1)NFC管理器(NFC Manager),提供给应用程序编程接口,作为应用程序访问NFC功能的入口,为了获取NFC适配器实例。
2)NFC适配器(NFC Adapter),提供一切NFC操作,包括NFC设备开关、标签读取、NDEF数据交互、NFC安全访问、点对点通信等。
3)NDEF消息(NDEF Message),是设备和标签之间传递的数据标准封装格式,是由一个或多个NDEF数据记录组成,在应用程序中通过接受ACTION_TAG_DISCOVERED Intent来读取NDEF消息。
4)NDEF记录(NDEF Record),是NDEF数据包的基本组成单元。一个NDEF数据包可以有一个或者多个NDEF记录。
NFC 在android架构中采用Service和Manager基本结构模型,通过Binder和Service通信,如图一所示android基于Binder的IPC的基本模型是基于会话的客户/服务器(C/S)架构的。Android使用内核模块Binder来中转各个进程之间的会话数据,它是一个字符驱动程序,主要通过IOCTL与用户空间的进程交换数据。一次会话是在一个代理Binder对象和服务Binder对象之间进行,可以在同一进程也可以在不同进程。会话是一个同步操作,由代理Binder对象发起请求,一直要等到服务Binder对象将回复传递给代理Binder对象才算完成。
3.NFC Adapter的实现
NFC Adapter主要实现的功能如下:
1)设备初始化以及开关,相关函数方法:
private static synchronized INfcAdapter setupService()
public boolean enable()
public boolean disable()
2)NDEF消息的读写和安全管理链接,相关函数方法:
public void setLocalNdefMessage(NdefMessage message)
public NdefMessage getLocalNdefMessage()
public NdefSecureElement createNdefSecureElementConnection()
3)P2P的后台通信 ,相关函数方法:
public void enableForegroundNdefPush(Activity activity,NdefMessage message)
public void disableForegroundNdefPush(Activity activity)
这些基本函数都是NFC Binder客户端函数,通过Binder IPC调用Service 对应的函数进行通信处理。
4.NFC Server的实现
NFC Service主要实现以下功能:
1)Adapter中的Binder客户端对应Service函数实现,包括INfcTag.Stub,INfcAdapter.Stub等;
2)Service状态管理的消息处理;
3)Java本地接口(JNI)的Native代码的访问接口。
5. NFC HAL实现
NFC HAL(Hardware Abstract Layer)层,包括以下功能:
1)底层RF控制;
2)NFC标签读写处理以及标签模拟;
3)点对点通信;
4)同单总线的SIM卡或者其它安全设施通信;
5)对基于Felica,Mifare和ISO14443的RFID标签做兼容处理。
6.标签识别
在标签识别开始前,确认NFC设备使用正常,可获取NDEF设备。NFC HAL探测到有效距离范围内有标签存在,则读取数据,向NFC Service发送标签识别事件,NFC Service 广播NfcAdapter.ACTION_TAG_DISCOVERED Intent消息,应用程序通过接受该消息即可获取标签数据。
7.NFC简单代码实现
- import java.nio.charset.Charset;
- import android.app.Activity;
- import android.content.Intent;
- import android.nfc.NdefMessage;
- import android.nfc.NdefRecord;
- import android.nfc.NfcAdapter;
- import android.nfc.NfcAdapter.CreateNdefMessageCallback;
- import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
- import android.nfc.NfcEvent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.os.Parcelable;
- import android.provider.Settings;
- import android.text.format.Time;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.TextView;
- import android.widget.Toast;
- public class BeamActivity extends Activity implements
- CreateNdefMessageCallback,
- OnNdefPushCompleteCallback {
- NfcAdapter mNfcAdapter;
- TextView mInfoText;
- private static final int MESSAGE_SENT = 1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_beam);
- mInfoText = (TextView) findViewById(R.id.txtBeam);
- mNfcAdapter = NfcAdapter.getDefaultAdapter(this); // 实例化NFC设备
- if (mNfcAdapter == null) {
- mInfoText.setText("NFC is not available on this device.");
- return;
- } else if (mNfcAdapter != null){
- if(!mNfcAdapter.isEnabled()) {
- mInfoText.setText("请在系统设置中先启用NFC功能!");
- return;
- }else{
- Toast.makeText(this, "启动NFC注册成功...", Toast.LENGTH_SHORT).show();
- mNfcAdapter.setNdefPushMessageCallback(this, this); // 注册NDEF回调消息
- mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
- }
- }
- }
- @Override
- public NdefMessage createNdefMessage(NfcEvent event) {
- Time time = new Time();
- time.setToNow();
- String text = ("Beam me up!nn" +
- "Beam Time: " + time.format("%H:%M:%S"));
- NdefMessage msg = new NdefMessage(
- new NdefRecord[] { createMimeRecord(
- "application/com.example.android.beam", text.getBytes())
- });
- return msg;
- }
- @Override
- public void onNdefPushComplete(NfcEvent arg0) {
- // A handler is needed to send messages to the activity when this
- // callback occurs, because it happens from a binder thread
- mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
- }
- private final Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case MESSAGE_SENT:
- Toast.makeText(getApplicationContext(), "Message sent!",
- Toast.LENGTH_LONG).show();
- break;
- }
- }
- };
- @Override
- public void onResume() {
- super.onResume();
- Toast.makeText(this, "等待接受action信息...", Toast.LENGTH_SHORT).show();
- String action = this.getIntent().getAction();
- if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
- processIntent(getIntent());
- }
- }
- @Override
- public void onNewIntent(Intent intent) {
- // onResume gets called after this to handle the intent
- setIntent(intent);
- }
- /**
- *
- * Parses the NDEF Message from the intent and prints to the TextView
- */
- void processIntent(Intent intent) {
- Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
- NfcAdapter.EXTRA_NDEF_MESSAGES);
- // only one message sent during the beam
- NdefMessage msg = (NdefMessage) rawMsgs[0];
- // NDEF:NFC Data Exchange Format,即NFC数据交换格式
- // record 0 contains the MIME type, record 1 is the AAR, if present
- mInfoText.setText(new String(msg.getRecords()[0].getPayload()));
- }
- /**
- *
- * Creates a custom MIME type encapsulated in an NDEF record
- *
- *
- *
- * @param mimeType
- */
- public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
- byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
- NdefRecord mimeRecord = new NdefRecord(
- NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
- return mimeRecord;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // If NFC is not available, we won't be needing this menu
- if (mNfcAdapter == null) {
- return super.onCreateOptionsMenu(menu);
- }
- // MenuInflater inflater = getMenuInflater();
- //
- // inflater.inflate(menu.CATEGORY_SYSTEM, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- // case R.id.menu_settings:
- case 0:
- Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
- startActivity(intent);
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- }
import java.nio.charset.Charset; import android.app.Activity; import android.content.Intent; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.NfcAdapter.CreateNdefMessageCallback; import android.nfc.NfcAdapter.OnNdefPushCompleteCallback; import android.nfc.NfcEvent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.provider.Settings; import android.text.format.Time; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; public class BeamActivity extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback { NfcAdapter mNfcAdapter; TextView mInfoText; private static final int MESSAGE_SENT = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_beam); mInfoText = (TextView) findViewById(R.id.txtBeam); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); // 实例化NFC设备 if (mNfcAdapter == null) { mInfoText.setText("NFC is not available on this device."); return; } else if (mNfcAdapter != null){ if(!mNfcAdapter.isEnabled()) { mInfoText.setText("请在系统设置中先启用NFC功能!"); return; }else{ Toast.makeText(this, "启动NFC注册成功...", Toast.LENGTH_SHORT).show(); mNfcAdapter.setNdefPushMessageCallback(this, this); // 注册NDEF回调消息 mNfcAdapter.setOnNdefPushCompleteCallback(this, this); } } } @Override public NdefMessage createNdefMessage(NfcEvent event) { Time time = new Time(); time.setToNow(); String text = ("Beam me up!nn" + "Beam Time: " + time.format("%H:%M:%S")); NdefMessage msg = new NdefMessage( new NdefRecord[] { createMimeRecord( "application/com.example.android.beam", text.getBytes()) }); return msg; } @Override public void onNdefPushComplete(NfcEvent arg0) { // A handler is needed to send messages to the activity when this // callback occurs, because it happens from a binder thread mHandler.obtainMessage(MESSAGE_SENT).sendToTarget(); } private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_SENT: Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show(); break; } } }; @Override public void onResume() { super.onResume(); Toast.makeText(this, "等待接受action信息...", Toast.LENGTH_SHORT).show(); String action = this.getIntent().getAction(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { processIntent(getIntent()); } } @Override public void onNewIntent(Intent intent) { // onResume gets called after this to handle the intent setIntent(intent); } /** * * Parses the NDEF Message from the intent and prints to the TextView */ void processIntent(Intent intent) { Parcelable[] rawMsgs = intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES); // only one message sent during the beam NdefMessage msg = (NdefMessage) rawMsgs[0]; // NDEF:NFC Data Exchange Format,即NFC数据交换格式 // record 0 contains the MIME type, record 1 is the AAR, if present mInfoText.setText(new String(msg.getRecords()[0].getPayload())); } /** * * Creates a custom MIME type encapsulated in an NDEF record * * * * @param mimeType */ public NdefRecord createMimeRecord(String mimeType, byte[] payload) { byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII")); NdefRecord mimeRecord = new NdefRecord( NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload); return mimeRecord; } @Override public boolean onCreateOptionsMenu(Menu menu) { // If NFC is not available, we won't be needing this menu if (mNfcAdapter == null) { return super.onCreateOptionsMenu(menu); } // MenuInflater inflater = getMenuInflater(); // // inflater.inflate(menu.CATEGORY_SYSTEM, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // case R.id.menu_settings: case 0: Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } }
Manifest文件的权限配置:
- <uses-permission android:name="android.permission.NFC" />
- <uses-feature android:name="android.hardware.nfc"
- android:required="true" />
<uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
发表评论
-
设置内存卡资源图片
2013-03-12 10:11 487String imageDir = Environment. ... -
android字体过长渐变效果
2013-01-30 15:59 672使用 android:singleLine="t ... -
自定义之checkbox
2013-01-08 09:16 626android中自定义checkbox大小和图片 (转) ... -
Android中Activity,View,Window之间的关系(转)
2012-12-03 09:11 11421.首先来看Activity中的attach()方法,在调用o ... -
Dialog的dismiss和cancel 区别 (转)
2012-12-03 09:13 1858Dialog的dismiss和cancel AlertDial ... -
android应用程序适配在不同的手机上(转)
2012-12-03 09:15 500如何将一个应用程序适 ... -
android 堆栈 清理 (转)
2012-12-03 09:16 677启动任务 当一个activity ... -
Android权限共享UID(转)
2012-12-03 09:16 562共享UID 安装在设备中 ... -
ViewPager+TabHost 实现选项卡页面间滑动(转)
2012-12-04 09:19 929转自http://flycatdeng.iteye.com/b ... -
android扫描商品条形码
2012-12-04 09:19 1005(转自http://marshal.easymorse.com ... -
Android API包名及包的功能的中文介绍
2012-11-30 09:38 986android 包含应用平台和在定义应用程序所用到and ... -
Android 自动解锁 KeyguardManager(键盘管理器)(转)
2012-11-30 09:39 947写一个Activity启动该服务即可,用一个线程开启服务, ... -
Android之Service与IntentService的比较 .
2012-11-30 09:41 688转自http://blog.csdn.net/zhf19890 ... -
AlarmManager的使用
2012-11-30 09:42 679AlarmManager的作用文档中的解释是:在特定的时刻为 ... -
ActivityGroup之生命周期
2012-12-04 09:20 796Activity的生命周期已经 ... -
ActivityGroup之切换activity
2012-12-04 09:20 670前言 在一个主界面中做Activity切换一般都会用T ... -
Android SurviceView的触控和轨迹球事件 .
2012-12-05 14:10 1076SurfaceView 的触控和轨迹球事件 1,改开始什 ... -
home键屏蔽
2012-12-05 14:11 6291. 在activity中加上这段代码(重写onAttache ... -
彻底删除某个APK的方法
2012-12-05 14:11 10851. 连接ADB, 执行adb remount 2. 执行a ... -
android常用命令
2012-12-05 14:11 856首先配置好环境变量,有两种方法 1,在path里面配置 sdk ...
相关推荐
本文总结了基于 Android 平台 NFC 手机读卡器的设计与实现,涵盖了 NFC 技术的原理、NFC 和其他技术的相关性、NFC 手机的用途、NFC 在日本、亚洲、欧洲的概况,并描述了今后 NFC 技术的应用动向。 NFC 技术是近距离...
### 基于Android平台的NFC技术的设计与实现 #### 摘要 本文主要探讨了基于Android平台的近场通信(Near Field Communication, NFC)技术的设计与实现方法。NFC技术作为一种短距离高频无线通信技术,在移动支付、...
综上所述,本项目中的基于Android的NFC电子巡更系统的设计和实现涉及到了多个层面的知识点,包括但不限于Android应用开发、NFC技术应用、数据库管理、后端服务开发以及GIS技术的应用等。这个系统不仅能够在技术上...
近距离无线通信(NFC)是一种简单的、非触控式的互联技术,在电子消费领域有着广泛的应用。Android是当前最流 行的开源移动设备操作系统,NFC技术和Android结合,可以极大促进双方的发展。重点研究了NFC在Android上...
《基于Android和NFC技术的电子秤移动监管系统的实现》 本文主要探讨了一种利用Android技术和NFC(近场通信)技术实现电子秤移动监管系统的具体设计与实施方法。随着电子技术的快速发展,电子秤作弊手段层出不穷,这...
基于Android平台的NFC标签管理系统不仅提供基本的读取、写入、格式化标签功能,还可以实现更高级的应用,如通过NFC标签触发特定的手机模式。这种创新功能提升了用户体验,同时也拓宽了NFC技术在移动设备上的应用范围...
【Android NFC编程技术详解】 ...通过理解NFC的基础知识、Android系统对NFC的支持以及在AndroidManifest中的配置,开发者可以构建出高效、用户友好的NFC应用,实现诸如移动支付、信息交换等多种功能。
腾讯在 2011 年 1 月份文章中提到,基于 Android 的 NFC 应用已经出现,得益于日本在手机刷卡的应用氛围。PayPal 也在 2011 年 7 月份报道中提到,已经尝试了 NFC 移动支付服务。 从技术层面上,NFC 的开发流程可以...
在Android平台上,NFC(Near Field Communication)技术是一种短距离无线通信方式,允许设备之间进行非接触式数据交换。这个“Android通过NFC读取IC卡示例”旨在教会开发者如何利用Android SDK中的NFC API来实现对IC...
文章讨论了基于Android平台和NFC(近场通信)技术的企业一卡通系统设计。文章首先提出了随着移动通信技术的发展,智能手机的普及,将这些技术与传统的企业一卡通系统相结合,可以为企业提供一种全新的管理模式。文章...
本篇文章将深入探讨一个基于Android的NFC实例,帮助初学者理解NFC的基本原理以及如何在Android平台上实现NFC功能,尤其是移动支付的应用。 NFC技术源自RFID,主要工作在13.56MHz频率范围内,最大传输距离约为10厘米...
为了解决这一痛点,本文提出了一种基于Android移动平台和近场通信(NFC)技术的诚信二手车辆系统。该系统旨在提高二手车交易的透明度,增强消费者的信任,并促进二手车市场的规范化。 NFC技术是一种短距离无线通信...
基于Android的NFC移动支付系统设计与实现.pdf
Android NFC手机支付的实现与应用,不但极大地便利了人们的生活,而且对NFC技术的进一步发展起到了重要的推动作用。在技术参考文献中,[1]和[2]分别提供了NFC在校园网上订餐系统和Android移动支付系统设计与实现的...
**NFC技术详解与Android平台...通过学习以上知识点并结合提供的资源,开发者可以快速上手开发Android平台的NFC应用,实现各种创新功能。NFC技术的广泛应用使得移动设备变得更加智能化,为用户带来了便捷的交互体验。
基于NFC技术的Android支付平台的设计与实现.pdf
通过以上讨论,我们可以看到,基于Android平台的NFC读写方法是一个结合硬件交互、系统服务调用和安全策略实施的综合技术。理解并掌握这些知识点,对于开发能够利用NFC功能的Android应用至关重要。随着物联网和智能...