13、NFC技术:读写非NDEF格式的数据
将NFC标签的存储区域分为16个页,每一个页可以存储4个字节,一个可存储64个字节(512位)。页码从0开始(0至15)。前4页(0至3)存储了NFC标签相关的信息(如NFC标签的序列号、控制位等)。从第5页开始存储实际的数据(4至15页)。
使用MifareUltralight.get方法获取MifareUltralight对象,然后调用MifareUltralight.connect方法进行连接,并使用MifareUltralight.writePage方法每次写入1页(4个字节)。也可以使用MifareUltralight.readPages方法每次连续读取4页。如果读取的页的序号超过15,则从头开始读。例如,从第15页(序号为14)开始读。readPages方法会读取14、15、0、1页的数据。

<?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格式的数据的更多相关文章
- 6.NFC之非NDEF格式
先看流程图 使用步骤: 第一步:声明权限 <!-- 允许应用程序使用NFC功能 --> <uses-permission android:name="android.per ...
- android nfc中Ndef格式的读写
1. 在onCreate()中获取NfcAdapter对象: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 2.在onNewI ...
- 6、Android中的NFC技术
Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...
- 7、NFC技术:让Android自动运行程序
用于描述NDEF格式数据的两个重要的类 NdefMessage:描述NDEF格式的信息 NdefRecord:描述NDEF信息的一个信息段 NdefMessage和NdefRecord是Androi ...
- volley post非json格式数据并获取json数据
在使用JsonObjectRequest时无法post非json格式的数据,因而采用StringRequest获取到相应的数据后再转为json格式的数据. //这里的上下文需要讨论 private s ...
- 9、NFC技术:NDEF文本格式解析
NDEF文本格式规范 不管什么格式的数据本质上都是由一些字节组成的.对于NDEF文本格式来说.这些数据的第1个字节描述了数据的状态,然后若干个字节描述文本的语言编码,最后剩余字节表示文本数据. ...
- 3.非标准的NDEF格式数据解析--IsoDep
1.使用目的:正常开发是针对NDEF格式数据进行开发,但实际情况并非如此,以厦门公交卡为例,厦门公交卡保存的是非NDEF格式数据.其类型是IsoDep类型. 2.非标准的NDEF格式数据流程:当厦门公 ...
- IO流9 --- 使用FileInputStream和FileOutputStream读写非文本文件 --- 技术搬运工(尚硅谷)
字节流读写非文本文件(图片.视频等) @Test public void test5(){ File srcFile = new File("FLAMING MOUNTAIN.JPG&quo ...
- Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
Android NFC技术(三)--初次开发Android NFC你须知道NdefMessage和NdefRecord 这最近也是有好多天没写博客了,除了到处张罗着搬家之外,依旧还是许许多多的琐事阻碍 ...
随机推荐
- 编码,加解密,签名,Hash
工作中会听到各种各样是是而非的词汇,base64,url,sha256,rsa,hash等等,你能很好的分清这些词语吗? 这次我想把它们统一的整理说明下: 一: 编码 编码是信息从一种形式或格式转换为 ...
- Delphi操作XML的几个博客
http://www.cnblogs.com/acuier 整整十几篇,省得我自己研究,学一下就可以了. http://www.cnblogs.com/del/category/113561.htm ...
- MainWndProc运行观察
MainWndProc运行观察 把MainWndProc改写成如下代码,便于观察:procedure TWinControl.MainWndProc(var Message: TMessage);be ...
- 用PostGreSQL实现三层(复习)
modal DAL,BLL都是类库的形式 最终结果如下: 数据库代码: -- Table: student -- DROP TABLE student; CREATE TABLE student ( ...
- Help Jimmy--poj1661(dp)
题目链接:http://poj.org/problem?id=1661 下图是左边的,右边的同理: #include<stdio.h> #include<string.h> # ...
- lua简化cocos2dx的Action动画序列
情景 今天写代码时,又要写一个很常见的动画,就是变大变小模拟那个弹性的赶脚,很常用但写起来挺麻烦,封装一下用起来就简单多了. 当然我也知道有缓动动画(EaseAction)可以实现反弹效果,但这不是重 ...
- wiremock之录制和回放
Recording is done by starting the standalone runner like this: $ java -jar wiremock-1.50-standalone. ...
- scroll 事件绑定
var animateBlock={ isVisiable:function(el,wh,st,delta){ delta=delta||200; ...
- ftrace的使用【转】
转自:http://blog.csdn.net/cybertan/article/details/8258394 This article explains how to set up ftrace ...
- 判断线段相交 -- 51nod 1264 线段相交
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1264 三角形的有向面积:a.x*b.y+b.x*c.y+c.x*a.y ...