11、NFC技术:NDEF Uri格式解析

NDEF Uri格式规范

与NDEF文本格式一样,存储在NFC标签中的Uri也有一定的格式

http://www.nfc-forum.org/specs/spec_dashboard

编写可以解析Uri格式数据的类
 
  1 import java.nio.charset.Charset;
  2 import java.util.Arrays;
  3 import java.util.HashMap;
  4 import java.util.Map;
  5 import android.net.Uri;
  6 import android.nfc.NdefRecord;
  7 
  8 public class UriRecord {
  9     public static final Map<Byte, String> URI_PREFIX_MAP = new HashMap<Byte, String>();
 10     static {
 11         URI_PREFIX_MAP.put((byte) 0x00, "");
 12         URI_PREFIX_MAP.put((byte) 0x01, "http://www.");
 13         URI_PREFIX_MAP.put((byte) 0x02, "https://www.");
 14         URI_PREFIX_MAP.put((byte) 0x03, "http://");
 15         URI_PREFIX_MAP.put((byte) 0x04, "https://");
 16         URI_PREFIX_MAP.put((byte) 0x05, "tel:");
 17         URI_PREFIX_MAP.put((byte) 0x06, "mailto:");
 18         URI_PREFIX_MAP.put((byte) 0x07, "ftp://anonymous:anonymous@");
 19         URI_PREFIX_MAP.put((byte) 0x08, "ftp://ftp.");
 20         URI_PREFIX_MAP.put((byte) 0x09, "ftps://");
 21         URI_PREFIX_MAP.put((byte) 0x0A, "sftp://");
 22         URI_PREFIX_MAP.put((byte) 0x0B, "smb://");
 23         URI_PREFIX_MAP.put((byte) 0x0C, "nfs://");
 24         URI_PREFIX_MAP.put((byte) 0x0D, "ftp://");
 25         URI_PREFIX_MAP.put((byte) 0x0E, "dav://");
 26         URI_PREFIX_MAP.put((byte) 0x0F, "news:");
 27         URI_PREFIX_MAP.put((byte) 0x10, "telnet://");
 28         URI_PREFIX_MAP.put((byte) 0x11, "imap:");
 29         URI_PREFIX_MAP.put((byte) 0x12, "rtsp://");
 30         URI_PREFIX_MAP.put((byte) 0x13, "urn:");
 31         URI_PREFIX_MAP.put((byte) 0x14, "pop:");
 32         URI_PREFIX_MAP.put((byte) 0x15, "sip:");
 33         URI_PREFIX_MAP.put((byte) 0x16, "sips:");
 34         URI_PREFIX_MAP.put((byte) 0x17, "tftp:");
 35         URI_PREFIX_MAP.put((byte) 0x18, "btspp://");
 36         URI_PREFIX_MAP.put((byte) 0x19, "btl2cap://");
 37         URI_PREFIX_MAP.put((byte) 0x1A, "btgoep://");
 38         URI_PREFIX_MAP.put((byte) 0x1B, "tcpobex://");
 39         URI_PREFIX_MAP.put((byte) 0x1C, "irdaobex://");
 40         URI_PREFIX_MAP.put((byte) 0x1D, "file://");
 41         URI_PREFIX_MAP.put((byte) 0x1E, "urn:epc:id:");
 42         URI_PREFIX_MAP.put((byte) 0x1F, "urn:epc:tag:");
 43         URI_PREFIX_MAP.put((byte) 0x20, "urn:epc:pat:");
 44         URI_PREFIX_MAP.put((byte) 0x21, "urn:epc:raw:");
 45         URI_PREFIX_MAP.put((byte) 0x22, "urn:epc:");
 46         URI_PREFIX_MAP.put((byte) 0x23, "urn:nfc:");
 47     }
 48     private final Uri mUri;
 49 
 50     private UriRecord(Uri uri) {
 51         this.mUri = uri;
 52     }
 53 
 54     public Uri getUri() {
 55         return mUri;
 56     }
 57 
 58     private static UriRecord parseAbsolute(NdefRecord ndefRecord) {
 59         // 获得字节数据
 60         byte[] payload = ndefRecord.getPayload();
 61         // 把字符串转换成Uri对象。
 62         Uri uri = Uri.parse(new String(payload, Charset.forName("UTF-8")));
 63         return new UriRecord(uri);
 64     }
 65 
 66     /**
 67      * 处理已知类型的URI
 68      * @param ndefRecord
 69      * @return
 70      */
 71     private static UriRecord parseWellKnown(NdefRecord ndefRecord) {
 72         if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_URI))
 73             return null;
 74         // 获得所有字节数据
 75         byte[] payload = ndefRecord.getPayload();
 76         // 获得前缀(根据前面的隐射,查找对应的数据)
 77         String prefix = URI_PREFIX_MAP.get(payload[0]);
 78         byte[] prefixBytes = prefix.getBytes(Charset.forName("UTF-8"));
 79         // 减 1 ,是隐射编码的长度。
 80         byte[] fullUri = new byte[prefixBytes.length + payload.length - 1];
 81         // 数组拷贝
 82         System.arraycopy(prefixBytes, 0, fullUri, 0, prefixBytes.length);
 83         System.arraycopy(payload, 1, fullUri, prefixBytes.length,
 84                 payload.length - 1);
 85         // 生成一个URI。
 86         Uri uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8")));
 87         return new UriRecord(uri);
 88     }
 89     
 90     /**
 91      * 
 92      * @param record
 93      * @return
 94      */
 95     public static UriRecord parse(NdefRecord record) {
 96         short tnf = record.getTnf();
 97         if (tnf == NdefRecord.TNF_WELL_KNOWN) {
 98             return parseWellKnown(record);
 99         } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {
100             return parseAbsolute(record);
101         }
102         throw new IllegalArgumentException("Unknown TNF " + tnf);
103     }
104 }

 

posted on 2014-07-20 17:27  大米稀饭  阅读(1268)  评论(0编辑  收藏  举报