一、NFC是什麼?
近距離無線通訊技術,這個技術由非接觸式射頻識別(RFID)演變而來,由飛利浦半導體(現恩智浦半導體公司)、諾基亞和索尼共同研製開發,其基礎是RFID及互連技術。近場通信(Near Field Communication,NFC)是一種短距高頻的無線電技術,在13.56MHz頻率運行於20釐米距離內。其傳輸速度有106 Kbit/秒、212 Kbit/秒或者424 Kbit/秒三種。目前近場通信已通過成爲ISO/IEC IS 18092國際標準、ECMA-340標準與ETSI TS 102 190標準。NFC採用主動和被動兩種讀取模式。
NFC通信模式主要有以下幾種(信息來源):
1.讀卡器模式(Reader/writer mode):
作爲非接觸讀卡器使用,比如從海報或者展覽信息電子標籤上讀取相關信息。亦可實現NFC手機之間的數據交換,對於企業環境的中的文件共享,或者對於多玩家的遊戲應用,都將帶來諸多的便利。
2. 點對點模式(P2Pmode):
此模式和紅外線差不多,可用於數據交換,只是傳輸距離較短,傳輸創建速度較快,傳輸速度也快些,功耗低(藍牙也類似)。將兩個具備NFC功能的設備無線鏈接,能實現數據點對點傳輸,如下載音樂、交換圖片或者同步設備地址薄。因此通過NFC,多個設備如數位相機、PDA、計算機和手機之間都可以交換資料或者服務。
3.卡模式(Cardemulation):
這個模式其實就是相當於一張採用RFID技術的IC卡,可以替代大量的IC卡(包括信用卡)使用的場合,如商場刷卡、公交卡、門禁管制,車票,門票等等。此種方式下,有一個極大的優點,那就是卡片通過非接觸讀卡器的 RF 域來供電,即使寄主設備(如手機)沒電也可以工作。
二、如何使用與集成到項目?
1、首先在manifests裏面聲明NFC和添加相應的權限;
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<uses-permission android:name="android.permission.NFC" />
2、在Activity標籤中聲明識別NFC標籤;
<activity android:name=".Activity.Main.NFCActivity">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
3、封裝NFC的讀寫,方便調用;
public class NfcUtils {
//nfc
public static NfcAdapter mNfcAdapter;
public static IntentFilter[] mIntentFilter = null;
public static PendingIntent mPendingIntent = null;
public static String[][] mTechList = null;
/**
* 構造函數,用於初始化nfc
*/
public NfcUtils(Activity activity) {
mNfcAdapter = NfcCheck(activity);
NfcInit(activity);
}
/**
* 檢查NFC是否打開
*/
public static NfcAdapter NfcCheck(Activity activity) {
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (mNfcAdapter == null) {
return null;
} else {
if (!mNfcAdapter.isEnabled()) {
Intent setNfc = new Intent(Settings.ACTION_NFC_SETTINGS);
activity.startActivity(setNfc);
}
}
return mNfcAdapter;
}
/**
* 初始化nfc設置
*/
public static void NfcInit(Activity activity) {
mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter filter2 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
filter.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
mIntentFilter = new IntentFilter[]{filter, filter2};
mTechList = null;
}
/**
* 讀取NFC的數據
*/
public static String readNFCFromTag(Intent intent) throws UnsupportedEncodingException {
Parcelable[] rawArray = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawArray != null) {
NdefMessage mNdefMsg = (NdefMessage) rawArray[0];
NdefRecord mNdefRecord = mNdefMsg.getRecords()[0];
if (mNdefRecord != null) {
String readResult = new String(mNdefRecord.getPayload(), "UTF-8");
return readResult;
}
}
return "";
}
/**
* 往nfc寫入數據
*/
public static void writeNFCToTag(String data, Intent intent) throws IOException, FormatException {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndef = Ndef.get(tag);
ndef.connect();
NdefRecord ndefRecord = NdefRecord.createTextRecord(null, data);
NdefRecord[] records = {ndefRecord};
NdefMessage ndefMessage = new NdefMessage(records);
ndef.writeNdefMessage(ndefMessage);
}
/**
* 讀取nfcID
*/
public static String readNFCId(Intent intent) throws UnsupportedEncodingException {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String id = ByteArrayToHexString(tag.getId());
return id;
}
/**
* 將字節數組轉換爲字符串
*/
private static String ByteArrayToHexString(byte[] inarray) {
int i, j, in;
String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
String out = "";
for (j = 0; j < inarray.length; ++j) {
in = (int) inarray[j] & 0xff;
i = (in >> 4) & 0x0f;
out += hex[i];
i = in & 0x0f;
out += hex[i];
}
return out;
}
}
4、在NFCActivity代碼中的使用、使用標籤的前臺調度系統;
@Override
public void initData() {
//nfc初始化設置
NfcUtils nfcUtils = new NfcUtils(this);
}
@Override
protected void onResume() {
super.onResume();
//開啓前臺調度系統
NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);
}
@Override
protected void onPause() {
super.onPause();
//關閉前臺調度系統
NfcUtils.mNfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//當該Activity接收到NFC標籤時,運行該方法
//調用工具方法,讀取NFC數據
String str = NfcUtils.rendFromTag(intent);
}