本例參考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類型,可以定義為:
[html]
<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>
<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示例類似:
[java]
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);
}
}
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.
本例下載:http://up.aiwalls.com/2012/0517/20120517093609938.zip
摘自 引路蜂移動軟件