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

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

功能实现,如下代码所示:

 

读写NFC标签的Uri 主Activity

 

1 import cn.read.write.uri.library.UriRecord;  2 import android.app.Activity;  3 import android.content.Intent;  4 import android.nfc.NdefMessage;  5 import android.nfc.NdefRecord;  6 import android.nfc.NfcAdapter;  7 import android.nfc.Tag;  8 import android.nfc.tech.Ndef;  9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.TextView; 12 import android.widget.Toast; 13  14 /** 15  * 读写NFC标签的Uri 16  * @author dr 17  * 18  */ 19 public class ReadWriteUriMainActivity extends Activity { 20     private TextView mSelectUri; 21     private String mUri; 22  23     @Override 24     public void onCreate(Bundle savedInstanceState) { 25         super.onCreate(savedInstanceState); 26         setContentView(R.layout.activity_read_write_uri_main); 27         mSelectUri = (TextView) findViewById(R.id.textview_uri); 28     } 29  30     public void onClick_SelectUri(View view) { 31         Intent intent = new Intent(this, UriListActivity.class); 32         startActivityForResult(intent, 1); 33     } 34  35     @Override 36     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 37         if (requestCode == 1 && resultCode == 1) { 38             mUri = data.getStringExtra("uri"); 39             mSelectUri.setText(mUri); 40         } 41     } 42  43     public void onNewIntent(Intent intent) { 44         if (mUri == null) { 45             Intent myIntent = new Intent(this, ShowNFCTagContentActivity.class); 46             myIntent.putExtras(intent); 47             myIntent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 48             startActivity(myIntent); 49         } else {  // write nfc 50             Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 51             NdefMessage ndefMessage = new NdefMessage( 52                     new NdefRecord[] { createUriRecord(mUri) }); 53             if (writeTag(ndefMessage, tag)) { 54                 mUri = null; 55                 mSelectUri.setText(""); 56             } 57         } 58     } 59  60     public NdefRecord createUriRecord(String uriStr) { 61         byte prefix = 0; 62         for (Byte b : UriRecord.URI_PREFIX_MAP.keySet()) { 63             String prefixStr = UriRecord.URI_PREFIX_MAP.get(b).toLowerCase(); 64             if ("".equals(prefixStr)) 65                 continue; 66             if (uriStr.toLowerCase().startsWith(prefixStr)) { 67                 prefix = b; 68                 uriStr = uriStr.substring(prefixStr.length()); 69                 break; 70             } 71         } 72         byte[] data = new byte[1 + uriStr.length()]; 73         data[0] = prefix; 74         System.arraycopy(uriStr.getBytes(), 0, data, 1, uriStr.length()); 75  76         NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 77                 NdefRecord.RTD_URI, new byte[0], data); 78         return record; 79     } 80  81     boolean writeTag(NdefMessage message, Tag tag) { 82         int size = message.toByteArray().length; 83         try { 84             Ndef ndef = Ndef.get(tag); 85             if (ndef != null) { 86                 ndef.connect(); 87                 if (!ndef.isWritable()) { 88                     return false; 89                 } 90                 if (ndef.getMaxSize() < size) { 91                     return false; 92                 } 93                 ndef.writeNdefMessage(message); 94                 Toast.makeText(this, "ok", Toast.LENGTH_LONG).show(); 95                 return true; 96             } 97         } catch (Exception e) { 98             // TODO: handle exception 99         }100         return false;101     }102 }

 

 

 

读写NFC标签的Uri 主xml

 

1 
2
6 7
12 13
18 19
26 27
31 32

 

 

 

显示URI列表Activity

1 import android.app.ListActivity; 2 import android.content.Intent; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.widget.AdapterView; 6 import android.widget.AdapterView.OnItemClickListener; 7 import android.widget.ArrayAdapter; 8  9 /**10  * 显示URI列表11  * @author dr12  *13  */14 public class UriListActivity extends ListActivity implements15         OnItemClickListener {16     private String uris[] = new String[] { "http://www.google.com",17             "http://www.apple.com", "http://developer.apple.com",18             "http://www.126.com", "ftp://192.168.17.160",19             "https://192.168.17.120", "smb://192.168.17.100" };20 21     @Override22     public void onCreate(Bundle savedInstanceState) {23         super.onCreate(savedInstanceState);24         ArrayAdapter
arrayAdapter = new ArrayAdapter
(this,25 android.R.layout.simple_list_item_1, android.R.id.text1, uris);26 setListAdapter(arrayAdapter);27 getListView().setOnItemClickListener(this);28 }29 30 @Override31 public void onItemClick(AdapterView
parent, View view, int position,32 long id) {33 Intent intent = new Intent();34 intent.putExtra("uri", uris[position]);35 setResult(1, intent);36 finish();37 }38 39 }

 

显示NFC标签内容Activity

1 import android.app.Activity; 2 import android.nfc.NdefMessage; 3 import android.nfc.NdefRecord; 4 import android.nfc.NfcAdapter; 5 import android.nfc.Tag; 6 import android.nfc.tech.Ndef; 7 import android.os.Bundle; 8 import android.os.Parcelable; 9 import android.widget.TextView;10 import cn.read.write.uri.library.UriRecord;11 12 /**13  * 显示NFC标签内容14  * @author dr15  *16  */17 public class ShowNFCTagContentActivity extends Activity {18     private TextView mTagContent;19     private Tag mDetectedTag;20     private String mTagText;21 22     @Override23     public void onCreate(Bundle savedInstanceState) {24         super.onCreate(savedInstanceState);25         setContentView(R.layout.activity_show_nfctag_content);26         mTagContent = (TextView) findViewById(R.id.textview_tag_content);27         mDetectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);28         Ndef ndef = Ndef.get(mDetectedTag);29         mTagText = ndef.getType() + "\n max size:" + ndef.getMaxSize()30                 + " bytes\n\n";31 32         readNFCTag();33 34         mTagContent.setText(mTagText);35     }36 37     private void readNFCTag() {38         if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {39             Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(40                     NfcAdapter.EXTRA_NDEF_MESSAGES);41 42             NdefMessage ndefMessage = null;43 44             int contentSize = 0;45             if (rawMsgs != null) {46                 if (rawMsgs.length > 0) {47                     ndefMessage = (NdefMessage) rawMsgs[0];48                     contentSize = ndefMessage.toByteArray().length;49                 } else {50                     return;51                 }52             }53             try {54                 NdefRecord ndefRecord = ndefMessage.getRecords()[0];55                 UriRecord uriRecord = UriRecord.parse(ndefRecord);56                 mTagText += uriRecord.getUri().toString() + "\n\nUri\n"57                         + contentSize + " bytes";58             } catch (Exception e) {59                 // TODO: handle exception60             }61         }62     }63 64 }

 

显示NFC标签内容xml

1 
5 6
12 13

 

AndroidManifest.xml

 

1 
5 6
9 10
11 12
16
20
21
22 23
24
25
26
27 28
29 30
31
32
33
34
35
36 37
38
39
40 41
44
47 48 49

 

 

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

 

 

 

 

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

你可能感兴趣的文章
开源 免费 java CMS - FreeCMS1.2-标签 infoSign
查看>>
开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据
查看>>
git reset 三种用法总结
查看>>
hdfs笔记
查看>>
虚拟机新增加硬盘,不用重启读到新加的硬盘
查看>>
Java IO流详尽解析
查看>>
邮件服务系列之四基于虚拟用户的虚拟域的邮件系统(安装courier-authlib以及部分配置方法)...
查看>>
Linux VSFTP服务器
查看>>
DHCP中继数据包互联网周游记
查看>>
Squid 反向代理服务器配置
查看>>
Java I/O操作
查看>>
Tomcat性能调优
查看>>
项目管理心得
查看>>
Android自学--一篇文章基本掌握所有的常用View组件
查看>>
灰度图像和彩色图像
查看>>
通过vb.net 和NPOI实现对excel的读操作
查看>>
TCP segmentation offload
查看>>
java数据类型
查看>>
数据结构——串的朴素模式和KMP匹配算法
查看>>
FreeMarker-Built-ins for strings
查看>>