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

读写NFC标签的Uri 主Activity

 import cn.read.write.uri.library.UriRecord;
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;
import android.widget.Toast; /**
* 读写NFC标签的Uri
* @author dr
*
*/
public class ReadWriteUriMainActivity extends Activity {
private TextView mSelectUri;
private String mUri; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_write_uri_main);
mSelectUri = (TextView) findViewById(R.id.textview_uri);
} public void onClick_SelectUri(View view) {
Intent intent = new Intent(this, UriListActivity.class);
startActivityForResult(intent, 1);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == 1) {
mUri = data.getStringExtra("uri");
mSelectUri.setText(mUri);
}
} public void onNewIntent(Intent intent) {
if (mUri == null) {
Intent myIntent = new Intent(this, ShowNFCTagContentActivity.class);
myIntent.putExtras(intent);
myIntent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
startActivity(myIntent);
} else { // write nfc
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] { createUriRecord(mUri) });
if (writeTag(ndefMessage, tag)) {
mUri = null;
mSelectUri.setText("");
}
}
} public NdefRecord createUriRecord(String uriStr) {
byte prefix = 0;
for (Byte b : UriRecord.URI_PREFIX_MAP.keySet()) {
String prefixStr = UriRecord.URI_PREFIX_MAP.get(b).toLowerCase();
if ("".equals(prefixStr))
continue;
if (uriStr.toLowerCase().startsWith(prefixStr)) {
prefix = b;
uriStr = uriStr.substring(prefixStr.length());
break;
}
}
byte[] data = new byte[1 + uriStr.length()];
data[0] = prefix;
System.arraycopy(uriStr.getBytes(), 0, data, 1, uriStr.length()); NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_URI, new byte[0], data);
return record;
} boolean writeTag(NdefMessage message, Tag tag) {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
return false;
}
if (ndef.getMaxSize() < size) {
return false;
}
ndef.writeNdefMessage(message);
Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();
return true;
}
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
}

读写NFC标签的Uri 主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_SelectUri"
android:text="选择Uri" /> <TextView
android:id="@+id/textview_uri"
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标签靠近手机背面读取或写入Uri"
android:textColor="#F00"
android:textSize="16sp" /> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/read_nfc_tag" /> </LinearLayout>

显示URI列表Activity

 import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter; /**
* 显示URI列表
* @author dr
*
*/
public class UriListActivity extends ListActivity implements
OnItemClickListener {
private String uris[] = new String[] { "http://www.google.com",
"http://www.apple.com", "http://developer.apple.com",
"http://www.126.com", "ftp://192.168.17.160",
"https://192.168.17.120", "smb://192.168.17.100" }; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, uris);
setListAdapter(arrayAdapter);
getListView().setOnItemClickListener(this);
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent();
intent.putExtra("uri", uris[position]);
setResult(1, intent);
finish();
} }

显示NFC标签内容Activity

 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;
import cn.read.write.uri.library.UriRecord; /**
* 显示NFC标签内容
* @author dr
*
*/
public class ShowNFCTagContentActivity extends Activity {
private TextView mTagContent;
private Tag mDetectedTag;
private String mTagText; @Override
public void onCreate(Bundle savedInstanceState) {
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() + "\n max 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 ndefMessage = null; int contentSize = 0;
if (rawMsgs != null) {
if (rawMsgs.length > 0) {
ndefMessage = (NdefMessage) rawMsgs[0];
contentSize = ndefMessage.toByteArray().length;
} else {
return;
}
}
try {
NdefRecord ndefRecord = ndefMessage.getRecords()[0];
UriRecord uriRecord = UriRecord.parse(ndefRecord);
mTagText += uriRecord.getUri().toString() + "\n\nUri\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:layout_margin="6dp"
android:textSize="16sp" /> </LinearLayout>

AndroidManifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.eoe.read.write.uri"
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=".ReadWriteUriMainActivity"
android:label="读写NFC标签的Uri"
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:scheme="http" />
<data android:scheme="https" />
<data android:scheme="ftp" />
</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="cn.eoe.read.write.uri.ShowNFCTagContentActivity"
android:label="显示NFC标签内容" />
<activity
android:name="cn.eoe.read.write.uri.UriListActivity"
android:label="选择Uri" />
</application> </manifest>

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

12、NFC技术:读写NFC标签中的Uri数据的更多相关文章

  1. 10、NFC技术:读写NFC标签中的文本数据

    代码实现过程如下: 读写NFC标签的纯文本数据.java import java.nio.charset.Charset; import java.util.Locale; import androi ...

  2. android官方技术文档翻译——Case 标签中的常量字段

    本文译自androd官方技术文档<Non-constant Fields in Case Labels>,原文地址:http://tools.android.com/tips/non-co ...

  3. 11、NFC技术:NDEF Uri格式解析

    NDEF Uri格式规范 与NDEF文本格式一样,存储在NFC标签中的Uri也有一定的格式 http://www.nfc-forum.org/specs/spec_dashboard 编写可以解析Ur ...

  4. 【NFC】Android NFC API Reference中英文

    0 Near Field Communication Near Field Communication (NFC) is a set of   short-range wireless technol ...

  5. 6、Android中的NFC技术

    Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...

  6. 7、NFC技术:让Android自动运行程序

    用于描述NDEF格式数据的两个重要的类 NdefMessage:描述NDEF格式的信息 NdefRecord:描述NDEF信息的一个信息段  NdefMessage和NdefRecord是Androi ...

  7. Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord

    Android NFC技术(三)--初次开发Android NFC你须知道NdefMessage和NdefRecord 这最近也是有好多天没写博客了,除了到处张罗着搬家之外,依旧还是许许多多的琐事阻碍 ...

  8. 9、NFC技术:NDEF文本格式解析

    NDEF文本格式规范     不管什么格式的数据本质上都是由一些字节组成的.对于NDEF文本格式来说.这些数据的第1个字节描述了数据的状态,然后若干个字节描述文本的语言编码,最后剩余字节表示文本数据. ...

  9. iso15693芯片读写工具套件 icode-slix2读写 nfc type 5 tag读写

    iso15693芯片读写工具套件 icode-slix2读写 nfc type 5 tag读写校验套件 iso15693工具套件支持icode-slix,icode-slix2芯片的读写,支持iso1 ...

随机推荐

  1. Hadoop完全分布式集群配置

    1.前话 寒假实在太闲了,所以闲着无聊地去了解"大数据"这个新概念,这几年到处都在说什么大数据时代的,不能不让我感到好奇啊. 大数据有啥用?随便谷歌百度一大堆我也不多说了. 我自己 ...

  2. SQL中的Null深入研究分析

    SQL中的Null深入研究分析 虽然熟练掌握SQL的人对于Null不会有什么疑问,但总结得很全的文章还是很难找,看到一篇英文版的, 感觉还不错. Tony Hoare 在1965年发明了 null 引 ...

  3. Spring AOP 创建切面

        增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...

  4. PowerDesigner中name和code取消自动关联

    PowerDesigner中,如果修改了某个字段的name,其code也跟着修改,如果想取消,可以如下操作 解决方法如下: 1.选择Tools->GeneralOptions...菜单,出现Ge ...

  5. jmeter之配置文件介绍

    jmeter.bat, jmeter.properties在installpath/bin目录下 jmeter.bat文件包含如下内容: set HEAP=-Xms512m(初始化堆内存大小) -Xm ...

  6. linux rtc 接口【转】

    转自:http://blog.csdn.net/goldfighter/article/details/6126178 Linux操作系统内核对RTC的编程详解 转自: http://xenyinze ...

  7. flex 实时更新的一些方法总结

    诚为大家所知,Flash之所以优秀,就是它可以带来良好的用户体验和交互特性.既然涉及到交互性,开发人员在开发过程中必然会遇到实时更新的问题,那么,当后台数据发现变化的时候,如何让Flash端实时刷新显 ...

  8. HttpClient通过GET和POST获取网页内容

    中国银行支付网关---银行回调的接口 最简单的HTTP客户端,用来演示通过GET或者POST方式访问某个页面 /** * 中国银行支付网关---银行回调的接口 * @svncode svn://10. ...

  9. [lintcode the-smallest-difference]最小差(python)

    题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 ...

  10. Java NIO读书笔记2

    一.选择器(Selector) Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理多个channel ...