13、NFC技术:读写非NDEF格式的数据

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 }

 

转载于:https://www.cnblogs.com/androidsj/p/3856437.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值