MifareUltralight数据格式

将NFC标签的存储区域分为16个页,每一个页可以存储4个字节,一个可存储64个字节(512位)。页码从0开始(0至15)。前4页(0至3)存储了NFC标签相关的信息(如NFC标签的序列号、控制位等)。从第5页开始存储实际的数据(4至15页)。

读写MifareUltralight数据

使用MifareUltralight.get方法获取MifareUltralight对象,然后调用MifareUltralight.connect方法进行连接,并使用MifareUltralight.writePage方法每次写入1页(4个字节)。也可以使用MifareUltralight.readPages方法每次连续读取4页。如果读取的页的序号超过15,则从头开始读。例如,从第15页(序号为14)开始读。readPages方法会读取14、15、0、1页的数据。

编写读写MifareUltralight格式数据的程序
 
 <?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" > <CheckBox
android:id="@+id/checkbox_write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="是否向NFC标签写入数据" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="请将NFC标签或贴纸靠近手机背面"
android:textSize="16sp" /> <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:src="@drawable/read_nfc_tag" /> </LinearLayout>
 import java.nio.charset.Charset;

 import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareUltralight;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.Toast; public class MifareultralightMainActivity extends Activity { private CheckBox mWriteData;
private NfcAdapter mNfcAdapter;
private PendingIntent mPendingIntent; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_mifareultralight);
mWriteData = (CheckBox) findViewById(R.id.checkbox_write); mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()), 0);
} @Override
public void onResume() {
super.onResume();
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
null);
}
} @Override
public void onPause() {
super.onPause();
if (mNfcAdapter != null) {
mNfcAdapter.disableForegroundDispatch(this);
}
} @Override /** 处理标签 */
public void onNewIntent(Intent intent) {
// 获得TAG对象。
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// 技术列表规格也就是数据支持格式。
String[] techList = tag.getTechList(); boolean haveMifareUltralight = false;
for (String tech : techList) { // 判断是否有支持的数据格式。
if (tech.indexOf("MifareUltralight") >= 0) {
haveMifareUltralight = true;
break;
}
}
if (!haveMifareUltralight) {
Toast.makeText(this, "不支持MifareUltralight数据格式", Toast.LENGTH_LONG)
.show();
return;
}
if (mWriteData.isChecked()) {
writeTag(tag); // 向NFC写入数据。
} else {
String data = readTag(tag); // 读取数据。
if (data != null)
Toast.makeText(this, data, Toast.LENGTH_LONG).show();
} } public void writeTag(Tag tag) {
MifareUltralight ultralight = MifareUltralight.get(tag);
try {
ultralight.connect();
// 从第五页开始写,因为从0-3前四页是存储系统数据的。
ultralight.writePage(4, "中国".getBytes(Charset.forName("GB2312")));
ultralight.writePage(5, "美国".getBytes(Charset.forName("GB2312")));
ultralight.writePage(6, "英国".getBytes(Charset.forName("GB2312")));
ultralight.writePage(7, "法国".getBytes(Charset.forName("GB2312"))); Toast.makeText(this, "成功写入MifareUltralight格式数据!", Toast.LENGTH_LONG)
.show();
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
ultralight.close();
} catch (Exception e) {
// TODO: handle exception
}
}
} /** 读取数据,把字节转换成字符串,要不然字节无法显示 */
public String readTag(Tag tag) {
MifareUltralight ultralight = MifareUltralight.get(tag); try {
ultralight.connect();
// 从第5页开始读取。
byte[] data = ultralight.readPages(4);
return new String(data, Charset.forName("GB2312"));
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
ultralight.close();
} catch (Exception e) {
// TODO: handle exception
}
}
return null;
} }

13、NFC技术:读写非NDEF格式的数据的更多相关文章

  1. 6.NFC之非NDEF格式

    先看流程图 使用步骤: 第一步:声明权限 <!-- 允许应用程序使用NFC功能 --> <uses-permission android:name="android.per ...

  2. android nfc中Ndef格式的读写

    1. 在onCreate()中获取NfcAdapter对象: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 2.在onNewI ...

  3. 6、Android中的NFC技术

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

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

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

  5. volley post非json格式数据并获取json数据

    在使用JsonObjectRequest时无法post非json格式的数据,因而采用StringRequest获取到相应的数据后再转为json格式的数据. //这里的上下文需要讨论 private s ...

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

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

  7. 3.非标准的NDEF格式数据解析--IsoDep

    1.使用目的:正常开发是针对NDEF格式数据进行开发,但实际情况并非如此,以厦门公交卡为例,厦门公交卡保存的是非NDEF格式数据.其类型是IsoDep类型. 2.非标准的NDEF格式数据流程:当厦门公 ...

  8. IO流9 --- 使用FileInputStream和FileOutputStream读写非文本文件 --- 技术搬运工(尚硅谷)

    字节流读写非文本文件(图片.视频等) @Test public void test5(){ File srcFile = new File("FLAMING MOUNTAIN.JPG&quo ...

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

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

随机推荐

  1. 源码深度解析SpringMvc请求运行机制(转)

    源码深度解析SpringMvc请求运行机制 本文依赖的是springmvc4.0.5.RELEASE,通过源码深度解析了解springMvc的请求运行机制.通过源码我们可以知道从客户端发送一个URL请 ...

  2. struts2与struts1整合,java.lang.InstantiationException, Exception occurred during processing request: null

    做了2个action,其中一个运行没有问题,另一个报错,看下面的报错信息,再看了看struts.xml,因为没有给GetBooks这个action配置actionform,所以就导致报null.下面是 ...

  3. ubuntu安装postgresql与postgis

    版本信息 ubuntu    14.04.1LTS postgresql   9.3.5 postgis       2.1.2 今天尝试着安装了postgis 2.1.2,(较简便的包安装,不是源码 ...

  4. Fedora 15 KDE中如何打开software management及如何应用

    Fedora 15 KDE中如何打开software management级如何应用 software management中有转载和卸载软件(Get and remove software)的功能 ...

  5. 在Visual C++下搭建OpenGL的开发环境

    1.确保你的电脑已经安装了visual c++编译器 如果还没安装的话,这里有个安装包,可以复制链接进行下载:http://pan.baidu.com/s/1bn4XTqn   2.下载GLUT 下载 ...

  6. centos 升级GCC/G++

    #get rep yum install centos-release-scl-rh #yum install centos-release-scl # install g++ 5.2.1 yum - ...

  7. ACdream 1735 输油管道 (排序)

    http://acdream.info/problem?pid=1735 官方题解:http://acdream.info/topic?tid=4246 因为主干线是平行于x轴的直线,那么跟x坐标其实 ...

  8. HeadFirst Jsp 07 (使用 jsp)

    Jsp 变成 Servlet, 容器会查看你的JSP, 把它转换成java源代码, 再编译成完整的Java servlet类. Jsp 不需要你的编译, 容器会自动替换成servlet. 在 jsp中 ...

  9. Android应用主界面底部菜单实现

    介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的  <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...

  10. POJ 3107 Godfather (树形dp)

    题目链接 虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久. 还有据说这个题用vector会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...