高級 NFC
Tag技術支持工作
表 1. 支持的Tag技術
類 | 描述 |
---|---|
TagTechnology |
這個接口是下面所有tag technology類必須實現的。 |
NfcA |
提供訪問 NFC-A (ISO 14443-3A) 的屬性和 I/O 操作 |
NfcB |
提供訪問 NFC-B (ISO 14443-3B) 的屬性和 I/O 操作 |
NfcF |
提供訪問 NFC-F (JIS 6319-4) 的屬性和 I/O 操作 |
NfcV |
提供訪問 NFC-V (ISO 15693) 的屬性和 I/O 操作 |
IsoDep |
提供訪問 ISO-DEP (ISO 14443-4) 的屬性和 I/O 操作 |
Ndef |
提供對那些被格式化爲NDEF的tag的數據的訪問和其他操作 |
NdefFormatable |
對那些可以被格式化成NDEF格式的tag提供一個格式化的操作 |
表 2. 可選的支持的Tag技術
類 | 描述 |
---|---|
MifareClassic |
如果android設備支持MIFARE,提供對MIFARE Classic目標的屬性和I/O操作。 |
MifareUltralight |
如果android設備支持MIFARE,提供對MIFARE Ultralight目標的屬性和I/O操作。 |
Tag技術工作和 ACTION_TECH_DISCOVERED Intent
- 過濾一個你想要處理的Tag技術的 ACTION_TECH_DISCOVERED intent. 獲取更多信息請參閱 過濾 NFC intents. 一般來說。 當一個NDEF消息不能被映射到MIME類型或者URI上時,Tag發佈系統嘗試啓動一個ACTION_TECH_DISCOVERED intent, 否則如果被掃描到的Tag不包含NDEF數據. 欲瞭解如何確定的更多信息,請參閱Tag發佈系統.
- 當你的應用程序接收到該Intent, 從以圖中獲取Tag對象:
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
- 通過調用在android.nfc.tech包的類中的一個get factory方法,獲取到TagTechnology的一個實例
. 你可以在調用一個get factory方法以前,計算支持Tag技術通過調用getTechList(). 舉例來說, 爲了從一個Tag得到MifareUltralight的一個實例,
請執行下列操作:
MifareUltralight.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
讀取和寫入Tags
package com.example.android.nfc;
import android.nfc.Tag;
import android.nfc.tech.MifareUltralight;
import android.util.Log;
import java.io.IOException;
import java.nio.charset.Charset;
public class MifareUltralightTagTester {
private static final String TAG = MifareUltralightTagTester.class.getSimpleName();
public void writeTag(Tag tag, String tagText) {
MifareUltralight ultralight = MifareUltralight.get(tag);
try {
ultralight.connect();
ultralight.writePage(4, "abcd".getBytes(Charset.forName("US-ASCII")));
ultralight.writePage(5, "efgh".getBytes(Charset.forName("US-ASCII")));
ultralight.writePage(6, "ijkl".getBytes(Charset.forName("US-ASCII")));
ultralight.writePage(7, "mnop".getBytes(Charset.forName("US-ASCII")));
} catch (IOException e) {
Log.e(TAG, "IOException while closing MifareUltralight...", e);
} finally {
try {
ultralight.close();
} catch (IOException e) {
Log.e(TAG, "IOException while closing MifareUltralight...", e);
}
}
}
public String readTag(Tag tag) {
MifareUltralight mifare = MifareUltralight.get(tag);
try {
mifare.connect();
byte[] payload = mifare.readPages(4);
return new String(payload, Charset.forName("US-ASCII"));
} catch (IOException e) {
Log.e(TAG, "IOException while writing MifareUltralight
message...", e);
} finally {
if (mifare != null) {
try {
mifare.close();
}
catch (IOException e) {
Log.e(TAG, "Error closing tag...", e);
}
}
}
return null;
}
}
使用前端發佈系統
- 在你的activity的
onCreate()
方法中添加如下代碼:- 創建一個
PendingIntent
對象, 以便系統可以在它被掃描到時,用tag的細節填充它PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
- 在Intent filters裏聲明你想要處理的Intent,一個tag被檢測到時先檢查前臺發佈系統,如果前臺Activity符合Intent
filter的要求,那麼前臺的Activity的將處理此Intent。如果不符合,前臺發佈系統將Intent轉到Intent發佈系統。如果指定了null的Intent filters,當任意tag被檢測到時,你將收到TAG_DISCOVERED intent。下面的代碼片斷處理所有NDEF_DISCOVERED的MIME類型.
因此請注意你應該只處理你想要的Intent。
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); try { ndef.addDataType("*/*"); /* Handles all MIME based dispatches. You should specify only the ones that you need. */ } catch (MalformedMimeTypeException e) { throw new RuntimeException("fail", e); } intentFiltersArray = new IntentFilter[] {ndef, };
- 設置一個你程序要處理的Tag
technologies的列表,調用Object.class.getName() 方法來獲得你想要支持處理的technology類。
techListsArray = new String[][] { new String[] { NfcF.class.getName() } };
- 創建一個
- 覆蓋下面的方法來打開或關閉前臺發佈系統。比如onPause()和onResume()方法。必須在主線程裏調用enableForegroundDispatch(Activity,
PendingIntent, IntentFilter[], String[][]) 而且Activity在前臺(可以在onResume()裏調用來保證這點)。你也要覆蓋onNewIntent回調來處理得到的NFC
tag數據。
public void onPause() { super.onPause(); mAdapter.disableForegroundDispatch(this); } public void onResume() { super.onResume(); mAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray); } public void onNewIntent(Intent intent) { Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //do something with tagFromIntent }