2013年1月28日星期一

Android NFC 开发教程概述(转载)



Near  Field Communication (NFC) 为一短距离无线通信技术,通常有效通讯距离为4厘米以内。NFC工作频率为13.65 兆赫兹,通信速率为106 kbit/秒到 848kbit/秒。
NFC通信总是由一个发起者(initiator)和一个接受者(target)组成。通常initiator 主动发送电磁场(RF)可以为被动式接受者(passive target)提供电源。其工作的基本原理和收音机类似。正是由于被动式接受者可以通过发起者提供电源,因此target 可以有非常简单的形式,比如标签,卡,sticker 的形式。
NFC 也支持点到点的通信(peer to peer) 此时参与通信的双方都有电源支持。
和其它无线通信方式如Bluetooth相比,NFC 支持的通信带宽和距离要小的多,但是它成本低,如价格标签可能只有几分钱,也不需要配对,搜寻设备等,通信双方可以在靠近的瞬间完成交互。
在Android NFC 应用中,Android手机通常是作为通信中的发起者,也就是作为NFC 的读写器。Android手机也可以模拟作为NFC通信的接受者且从Android 2.3.3起也支持P2P通信。
Android对NFC的支持主要在 android.nfc 和android.nfc.tech 两个包中。
android.nfc 包中主要类如下:

NfcManager 可以用来管理Android设备中指出的所有NFC Adapter,但由于大部分Android设备只支持一个NFC Adapter,可以直接使用getDefaultAapater 来获取系统支持的Adapter。
NfcAdapter 为一NFC Adapter 对象,可以用来定义一个Intent使系统在检测到NFC Tag时通知你定义的Activity,并提供用来注册forground tag 消息发送的方法等。
NdefMessage 和NdefRecord NDEF 为NFC forum 定义的数据格式。


Tag 代表一个被动式Tag对象,可以代表一个标签,卡片,钥匙扣等。当Android设备检测到一个Tag时,会创建一个Tag对象,将其放在Intent对象,然后发送到相应的Activity。

android.nfc.tech 中则定义了可以对Tag进行的读写操作的类,这些类按照其使用的技术类型可以分成不同的类如:NfcA, NfcB, NfcF,以及MifareClassic 等。
常见的Tag为Mifare ,后面的例子将以这种Tag 为例介绍NFC读写方法。

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

1.  在Android manifest 文件中申明和NFC相关的权限和功能选项:
权限申明:
<uses-permission android:name=”android.permission.NFC” />
最低版本要求,NFC是指Android2.3 (Level 10) 才开始支持的,因此最低版本要求必须指定为10.
<uses-sdk android:minSdkVersion=”10″/>
如果需要在Android Market上发布,需要指定手机支持NFC 功能。
<uses-feature android:name=”android.hardware.nfc” android:required=”true” />
为Activity申明它支持处理NFC Tag
比如我们的示例Activity 在Manifest 的申明如下:

<activity android:name=”.NFCDemoActivity”
android:label=”@string/app_name”
android:launchMode=”singleTop”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
<intent-filter>
<action android:name=”android.nfc.action.NDEF_DISCOVERED”/>
<data android:mimeType=”text/plain” />
</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 –>
<intent-filter>
<action android:name=”android.nfc.action.TECH_DISCOVERED” />
</intent-filter>
 
<meta-data android:name=”android.nfc.action.TECH_DISCOVERED”
android:resource=”@xml/filter_nfc”
/>
 
</activity>



三种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 消息发送机制,其中
<meta-data android:name=”android.nfc.action.TECH_DISCOVERED”
android:resource=”@xml/filter_nfc”/>
的filter_nfc 指明了支持处理的NFC Tag类型,filter_nfc.xml 定义如下:

<resources xmlns:xliff=”urn:oasis:names:tc:xliff:document:1.2″>
<!– capture anything using NfcF –>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
 
</tech-list>
 
</resources>


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

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>



有了这个Manifest中的申明,当Android检测到有Tag时,会显示Activity选择窗口,如上图中的Reading Example。
当NFCDemoActiviy在前台运行时,我们希望只有它来处理Mifare 类型的Tag,此时可以使用前台消息发送机制,下面的代码基本和ApiDemos中的NFC示例类似:

public class NFCDemoActivity extends Activity { 
private NfcAdapter mAdapter; 
private PendingIntent mPendingIntent; 
private IntentFilter[] mFilters; 
private String[][] mTechLists; 
private TextView mText; 
private int mCount = 0; 
 
@Override
public void onCreate(Bundle savedState) { 
super.onCreate(savedState); 
 
setContentView(R.layout.foreground_dispatch); 
mText = (TextView) findViewById(R.id.text); 
mText.setText("Scan a tag"); 
 
mAdapter = NfcAdapter.getDefaultAdapter(this); 
 
// Create a generic PendingIntent that will be deliver 
// to this activity. The NFC stack 
// will fill in the intent with the details of the 
//discovered tag before delivering to 
// this activity. 
mPendingIntent = PendingIntent.getActivity(this, 0, 
new Intent(this, 
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); 
 
// Setup an intent filter for all MIME based dispatches 
IntentFilter ndef 
= new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); 
try { 
ndef.addDataType("*/*"); 
} catch (MalformedMimeTypeException e) { 
throw new RuntimeException("fail", e); 
mFilters = new IntentFilter[] { 
ndef, 
}; 
 
// Setup a tech list for all MifareClassic tags 
mTechLists 
= new String[][] { new String[] { MifareClassic.class.getName() } }; 
 
@Override
public void onResume() { 
super.onResume(); 
mAdapter.enableForegroundDispatch(this, 
mPendingIntent, mFilters, mTechLists); 
 
@Override
public void onNewIntent(Intent intent) { 
Log.i("Foreground dispatch", 
"Discovered tag with intent: " + intent); 
mText.setText("Discovered tag " + 
++mCount + " with intent: " + intent); 
 
@Override
public void onPause() { 
super.onPause(); 
mAdapter.disableForegroundDispatch(this); 
}



只改了一行,将处理NfcF类型的Tag 改为处理MifareClassic 类型的NFC Tag。
mTechLists = new String[][] { new String[] { MifareClassic.class.getName() } };
运行该示例,每靠近一次Tag,计数加1。


前面例子介绍了检测,读写NFC TAG开发的一般步骤,本例针对常用的Mifare Tag 具体说明。

Mifare Tag 可以有1K ,2K, 4K,其内存分区大同小异,下图给出了1K字节容量的Tag的内存分布:


数据分为16个区(Sector) ,每个区有4个块(Block) ,每个块可以存放16字节的数据,其大小为16 X 4 X 16 =1024 bytes
每个区最后一个块称为Trailer ,主要用来存放读写该区Block数据的Key ,可以有A,B两个Key,每个Key 长度为6个字节,缺省的Key值一般为全FF或是0. 由 MifareClassic.KEY_DEFAULT 定义。
因此读写Mifare Tag 首先需要有正确的Key值(起到保护的作用),如果鉴权成功
auth = mfc.authenticateSectorWithKeyA(j,
MifareClassic.KEY_DEFAULT);
然后才可以读写该区数据。

本例定义几个Mifare相关的类 MifareClassCard ,MifareSector, MifareBlock 和MifareKey 以方便读写Mifare Tag.
Android 系统来检测到NFC Tag, 将其封装成Tag类,存放到Intent的NfcAdapter.EXTRA_TAG Extra 数据包中,可以使用MifareClassic.get(Tag) 获取对象的 MifareClassic类。


Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 4) Get an instance of the Mifare classic card from this TAG // intent MifareClassic mfc = MifareClassic.get(tagFromIntent); 



下面为读取Mifare card 的主要代码:

// 1) Parse the intent and get the action that triggered this intent 
String action = intent.getAction(); 
// 2) Check if it was triggered by a tag discovered interruption. 
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { 
// 3) Get an instance of the TAG from the NfcAdapter 
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
// 4) Get an instance of the Mifare classic card from this TAG 
// intent 
MifareClassic mfc = MifareClassic.get(tagFromIntent); 
MifareClassCard mifareClassCard=null; 
 
try { // 5.1) Connect to card 
mfc.connect(); 
boolean auth = false; 
// 5.2) and get the number of sectors this card has..and loop 
// thru these sectors 
int secCount = mfc.getSectorCount(); 
mifareClassCard= new MifareClassCard(secCount); 
int bCount = 0; 
int bIndex = 0; 
for (int j = 0; j < secCount; j++) { 
MifareSector mifareSector = new MifareSector(); 
mifareSector.sectorIndex = j; 
// 6.1) authenticate the sector 
auth = mfc.authenticateSectorWithKeyA(j, 
MifareClassic.KEY_DEFAULT); 
mifareSector.authorized = auth; 
if (auth) { 
// 6.2) In each sector - get the block count 
bCount = mfc.getBlockCountInSector(j); 
bCount =Math.min(bCount, MifareSector.BLOCKCOUNT); 
bIndex = mfc.sectorToBlock(j); 
for (int i = 0; i < bCount; i++) { 
 
// 6.3) Read the block 
byte []data = mfc.readBlock(bIndex); 
MifareBlock mifareBlock = new MifareBlock(data); 
mifareBlock.blockIndex = bIndex; 
// 7) Convert the data into a string from Hex 
// format. 
 
bIndex++; 
mifareSector.blocks<i> = mifareBlock; 
 
mifareClassCard.setSector(mifareSector.sectorIndex, 
mifareSector); 
} else { // Authentication failed - Handle it 
 
ArrayList<String> blockData=new ArrayList<String>(); 
int blockIndex=0; 
for(int i=0;i<secCount;i++){ 
 
MifareSector mifareSector=mifareClassCard.getSector(i); 
for(int j=0;j<MifareSector.BLOCKCOUNT;j++){ 
MifareBlock mifareBlock=mifareSector.blocks[j]; 
byte []data=mifareBlock.getData(); 
blockData.add("Block "+ blockIndex++ +" : "+ 
Converter.getHexString(data, data.length)); 
String []contents=new String[blockData.size()]; 
blockData.toArray(contents); 
setListAdapter(new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, contents)); 
getListView().setTextFilterEnabled(true); 
 
} catch (IOException e) { 
Log.e(TAG, e.getLocalizedMessage()); 
showAlert(3); 
}finally{ 
 
if(mifareClassCard!=null){ 
mifareClassCard.debugPrint(); 
}
 
</i>

没有评论:

发表评论