Android實(shí)現(xiàn)NFC讀取校園卡
本文實(shí)例為大家分享了Android實(shí)現(xiàn)NFC讀取校園卡的具體代碼,供大家參考,具體內(nèi)容如下
主程序:
package com.nfclab.stuCard; import java.io.IOException; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.content.IntentFilter; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.nfc.tech.NdefFormatable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class stuCardActivity extends Activity { private NfcAdapter mNfcAdapter; private PendingIntent mPendingIntent; private IntentFilter[] mFilters; private String[][] mTechLists; private String studentId=""; private String studentName=" "; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText studentIdEditText = (EditText)this.findViewById(R.id.studentIdEditText); final EditText studentNameEditText = (EditText)this.findViewById(R.id.studentNameEditText); Button writeStudentButton = (Button)this.findViewById(R.id.writeStudentButton); writeStudentButton.setOnClickListener(new android.view.View.OnClickListener() { public void onClick(View view) { studentId = studentIdEditText.getText().toString(); studentName = studentNameEditText.getText().toString(); TextView messageText = (TextView)findViewById(R.id.messageText); messageText.setText("Touch NFC Tag to write \n"); messageText.append("Student id:" + studentId + "\nStudent Name: " + studentName ); } }); Button exitButton = (Button)findViewById(R.id.exitButton); exitButton.setOnClickListener(new android.view.View.OnClickListener() { public void onClick(View v) { finish(); } }); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); //ndef.addDataScheme("http"); mFilters = new IntentFilter[] { ndef, }; mTechLists = new String[][] { new String[] { Ndef.class.getName() }, new String[] { NdefFormatable.class.getName() }}; } @Override public void onResume() { super.onResume(); if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); } @Override public void onNewIntent(Intent intent) { Log.i("Foreground dispatch", "Discovered tag with intent: " + intent); Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String externalType = "nfclab.com:transport"; String payload = studentId+":"+studentName; NdefRecord extRecord1 = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, externalType.getBytes(), new byte[0], payload.getBytes()); NdefMessage newMessage = new NdefMessage(new NdefRecord[] { extRecord1}); writeNdefMessageToTag(newMessage, tag); } @Override public void onPause() { super.onPause(); mNfcAdapter.disableForegroundDispatch(this); } boolean writeNdefMessageToTag(NdefMessage message, Tag detectedTag) { int size = message.toByteArray().length; try { Ndef ndef = Ndef.get(detectedTag); if (ndef != null) { ndef.connect(); if (!ndef.isWritable()) { Toast.makeText(this, "Tag is read-only.", Toast.LENGTH_SHORT).show(); return false; } if (ndef.getMaxSize() < size) { Toast.makeText(this, "The data cannot written to tag, Tag capacity is " + ndef.getMaxSize() + " bytes, message is " + size + " bytes.", Toast.LENGTH_SHORT).show(); return false; } ndef.writeNdefMessage(message); ndef.close(); Toast.makeText(this, "Message is written tag.", Toast.LENGTH_SHORT).show(); return true; } else { NdefFormatable ndefFormat = NdefFormatable.get(detectedTag); if (ndefFormat != null) { try { ndefFormat.connect(); ndefFormat.format(message); ndefFormat.close(); Toast.makeText(this, "The data is written to the tag ", Toast.LENGTH_SHORT).show(); return true; } catch (IOException e) { Toast.makeText(this, "Failed to format tag", Toast.LENGTH_SHORT).show(); return false; } } else { Toast.makeText(this, "NDEF is not supported", Toast.LENGTH_SHORT).show(); return false; } } } catch (Exception e) { Toast.makeText(this, "Write opreation is failed", Toast.LENGTH_SHORT).show(); } return false; } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="fill_parent" android:columnCount="2" android:orientation="horizontal" android:rowCount="5" > <TextView android:id="@+id/requestStudentId" android:layout_row="0" android:layout_column="0" android:text="@string/requestStudentIdText" android:textSize="16sp" /> <EditText android:id="@+id/studentIdEditText" android:layout_row="0" android:layout_column="1" android:inputType="text" android:text="@string/emptyText" > <requestFocus /> </EditText> <TextView android:id="@+id/requestName" android:layout_row="1" android:layout_column="0" android:text="@string/requestStudentNameText" android:textSize="16sp" ></TextView> <EditText android:id="@+id/studentNameEditText" android:layout_row="1" android:layout_column="1" android:inputType="text" android:text="@string/emptyText" > </EditText> <TextView android:id="@+id/messageText" android:layout_row="3" android:layout_column="0" android:layout_columnSpan="2" android:gravity="left" android:text="@string/emptyText" android:textSize="24sp" /> <Button android:id="@+id/writeStudentButton" android:layout_row="2" android:layout_column="0" android:gravity="left" android:text="@string/writeButtonText" /> <Button android:id="@+id/exitButton" android:layout_row="2" android:layout_column="1" android:gravity="left" android:text="@string/exitButtonText" /> </GridLayout>
配置文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.nfclab.transportationwriter" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <uses-permission android:name="android.permission.NFC" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".stuCardActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容,主要通過設(shè)置EditText的setTransformationMethod()方法來實(shí)現(xiàn),需要的朋友可以參考下2014-09-09Android ViewPager無限循環(huán)滑動(dòng)并可自動(dòng)滾動(dòng)完整實(shí)例
對(duì)于Android ViewPager廣告頁(yè)可無限循環(huán)滑動(dòng)并可自動(dòng)滾動(dòng)帶有小圓點(diǎn)的這個(gè)功能很多APP都有這個(gè)功能,這里為大家提供了完整的實(shí)例代碼2018-03-03Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能示例
這篇文章主要介紹了Golang+Android基于HttpURLConnection實(shí)現(xiàn)的文件上傳功能,結(jié)合具體實(shí)例形式分析了Android基于HttpURLConnection的客戶端結(jié)合Go語(yǔ)言服務(wù)器端實(shí)現(xiàn)文件上傳功能的操作技巧,需要的朋友可以參考下2017-03-03android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案
今天小編就為大家分享一篇關(guān)于android和服務(wù)器的URLEncodedUtils亂碼編碼問題的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Android實(shí)現(xiàn)選項(xiàng)菜單子菜單
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)選項(xiàng)菜單子菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12Android實(shí)現(xiàn)跑馬燈效果的代碼詳解
Android中實(shí)現(xiàn)跑馬燈效果有多種方式,本文給大家介紹了Android實(shí)現(xiàn)跑馬燈效果的簡(jiǎn)單示例,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的朋友可以參考下2018-05-05Android仿微信公眾號(hào)文章頁(yè)面加載進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android仿微信公眾號(hào)文章頁(yè)面加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android自定義相機(jī)界面的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)界面的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)現(xiàn)手指觸控圖片縮放功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手指觸控圖片縮放功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12