13、NFC技术:读写非NDEF格式的数据
2023-09-14 08:59:00 时间
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格式数据的程序
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <CheckBox 8 android:id="@+id/checkbox_write" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:text="是否向NFC标签写入数据" /> 12 13 <TextView 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:layout_marginBottom="5dp" 17 android:text="请将NFC标签或贴纸靠近手机背面" 18 android:textSize="16sp" /> 19 20 <ImageView 21 android:layout_width="match_parent" 22 android:layout_height="match_parent" 23 android:layout_margin="10dp" 24 android:src="@drawable/read_nfc_tag" /> 25 26 </LinearLayout>
1 import java.nio.charset.Charset; 2 3 import android.app.Activity; 4 import android.app.PendingIntent; 5 import android.content.Intent; 6 import android.nfc.NfcAdapter; 7 import android.nfc.Tag; 8 import android.nfc.tech.MifareUltralight; 9 import android.os.Bundle; 10 import android.widget.CheckBox; 11 import android.widget.Toast; 12 13 public class MifareultralightMainActivity extends Activity { 14 15 private CheckBox mWriteData; 16 private NfcAdapter mNfcAdapter; 17 private PendingIntent mPendingIntent; 18 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 23 setContentView(R.layout.activity_mifareultralight); 24 mWriteData = (CheckBox) findViewById(R.id.checkbox_write); 25 26 mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 27 mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, 28 getClass()), 0); 29 } 30 31 @Override 32 public void onResume() { 33 super.onResume(); 34 if (mNfcAdapter != null) { 35 mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, 36 null); 37 } 38 } 39 40 @Override 41 public void onPause() { 42 super.onPause(); 43 if (mNfcAdapter != null) { 44 mNfcAdapter.disableForegroundDispatch(this); 45 } 46 } 47 48 @Override /** 处理标签 */ 49 public void onNewIntent(Intent intent) { 50 // 获得TAG对象。 51 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 52 // 技术列表规格也就是数据支持格式。 53 String[] techList = tag.getTechList(); 54 55 boolean haveMifareUltralight = false; 56 for (String tech : techList) { // 判断是否有支持的数据格式。 57 if (tech.indexOf("MifareUltralight") >= 0) { 58 haveMifareUltralight = true; 59 break; 60 } 61 } 62 if (!haveMifareUltralight) { 63 Toast.makeText(this, "不支持MifareUltralight数据格式", Toast.LENGTH_LONG) 64 .show(); 65 return; 66 } 67 if (mWriteData.isChecked()) { 68 writeTag(tag); // 向NFC写入数据。 69 } else { 70 String data = readTag(tag); // 读取数据。 71 if (data != null) 72 Toast.makeText(this, data, Toast.LENGTH_LONG).show(); 73 } 74 75 } 76 77 public void writeTag(Tag tag) { 78 MifareUltralight ultralight = MifareUltralight.get(tag); 79 try { 80 ultralight.connect(); 81 // 从第五页开始写,因为从0-3前四页是存储系统数据的。 82 ultralight.writePage(4, "中国".getBytes(Charset.forName("GB2312"))); 83 ultralight.writePage(5, "美国".getBytes(Charset.forName("GB2312"))); 84 ultralight.writePage(6, "英国".getBytes(Charset.forName("GB2312"))); 85 ultralight.writePage(7, "法国".getBytes(Charset.forName("GB2312"))); 86 87 Toast.makeText(this, "成功写入MifareUltralight格式数据!", Toast.LENGTH_LONG) 88 .show(); 89 } catch (Exception e) { 90 // TODO: handle exception 91 } finally { 92 try { 93 ultralight.close(); 94 } catch (Exception e) { 95 // TODO: handle exception 96 } 97 } 98 } 99 100 /** 读取数据,把字节转换成字符串,要不然字节无法显示 */ 101 public String readTag(Tag tag) { 102 MifareUltralight ultralight = MifareUltralight.get(tag); 103 104 try { 105 ultralight.connect(); 106 // 从第5页开始读取。 107 byte[] data = ultralight.readPages(4); 108 return new String(data, Charset.forName("GB2312")); 109 } catch (Exception e) { 110 // TODO: handle exception 111 } finally { 112 try { 113 ultralight.close(); 114 } catch (Exception e) { 115 // TODO: handle exception 116 } 117 } 118 return null; 119 } 120 121 }
相关文章
- 【华为云技术分享】直播回顾丨激发数据裂变新动能,HDC.Cloud云数据库前沿技术解读
- 【华为云技术分享】唐老师带你秒懂大数据,以及Spark和Flink在干啥咧
- 两个框架之间的数据类型转换,需要桥接技术
- OTL技术应用
- iOS中 UIWebView加载网络数据 技术分享
- CSS3中的弹性流体盒模型技术详解
- 【模块04-大数据技术入门】01节-需要的基础知识
- 数据分析技术:数据差异的显著性检验
- 13、NFC技术:读写非NDEF格式的数据
- Atitit web 3.0(web技术)展望与实现 和语义网络 目录 1. 为了说明Web 3.0,我们需要回顾Web历史上的重要浪潮。2 1.1. 2.Web 2.0:任何人可以参与。Web
- Atitit 2019技术趋势与没落技术 目录 1.1. abcdAtitit 技术领域趋势 abcd研究总结AI(人工智能)BlockChain(区块链)、Cloud(云)、和Data(大数据)
- Atitit.cto 与技术总监的区别
- paip.提升用户体验----gcc c++ JIT-debugging 技术
- 关于将本地 SAP UI5 应用配置到本地 Fiori Launchpad 的技术实现深入讲解试读版
- 【阿里在线技术峰会】蒋晓伟:Blink计算引擎
- 华为云数据库亮相下一代数据技术发展论坛,助力“数字一带一路”
- 【华为云技术分享】云图说 | 初识华为云存储容灾服务:为您的云上数据保驾护航
- 【华为云技术分享】基于小熊派STM32芯片的通过MQTT上报JSON数据到华为物联网平台的自动售货机Demo解析
- 第十三讲:MSTP技术应用
- LabVIEW编程LabVIEW开发高级数据采集技术 操作数字IO 例程与相关资料
- Spring技术内幕:Spring AOP的实现原理(一)
- 程序人生:测试开发工程师必备技术栈(附详细技术点)
- 无文件攻击形式——在注册表中保存加密数据,注入代码到正在运行的进程,并使用PowerShell、Windows Management Instrumentation和其他技术使其难以被检测以及分析。
- 万亿级日志与行为数据存储查询技术剖析(续)——Tindex是改造的lucene和druid