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. uchome 积分体系

    一.总体流程 1):管理员在后台修改积分规则2):数据被写入数据表creditrule 中,并将数据写入缓存文件data/data_creditrule.php 中3):用户发表文章或者进行其他操作的 ...

  2. 针对安卓java入门:类和对象

    定义类 class Dog { String name; int age; void jump(){ } } 生成对象: public class Test { public static void ...

  3. 让fdisk输出更准确合理

    注意:新版本的fdisk默认输出已经正确合理了,本文只适用于旧版本的fdisk. 1 无option选项参数的输出 [root@localhost ~]# fdisk -l Disk /dev/sda ...

  4. Android 常用时间格式转换代码

    /** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date curren ...

  5. Centos 7下安装Oracle 12c

    SQL Server玩了有些年,最近想玩玩Oracle,于是想到装一台Oracle server来玩玩.第一次在Linux下安装Oracle,整个过程参考了一篇文章:http://blog.csdn. ...

  6. 开发ProxyServer的时候如何在一台PC上调试

    为了测试在真实的网络环境下你的ProxyServer性能如何,而你手头又只有一台电脑,怎么办? 打开你的ProxyServer(我用java写的,因此ProxyServer的进程是javaw.exe) ...

  7. SQL SERVER 常用字符类型的区别

    长度为 n 个字节的固定长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为 n 个字节.char 在 SQL-92 中的同义词为 character. ...

  8. 每天一个小算法(Shell sort5)

    希尔排序的关键在于步长的选取. 希尔排序的复杂度比较复杂,主要跟步长的选择有关,大概是 O(n logn^2),一般认为就是介于 O(n^2) 和 O(n logn) 之间.最好步长比较复杂,一般第一 ...

  9. pyhton小方法

    import osa = os.walk('.') for i in a: print(i)

  10. linux 防火墙打开端口/屏蔽IP等

    开启/禁用 sudo ufw allow|deny [service] 打开或关闭某个端口,例如: sudo ufw allow smtp 允许所有的外部IP访问本机的25/tcp (smtp)端口 ...