10、NFC技术:读写NFC标签中的文本数据
2014-07-20 17:26
836 查看
[b]代码实现过程如下:[/b]
读写NFC标签的纯文本数据.java
import java.nio.charset.Charset; import java.util.Locale; import android.app.Activity; import android.content.Intent; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.os.Bundle; import android.view.View; import android.widget.TextView; /** * 主 Activity * 读写NFC标签的纯文本数据 * @author dr * */ public class ReadWriteTextMainActivity extends Activity { private TextView mInputText; private String mText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_write_text_main); mInputText = (TextView) findViewById(R.id.textview_input_text); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == 1) { mText = data.getStringExtra("text"); mInputText.setText(mText); } } public void onNewIntent(Intent intent) { // read nfc text if (mText == null) { // 显示NFC标签内容 Intent myIntent = new Intent(this, ShowNFCTagContentActivity.class); myIntent.putExtras(intent); myIntent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED); startActivity(myIntent); } else { // write nfc text Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); NdefMessage ndefMessage = new NdefMessage( new NdefRecord[] { createTextRecord(mText) }); writeTag(ndefMessage, tag); } } /** * 将纯文本转换成NdefRecord对象。 * @param text * @return */ public NdefRecord createTextRecord(String text) { // 得到生成语言编码字节数组 byte[] langBytes = Locale.CHINA.getLanguage().getBytes( Charset.forName("US-ASCII")); Charset utfEncoding = Charset.forName("UTF-8"); byte[] textBytes = text.getBytes(utfEncoding); int utfBit = 0; // 状态字节 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); NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); return ndefRecord; } boolean writeTag(NdefMessage ndefMessage, Tag tag) { try { Ndef ndef = Ndef.get(tag); ndef.connect(); ndef.writeNdefMessage(ndefMessage); return true; } catch (Exception e) { // TODO: handle exception } return false; } // 向NFC标签写入文本 public void onClick_InputText(View view) { Intent intent = new Intent(this, InputTextActivity.class); startActivityForResult(intent, 1); } }
读写NFC标签的纯文本数据.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick_InputText" android:text="输入要写入文本" /> <TextView android:id="@+id/textview_input_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="请将NFC标签靠近手机背面读取或写入文本" android:textColor="#F00" android:textSize="16sp" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/read_nfc_tag" /> </LinearLayout>
显示NFC标签内容.java
import cn.eoe.read.write.text.library.TextRecord; import android.app.Activity; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.os.Bundle; import android.os.Parcelable; import android.widget.TextView; /** * 显示NFC标签内容 * @author dr * */ public class ShowNFCTagContentActivity extends Activity { private TextView mTagContent; private Tag mDetectedTag; // 传递过来的NFC的一个TAG对象 private String mTagText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_nfctag_content); mTagContent = (TextView) findViewById(R.id.textview_tag_content); mDetectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); Ndef ndef = Ndef.get(mDetectedTag); mTagText = ndef.getType() + "\nmax size:" + ndef.getMaxSize() + "bytes\n\n"; readNFCTag(); mTagContent.setText(mTagText); } /** * */ private void readNFCTag() { if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES); NdefMessage msgs[] = null; int contentSize = 0; if (rawMsgs != null) { msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; contentSize += msgs[i].toByteArray().length; } } try { if (msgs != null) { NdefRecord record = msgs[0].getRecords()[0]; TextRecord textRecord = TextRecord.parse(record); mTagText += textRecord.getText() + "\n\ntext\n" + contentSize + " bytes"; } } catch (Exception e) { // TODO: handle exception } } } }
显示NFC标签内容.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textview_tag_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:layout_margin="6dp" /> </LinearLayout>
向NFC标签写入文本.java
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.EditText; /** * 向NFC标签写入文本 * * @author dr * */ public class InputTextActivity extends Activity { private EditText mTextTag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input_text); mTextTag = (EditText) findViewById(R.id.edittext_text_tag); } public void onClick_OK(View view) { Intent intent = new Intent(); intent.putExtra("text", mTextTag.getText().toString()); setResult(1, intent); finish(); } }
向NFC标签写入文本.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请输入要写入的文本" android:textSize="16sp" /> <EditText android:layout_marginTop="10dp" android:id="@+id/edittext_text_tag" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick_OK" android:text="确定" /> </LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.eoe.read.write.text" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.NFC" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".ReadWriteTextMainActivity" android:label="读写NFC标签的纯文本数据" android:launchMode="singleTask" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity> <activity android:name=".ShowNFCTagContentActivity" android:label="显示NFC标签内容" android:launchMode="singleTask" /> <activity android:name=".InputTextActivity" android:label="向NFC标签写入文本" /> </application> </manifest>
DEMO:下载地址:http://download.csdn.net/detail/androidsj/7678947
相关文章推荐
- 12、NFC技术:读写NFC标签中的Uri数据
- 13、NFC技术:读写非NDEF格式的数据
- Android基础——NFC标签初始化设置、NFC标签读写数据和获取ID、NFC标签前台调度系统
- Android硬件开发之——读写NFC标签中URI数据
- NFC之读写NFC标签的文本
- Android 使用NFC通过标签协议栈读写标签数据
- NFC标签初始化、NFC标签读写数据功能、NFC标签前台调度系统
- Android开发——NFC标签读写
- Android平台上读写NFC标签 原理
- Director中读写外部文本的技术
- 14、NFC技术:使用Android Beam技术传输文本
- 《数据挖掘概念与技术》学习笔记第2章(2/10)数据仓库和数据挖掘的OLAP技术
- 数据仓库专题(10)-文本事实和杂项维度
- OpenCV学习笔记(10)CvMat 与 STL vector 的格式转换与数据读写
- Java -- JDBC mysql读写大数据,文本 和 二进制文件
- 黑马程序员——Objective-C程序设计(第4版)学习笔记之10-变量和数据类型——黑马 IOS 技术博客
- Android平台上读写NFC标签
- Java -- JDBC mysql读写大数据,文本 和 二进制文件
- Android应用实例之----MifareUltralight格式的nfc标签读写
- c++简单读写文本,统计文件的行数,读取文件数据到数组