博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
10、NFC技术:读写NFC标签中的文本数据
阅读量:7052 次
发布时间:2019-06-28

本文共 7374 字,大约阅读时间需要 24 分钟。

代码实现过程如下:

 

读写NFC标签的纯文本数据.java

1  import java.nio.charset.Charset;  2 import java.util.Locale;  3   4 import android.app.Activity;  5 import android.content.Intent;  6 import android.nfc.NdefMessage;  7 import android.nfc.NdefRecord;  8 import android.nfc.NfcAdapter;  9 import android.nfc.Tag; 10 import android.nfc.tech.Ndef; 11 import android.os.Bundle; 12 import android.view.View; 13 import android.widget.TextView; 14  15 /** 16  * 主 Activity 17  * 读写NFC标签的纯文本数据 18  * @author dr 19  *  20  */ 21 public class ReadWriteTextMainActivity extends Activity { 22     private TextView mInputText; 23     private String mText; 24  25     @Override 26     protected void onCreate(Bundle savedInstanceState) { 27         // TODO Auto-generated method stub 28         super.onCreate(savedInstanceState); 29         setContentView(R.layout.activity_read_write_text_main); 30  31         mInputText = (TextView) findViewById(R.id.textview_input_text); 32     } 33  34     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 35         if (requestCode == 1 && resultCode == 1) { 36             mText = data.getStringExtra("text"); 37             mInputText.setText(mText); 38         } 39     } 40  41     public void onNewIntent(Intent intent) { 42         // read nfc text 43         if (mText == null) { 44             // 显示NFC标签内容 45             Intent myIntent = new Intent(this, ShowNFCTagContentActivity.class); 46             myIntent.putExtras(intent); 47             myIntent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 48             startActivity(myIntent); 49  50         } else {  // write nfc text 51             Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 52             NdefMessage ndefMessage = new NdefMessage( 53                     new NdefRecord[] { createTextRecord(mText) }); 54             writeTag(ndefMessage, tag); 55         } 56     } 57      58     /** 59      * 将纯文本转换成NdefRecord对象。 60      * @param text 61      * @return 62      */ 63     public NdefRecord createTextRecord(String text) { 64         // 得到生成语言编码字节数组 65         byte[] langBytes = Locale.CHINA.getLanguage().getBytes( 66                 Charset.forName("US-ASCII")); 67         Charset utfEncoding = Charset.forName("UTF-8"); 68         byte[] textBytes = text.getBytes(utfEncoding); 69         int utfBit = 0; 70         // 状态字节 71         char status = (char) (utfBit + langBytes.length); 72  73         byte[] data = new byte[1 + langBytes.length + textBytes.length]; 74         data[0] = (byte) status; 75         // 数组拷贝 76         System.arraycopy(langBytes, 0, data, 1, langBytes.length); 77         System.arraycopy(textBytes, 0, data, 1 + langBytes.length, 78                 textBytes.length); 79  80         NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 81                 NdefRecord.RTD_TEXT, new byte[0], data); 82         return ndefRecord; 83     } 84  85     boolean writeTag(NdefMessage ndefMessage, Tag tag) { 86         try { 87             Ndef ndef = Ndef.get(tag); 88             ndef.connect(); 89             ndef.writeNdefMessage(ndefMessage); 90             return true; 91         } catch (Exception e) { 92             // TODO: handle exception 93         } 94         return false; 95     } 96  97     // 向NFC标签写入文本 98     public void onClick_InputText(View view) { 99         Intent intent = new Intent(this, InputTextActivity.class);100         startActivityForResult(intent, 1);101     }102 103 }

 

读写NFC标签的纯文本数据.xml

1 
2
6 7
12 13
18 19
27 28
32 33

 

显示NFC标签内容.java

1 import cn.eoe.read.write.text.library.TextRecord; 2 import android.app.Activity; 3 import android.nfc.NdefMessage; 4 import android.nfc.NdefRecord; 5 import android.nfc.NfcAdapter; 6 import android.nfc.Tag; 7 import android.nfc.tech.Ndef; 8 import android.os.Bundle; 9 import android.os.Parcelable;10 import android.widget.TextView;11 12 13 /**14  * 显示NFC标签内容15  * @author dr16  *17  */18 public class ShowNFCTagContentActivity extends Activity {19 20     private TextView mTagContent;21     private Tag mDetectedTag;   // 传递过来的NFC的一个TAG对象22     private String mTagText;23 24     @Override25     protected void onCreate(Bundle savedInstanceState) {26         // TODO Auto-generated method stub27         super.onCreate(savedInstanceState);28         setContentView(R.layout.activity_show_nfctag_content);29         mTagContent = (TextView) findViewById(R.id.textview_tag_content);30 31         mDetectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);32 33         Ndef ndef = Ndef.get(mDetectedTag);34 35         mTagText = ndef.getType() + "\nmax size:" + ndef.getMaxSize()36                 + "bytes\n\n";37 38         readNFCTag();39 40         mTagContent.setText(mTagText);41     }42 43     /**44      * 45      */46     private void readNFCTag() {47         if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {48             Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(49                     NfcAdapter.EXTRA_NDEF_MESSAGES);50             NdefMessage msgs[] = null;51             int contentSize = 0;52             if (rawMsgs != null) {53                 msgs = new NdefMessage[rawMsgs.length];54                 for (int i = 0; i < rawMsgs.length; i++) {55                     msgs[i] = (NdefMessage) rawMsgs[i];56                     contentSize += msgs[i].toByteArray().length;57                 }58             }59             try {60                 if (msgs != null) {61                     NdefRecord record = msgs[0].getRecords()[0];62                     TextRecord textRecord = TextRecord.parse(record);63                     mTagText += textRecord.getText() + "\n\ntext\n"64                             + contentSize + " bytes";65                 }66             } catch (Exception e) {67                 // TODO: handle exception68             }69         }70     }71 }

 

显示NFC标签内容.xml

1 
5 6
11 12

 

向NFC标签写入文本.java

1 import android.app.Activity; 2 import android.content.Intent; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.widget.EditText; 6  7 /** 8  * 向NFC标签写入文本 9  * 10  * @author dr11  * 12  */13 public class InputTextActivity extends Activity {14 15     private EditText mTextTag;16 17     @Override18     protected void onCreate(Bundle savedInstanceState) {19         super.onCreate(savedInstanceState);20         setContentView(R.layout.activity_input_text);21         mTextTag = (EditText) findViewById(R.id.edittext_text_tag);22     }23 24     public void onClick_OK(View view) {25         Intent intent = new Intent();26         intent.putExtra("text", mTextTag.getText().toString());27         setResult(1, intent);28         finish();29 30     }31 }

 

向NFC标签写入文本.xml

 

 

1 
6 7
12 13
18 19
24 25

 

 

1 
5 6
9 10
11 12
16
20
21
22
23
24
25
26
27
28
29 30
34 35
38 39 40 41

 

 DEMO:下载地址:http://download.csdn.net/detail/androidsj/7678947

 

转载地址:http://wesol.baihongyu.com/

你可能感兴趣的文章
(转载)iPhone开发视频教程 Objective-C部分 (51课时)
查看>>
Unity 5.1+ Assertion Library (断言库)
查看>>
OracleLinux上安装数据库(DBCA)
查看>>
[LeetCode] Happy Number 快乐数
查看>>
12306-票
查看>>
LINQ的ORM功能中对使用sp_executesql语句的存储过程的支持
查看>>
springMvc 的参数验证 BindingResult result 的使用
查看>>
JQuery ztree 异步加载实践
查看>>
XOR算法的原理和实现
查看>>
iOS - C 基本语法
查看>>
我的软件测试之旅:(8)困难——没有现成的测试工具
查看>>
“智慧城市”建设风生水起
查看>>
楼宇对讲防盗报警系统两大特点
查看>>
使用“伪造”数据是消除大数据隐私问题的关键
查看>>
浅谈信息安全与数据中心安全的关系
查看>>
《PostgreSQL服务器编程》一一2.7 小结
查看>>
Oracle Database 12.2新特性详解
查看>>
IBM:量子计算现在跟1940年代电脑差不多 更看重长远目标
查看>>
研究机构称独角兽更易获得融资 明后年或有大量企业IPO
查看>>
三星将斥资80亿美元收购美国哈曼国际
查看>>