• 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 }
  • 相关阅读:
    labview dll 崩溃
    java方法01什么是方法?
    java控制流程控制10增强For循环
    Java方法05可变参数
    java流程控制09打印九九乘法表
    Java流程控制12打印三角形及DUG
    Java方法02方法的定义和调用
    Java流程控制08For循环详解
    java流程控制11break、continue、goto
    java方法04命令行传递参数
  • 原文地址:https://www.cnblogs.com/androidsj/p/3856437.html
Copyright © 2020-2023  润新知