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 这最近也是有好多天没写博客了,除了到处张罗着搬家之外,依旧还是许许多多的琐事阻碍 ...
随机推荐
- IOS 视频分解图片、图片合成视频
在IOS视频处理中,视频分解图片和图片合成视频是IOS视频处理中经常遇到的问题,这篇博客就这两个部分对IOS视频图像的相互转换做一下分析. (1)视频分解图片 这里视频分解图片使用的是AVAssetI ...
- 286. Walls and Gates
题目: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an ob ...
- 267. Palindrome Permutation II
题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...
- SSIS -->> Data Type
SSIS和SQL SERVER, .NET数据类型的映射表
- VNC常用操作及常见问题解决办法汇总
VNC登录用户缺省是root,但在安装oracle时必须用oracle用户的身份登录,下面我们就以oracle为例说明如何配置VNC,从而可以使用不同的用户登录到主机.步骤描述如下: 步骤一:修 ...
- SQLite的介绍 操作Sqlite 具体实例
1.SQLite简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入 式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能 ...
- 深入理解Java内存模型(一)——基础
并发编程模型的分类 在并发编程中,我们需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来交换信息.在命令式编程中,线程之间的通信 ...
- js判断是移动端还是pc端
运行页面的时候,执行到js会判断来自于移动端还是pc端,如果是移动端则跳转制定链接地址,这样在手机端会有额外的不必要浪费的加载时间 var browser={ versions:function(){ ...
- BZOJ 1018 堵塞的交通traffic(线段树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1018 题意:一个2*n的格子,相邻格子之间有一条道路.初始时道路是不通的. 三种操作:( ...
- Jquery和一些Html控件
1.1 Jquery中如何获取各种Html控件的值 1.$("#ID").val(); 2.Check获取选中的值:$("#ID").is(&quo ...