Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6514617
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2014-07-03 21:26:48

本例参考ApiDemos中NFC的ForegoundDispatch来介绍编写Android NFC 的基本步骤,因为手边只有类型的Tag ,需要对ForegoundDispatch的代码做些修改来检测 的类型的NFC Tag,读写其他类型的NFC Tag的基本步骤是一致的。

1. 在Android manifest 文件中申明和NFC相关的权限和功能选项:
权限申明:

最低版本要求,NFC是指Android2.3 (Level 10) 才开始支持的,因此最低版本要求必须指定为10.

如果需要在Android Market上发布,需要指定手机支持NFC 功能。

为Activity申明它支持处理NFC Tag

比如我们的示例Activity 在Manifest 的申明如下:

android:label=”@string/app_name”
android:launchMode=”singleTop”>

< action android:name=”android.intent.action.MAIN” />

< /intent-filter>
< intent-filter>
< action android:name=”android.nfc.action.NDEF_DISCOVERED”/>

< /intent-filter>
< intent-filter>
< action
android:name=”android.nfc.action.TAG_DISCOVERED”
>
< /action>
< category
android:name=”android.intent.category.DEFAULT”
>
< /category>
< /intent-filter>
< !– Add a technology filter –>

< action android:name=”android.nfc.action.TECH_DISCOVERED” />
< /intent-filter>

android:resource=”@xml/filter_nfc”
/>

三种Activity NDEF_DISCOVERED ,TECH_DISCOVERED,TAG_DISCOVERED 指明的先后顺序非常重要, 当Android设备检测到有NFC Tag靠近时,会根据Action申明的顺序给对应的Activity 发送含NFC消息的 Intent.

2. Android NFC 消息发送机制

当Android设备检测到有NFC Tag时,理想的行为是触发最合适的Activity来处理检测到的Tag,这是因为NFC通常是在非常近的距离才起作用(<4m) ,如果此时需要用户来选择合适的应用来处理Tag,很容易断开与Tag之间的通信。因此你需要选择合适的Intent filter 只处理你想读写的Tag类型。

Android系统支持两种NFC消息发送机制:Intent 发送机制和前台Activity 消息发送机制。

Intent 发送机制 当系统检测到Tag时,Android系统提供manifest 中定义的Intent filter 来选择合适的Activity来处理对应的Tag,当有多个Activity可以处理对应的Tag类型时,则会显示Activity选择窗口由用户选择:

前台Activity 消息发送机制 允许一个在前台运行的Activity在读写NFC Tag 具有优先权,此时如果Android检测到有NFC Tag ,如果前台允许的Activity可以处理该种类型的Tag则该Activity具有优先权,而不出现Activity 选择窗口。

这两种方法基本上都是使用Intent-filter 来指明Activity可以处理的Tag类型,一个是使用Android的Manifest 来说明,一个是通过代码来申明。

下图显示当Android检测到Tag,消息发送的优先级:

本例 NFCDemoActivity 支持两种NFC消息发送机制,上面的XML指明了Intent 消息发送机制,其中

android:resource=”@xml/filter_nfc”
/>

的filter_nfc 指明了支持处理的NFC Tag类型,filter_nfc.xml 定义如下:


< !– capture anything using NfcF –>

< tech>android.nfc.tech.NfcA
< tech>android.nfc.tech.MifareClassic
< tech>android.nfc.tech.MifareUltralight

因为我只有MifareClassic 类型的Tag,所以只定义了MifareClassic相关的Tag类型,如果你可以处理所有Android支持的NFC类型,可以定义为:

  1. <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">  
  2.     <tech-list>  
  3.         <tech>android.nfc.tech.IsoDeptech>  
  4.         <tech>android.nfc.tech.NfcAtech>  
  5.         <tech>android.nfc.tech.NfcBtech>  
  6.         <tech>android.nfc.tech.NfcFtech>  
  7.         <tech>android.nfc.tech.NfcVtech>  
  8.         <tech>android.nfc.tech.Ndeftech>  
  9.         <tech>android.nfc.tech.NdefFormatabletech>  
  10.         <tech>android.nfc.tech.MifareClassictech>  
  11.         <tech>android.nfc.tech.MifareUltralighttech>  
  12.     tech-list>  
  13. resources>  

有了这个Manifest中的申明,当Android检测到有Tag时,会显示Activity选择窗口,如上图中的Reading Example。

当NFCDemoActiviy在前台运行时,我们希望只有它来处理Mifare 类型的Tag,此时可以使用前台消息发送机制,下面的代码基本和ApiDemos中的NFC示例类似:

  1. public class NFCDemoActivity extends Activity {  
  2.  private NfcAdapter mAdapter;  
  3.  private PendingIntent mPendingIntent;  
  4.  private IntentFilter[] mFilters;  
  5.  private String[][] mTechLists;  
  6.  private TextView mText;  
  7.  private int mCount = 0;  
  8.    
  9.  @Override  
  10.  public void onCreate(Bundle savedState) {  
  11.  super.onCreate(savedState);  
  12.    
  13.  setContentView(R.layout.foreground_dispatch);  
  14.  mText = (TextView) findViewById(R.id.text);  
  15.  mText.setText("Scan a tag");  
  16.    
  17.  mAdapter = NfcAdapter.getDefaultAdapter(this);  
  18.    
  19.  // Create a generic PendingIntent that will be deliver  
  20.  // to this activity. The NFC stack  
  21.  // will fill in the intent with the details of the  
  22.  //discovered tag before delivering to  
  23.  // this activity.  
  24.  mPendingIntent = PendingIntent.getActivity(this0,  
  25.  new Intent(this,  
  26.     getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);  
  27.    
  28.  // Setup an intent filter for all MIME based dispatches  
  29.  IntentFilter ndef  
  30.     = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);  
  31.  try {  
  32.  ndef.addDataType("*/*");  
  33.  } catch (MalformedMimeTypeException e) {  
  34.  throw new RuntimeException("fail", e);  
  35.  }  
  36.  mFilters = new IntentFilter[] {  
  37.  ndef,  
  38.  };  
  39.    
  40.  // Setup a tech list for all MifareClassic tags  
  41.  mTechLists  
  42.   = new String[][] { new String[] { MifareClassic.class.getName() } };  
  43.  }  
  44.    
  45.  @Override  
  46.  public void onResume() {  
  47.  super.onResume();  
  48.  mAdapter.enableForegroundDispatch(this,  
  49.      mPendingIntent, mFilters, mTechLists);  
  50.  }  
  51.    
  52.  @Override  
  53.  public void onNewIntent(Intent intent) {  
  54.  Log.i("Foreground dispatch",  
  55.      "Discovered tag with intent: " + intent);  
  56.  mText.setText("Discovered tag " +  
  57.        ++mCount + " with intent: " + intent);  
  58.  }  
  59.    
  60.  @Override  
  61.  public void onPause() {  
  62.  super.onPause();  
  63.  mAdapter.disableForegroundDispatch(this);  
  64.  }  
  65. }  

只改了一行,将处理NfcF类型的Tag 改为处理MifareClassic 类型的NFC Tag。

mTechLists = new String[][] { new String[] { MifareClassic.class.getName() } };

运行该示例,每靠近一次Tag,计数加1.

本例

阅读(3894) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~