先說(shuō)說(shuō)NFC開(kāi)發(fā)總結(jié),看了幾天NFC開(kāi)發(fā)資料,搜集了不少關(guān)于這方面的資料、demo、以及他人的總結(jié),android NFC開(kāi)發(fā)
。以下有部分是摘錄總結(jié)的。因?yàn)橐乾F(xiàn)在總結(jié)也是那些,最后附送代碼。關(guān)于demo我也有,有需要在評(píng)論去發(fā)郵箱給我。
一、NFC的配置總結(jié)
第一:屏幕沒(méi)有鎖住 。 第二:NFC功能已經(jīng)在設(shè)置中打開(kāi)
當(dāng)系統(tǒng)檢測(cè)到一個(gè)NFC標(biāo)簽的時(shí)候,他會(huì)自動(dòng)去尋找最合適的activity去處理這個(gè)intent.
NFC發(fā)出的這個(gè)Intent將會(huì)有三種action:
ACTION_NDEF_DISCOVERED:當(dāng)系統(tǒng)檢測(cè)到tag中含有NDEF格式的數(shù)據(jù)時(shí),且系統(tǒng)中有activity聲明可以接受包含NDEF數(shù)據(jù)的Intent的時(shí)候,系統(tǒng)會(huì)優(yōu)先發(fā)出這個(gè)action的intent。
ACTION_TECH_DISCOVERED:當(dāng)沒(méi)有任何一個(gè)activity聲明自己可以響應(yīng)ACTION_NDEF_DISCOVERED時(shí),系統(tǒng)會(huì)嘗試發(fā)出TECH的intent.即便你的tag中所包含的數(shù)據(jù)是NDEF的,但是如果這個(gè)數(shù)據(jù)的MIMEtype或URI不能和任何一個(gè)activity所聲明的想吻合,系統(tǒng)也一樣會(huì)嘗試發(fā)出tech格式的intent,而不是NDEF.
ACTION_TAG_DISCOVERED:當(dāng)系統(tǒng)發(fā)現(xiàn)前兩個(gè)intent在系統(tǒng)中無(wú)人會(huì)接受的時(shí)候,就只好發(fā)這個(gè)默認(rèn)的TAG類型的
二、NFC相關(guān)androidManifest文件設(shè)置
首先是權(quán)限:
然后是sdk級(jí)別限制:我個(gè)人建議API11開(kāi)始比較合適:
如果是API8,在代碼中,nfc功能設(shè)置的代碼會(huì)出錯(cuò),要拋出
例如:
NfcAdapter mAdapter = NfcAdapter.getDefaultAdapter(this);
mAdapter.isEnabled()
接著是特殊功能限制:
這個(gè)生命可以讓你的應(yīng)用在googleplay上被聲明使用者必須擁有nfc功能。
三、NFC標(biāo)簽過(guò)濾,也在androidManifest文件設(shè)置
在activity的intent過(guò)濾xml聲明中,你可以同時(shí)聲明過(guò)濾這三種action.但是由之前所說(shuō),你應(yīng)該知道系統(tǒng)在發(fā)送intent的時(shí)候是有優(yōu)先級(jí)的,所以你最好清楚自己最想處理哪個(gè)。
1:過(guò)濾ACTION_TAG_DISCOVERED:
<intent-filter><category android:name="android.intent.category.DEFAULT"></category></intent-filter>這個(gè)最簡(jiǎn)單,也是最后一個(gè)被嘗試接受intent的選項(xiàng)。
2:過(guò)濾ACTION_NDEF_DISCOVERED:
<intent-filter><category android:name="android.intent.category.DEFAULT"><data android:mimetype="text/plain"></data></category></intent-filter>其中最重要的應(yīng)該算是data的mimeType類型了,這個(gè)定義的越準(zhǔn)確,intent指向你這個(gè)activity的成功率就越高,否則系統(tǒng)可能不會(huì)發(fā)出你想要的NDEF intent了。下面在講如何使用NDEF寫(xiě)入NFC標(biāo)簽的時(shí)候會(huì)多舉幾個(gè)類型的例子。
3:過(guò)濾ACTION_TECH_DISCOVERED:
你首先需要在你的
一個(gè)nfc_tech_filter.xml中可以定義多個(gè)
<span><resources xmlns:xliff="urn:oasis:names:tc:xliff:documen</span>t: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-list><tech-list><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></span>
在androidManifest文件中聲明xml過(guò)濾的舉例如下:
<intent-filter></intent-filter><meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter"></meta-data>
四、看具體代碼:
1、主要代碼實(shí)現(xiàn)如下:
package org.reno.Beam;import java.io.ByteArrayOutputStream;import java.nio.charset.Charset;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import java.util.Locale;import org.nfc.read.ParsedNdefRecord;import android.app.Activity;import android.app.AlertDialog;import android.app.PendingIntent;import android.content.DialogInterface;import android.content.Intent;import android.nfc.NdefMessage;import android.nfc.NdefRecord;import android.nfc.NfcAdapter;import android.nfc.Tag;import android.nfc.tech.MifareClassic;import android.nfc.tech.MifareUltralight;import android.os.Bundle;import android.os.Parcelable;import android.provider.Settings;import android.widget.TextView;public class MainActivity extends Activity { private static final DateFormat TIME_FORMAT = SimpleDateFormat .getDateTimeInstance(); private NfcAdapter mAdapter; private PendingIntent mPendingIntent; private NdefMessage mNdefPushMessage; private TextView promt; private AlertDialog mDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); promt = (TextView) findViewById(R.id.promt); resolveIntent(getIntent()); mDialog = new AlertDialog.Builder(this).setNeutralButton("Ok", null) .create(); // 獲取默認(rèn)的NFC控制器 mAdapter = NfcAdapter.getDefaultAdapter(this); //攔截系統(tǒng)級(jí)的NFC掃描,例如掃描藍(lán)牙 mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); mNdefPushMessage = new NdefMessage(new NdefRecord[] { newTextRecord("", Locale.ENGLISH, true) }); } @Override protected void onResume() { super.onResume(); if (mAdapter == null) { if (!mAdapter.isEnabled()) { showWirelessSettingsDialog(); } showMessage(R.string.error, R.string.no_nfc); promt.setText("設(shè)備不支持NFC!"); return; } if (!mAdapter.isEnabled()) { promt.setText("請(qǐng)?jiān)谙到y(tǒng)設(shè)置中先啟用NFC功能!"); return; } if (mAdapter != null) { //隱式啟動(dòng) mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null); mAdapter.enableForegroundNdefPush(this, mNdefPushMessage); } } @Override protected void onPause() { super.onPause(); if (mAdapter != null) { //隱式啟動(dòng) mAdapter.disableForegroundDispatch(this); mAdapter.disableForegroundNdefPush(this); } } //16進(jìn)制字符串轉(zhuǎn)換為String private String hexString = "0123456789ABCDEF"; public String decode(String bytes) { if (bytes.length() != 30) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream( bytes.length() / 2); // 將每2位16進(jìn)制整數(shù)組裝成一個(gè)字節(jié) for (int i = 0; i < bytes.length(); i += 2) baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString .indexOf(bytes.charAt(i + 1)))); return new String(baos.toByteArray()); } // 字符序列轉(zhuǎn)換為16進(jìn)制字符串 private static String bytesToHexString(byte[] src, boolean isPrefix) { StringBuilder stringBuilder = new StringBuilder(); if (isPrefix == true) { stringBuilder.append("0x"); } if (src == null || src.length <= 0) { return null; } char[] buffer = new char[2]; for (int i = 0; i < src.length; i++) { buffer[0] = Character.toUpperCase(Character.forDigit( (src[i] >>> 4) & 0x0F, 16)); buffer[1] = Character.toUpperCase(Character.forDigit(src[i] & 0x0F, 16)); System.out.println(buffer); stringBuilder.append(buffer); } return stringBuilder.toString(); } private void showMessage(int title, int message) { mDialog.setTitle(title); mDialog.setMessage(getText(message)); mDialog.show(); } private NdefRecord newTextRecord(String text, Locale locale, boolean encodeInUtf8) { byte[] langBytes = locale.getLanguage().getBytes( Charset.forName("US-ASCII")); Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset .forName("UTF-16"); byte[] textBytes = text.getBytes(utfEncoding); int utfBit = encodeInUtf8 ? 0 : (1 << 7); char status = (char) (utfBit + langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; data[0] = (byte) status; System.arraycopy(langBytes, 0, data, 1, langBytes.length); System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); } private void showWirelessSettingsDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(R.string.nfc_disabled); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent( Settings.ACTION_WIRELESS_SETTINGS); startActivity(intent); } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { finish(); } }); builder.create().show(); return; } //初步判斷是什么類型NFC卡 private void resolveIntent(Intent intent) { String action = intent.getAction(); if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) { Parcelable[] rawMsgs = intent .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage[] msgs; if (rawMsgs != null) { msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; } } else { // Unknown tag type byte[] empty = new byte[0]; byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); Parcelable tag = intent .getParcelableExtra(NfcAdapter.EXTRA_TAG); byte[] payload = dumpTagData(tag).getBytes(); NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload); NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); msgs = new NdefMessage[] { msg }; } // Setup the views buildTagViews(msgs); } } //一般公家卡,掃描的信息 private String dumpTagData(Parcelable p) { StringBuilder sb = new StringBuilder(); Tag tag = (Tag) p; byte[] id = tag.getId(); sb.append("Tag ID (hex): ").append(getHex(id)).append("\n"); sb.append("Tag ID (dec): ").append(getDec(id)).append("\n"); sb.append("ID (reversed): ").append(getReversed(id)).append("\n"); String prefix = "android.nfc.tech."; sb.append("Technologies: "); for (String tech : tag.getTechList()) { sb.append(tech.substring(prefix.length())); sb.append(", "); } sb.delete(sb.length() - 2, sb.length()); for (String tech : tag.getTechList()) { if (tech.equals(MifareClassic.class.getName())) { sb.append('\n'); MifareClassic mifareTag = MifareClassic.get(tag); String type = "Unknown"; switch (mifareTag.getType()) { case MifareClassic.TYPE_CLASSIC: type = "Classic"; break; case MifareClassic.TYPE_PLUS: type = "Plus"; break; case MifareClassic.TYPE_PRO: type = "Pro"; break; } sb.append("Mifare Classic type: "); sb.append(type); sb.append('\n'); sb.append("Mifare size: "); sb.append(mifareTag.getSize() + " bytes"); sb.append('\n'); sb.append("Mifare sectors: "); sb.append(mifareTag.getSectorCount()); sb.append('\n'); sb.append("Mifare blocks: "); sb.append(mifareTag.getBlockCount()); } if (tech.equals(MifareUltralight.class.getName())) { sb.append('\n'); MifareUltralight mifareUlTag = MifareUltralight.get(tag); String type = "Unknown"; switch (mifareUlTag.getType()) { case MifareUltralight.TYPE_ULTRALIGHT: type = "Ultralight"; break; case MifareUltralight.TYPE_ULTRALIGHT_C: type = "Ultralight C"; break; } sb.append("Mifare Ultralight type: "); sb.append(type); } } return sb.toString(); } private String getHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = bytes.length - 1; i >= 0; --i) { int b = bytes[i] & 0xff; if (b < 0x10) sb.append('0'); sb.append(Integer.toHexString(b)); if (i > 0) { sb.append(" "); } } return sb.toString(); } private long getDec(byte[] bytes) { long result = 0; long factor = 1; for (int i = 0; i < bytes.length; ++i) { long value = bytes[i] & 0xffl; result += value * factor; factor *= 256l; } return result; } private long getReversed(byte[] bytes) { long result = 0; long factor = 1; for (int i = bytes.length - 1; i >= 0; --i) { long value = bytes[i] & 0xffl; result += value * factor; factor *= 256l; } return result; } //顯示NFC掃描的數(shù)據(jù) private void buildTagViews(NdefMessage[] msgs) { if (msgs == null || msgs.length == 0) { return; } // Parse the first message in the list // Build views for all of the sub records Date now = new Date(); List<parsedndefrecord>records = NdefMessageParser.parse(msgs[0]); final int size = records.size(); for (int i = 0; i < size; i++) { TextView timeView = new TextView(this); timeView.setText(TIME_FORMAT.format(now)); ParsedNdefRecord record = records.get(i); promt.append(record.getViewText()); } } //獲取系統(tǒng)隱式啟動(dòng)的 @Override public void onNewIntent(Intent intent) { setIntent(intent); resolveIntent(intent); }}</parsedndefrecord>
2、androidManifest配置
<manifest android:versioncode="1" android:versionname="1.0" package="org.reno.Beam" xmlns:android="http://schemas.android.com/apk/res/android"><uses-sdk android:minsdkversion="11" android:targetsdkversion="16"><uses-permission android:name="android.permission.NFC"><uses-feature android:name="android.hardware.nfc"><intent-filter><category android:name="android.intent.category.LAUNCHER"></category></intent-filter><intent-filter><category android:name="android.intent.category.DEFAULT"></category></intent-filter></uses-feature></uses-permission></uses-sdk></manifest>