您好,登錄后才能下訂單哦!
小編給大家分享一下android如何實現(xiàn)NFC讀寫功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
近距離無線通訊技術(shù),這個技術(shù)由非接觸式射頻識別(RFID)演變而來,由飛利浦半導(dǎo)體(現(xiàn)恩智浦半導(dǎo)體公司)、諾基亞和索尼共同研制開發(fā),其基礎(chǔ)是RFID及互連技術(shù)。近場通信(Near Field Communication,NFC)是一種短距高頻的無線電技術(shù),在13.56MHz頻率運行于20厘米距離內(nèi)。其傳輸速度有106 Kbit/秒、212 Kbit/秒或者424 Kbit/秒三種。目前近場通信已通過成為ISO/IEC IS 18092國際標準、ECMA-340標準與ETSI TS 102 190標準。NFC采用主動和被動兩種讀取模式。
NFC通信模式主要有以下幾種(信息來源):
1.讀卡器模式(Reader/writer mode):
作為非接觸讀卡器使用,比如從海報或者展覽信息電子標簽上讀取相關(guān)信息。亦可實現(xiàn)NFC手機之間的數(shù)據(jù)交換,對于企業(yè)環(huán)境的中的文件共享,或者對于多玩家的游戲應(yīng)用,都將帶來諸多的便利。
2. 點對點模式(P2Pmode):
此模式和紅外線差不多,可用于數(shù)據(jù)交換,只是傳輸距離較短,傳輸創(chuàng)建速度較快,傳輸速度也快些,功耗低(藍牙也類似)。將兩個具備NFC功能的設(shè)備無線鏈接,能實現(xiàn)數(shù)據(jù)點對點傳輸,如下載音樂、交換圖片或者同步設(shè)備地址薄。因此通過NFC,多個設(shè)備如數(shù)位相機、PDA、計算機和手機之間都可以交換資料或者服務(wù)。
3.卡模式(Cardemulation):
這個模式其實就是相當(dāng)于一張采用RFID技術(shù)的IC卡,可以替代大量的IC卡(包括信用卡)使用的場合,如商場刷卡、公交卡、門禁管制,車票,門票等等。此種方式下,有一個極大的優(yōu)點,那就是卡片通過非接觸讀卡器的 RF 域來供電,即使寄主設(shè)備(如手機)沒電也可以工作。
1、首先在manifests里面聲明NFC和添加相應(yīng)的權(quán)限;
<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的讀寫,方便調(diào)用;
public class NfcUtils { //nfc public static NfcAdapter mNfcAdapter; public static IntentFilter[] mIntentFilter = null; public static PendingIntent mPendingIntent = null; public static String[][] mTechList = null; /** * 構(gòu)造函數(shù),用于初始化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設(shè)置 */ 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的數(shù)據(jù) */ 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寫入數(shù)據(jù) */ 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; } /** * 將字節(jié)數(shù)組轉(zhuǎn)換為字符串 */ 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代碼中的使用、使用標簽的前臺調(diào)度系統(tǒng);
@Override public void initData() { //nfc初始化設(shè)置 NfcUtils nfcUtils = new NfcUtils(this); }
@Override protected void onResume() { super.onResume(); //開啟前臺調(diào)度系統(tǒng) NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList); }
@Override protected void onPause() { super.onPause(); //關(guān)閉前臺調(diào)度系統(tǒng) NfcUtils.mNfcAdapter.disableForegroundDispatch(this); }
@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); //當(dāng)該Activity接收到NFC標簽時,運行該方法 //調(diào)用工具方法,讀取NFC數(shù)據(jù) String str = NfcUtils.rendFromTag(intent); }
以上是“android如何實現(xiàn)NFC讀寫功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。