NDEF Uri格式规范

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

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

编写可以解析Uri格式数据的类

 import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import android.net.Uri;
import android.nfc.NdefRecord; public class UriRecord {
public static final Map<Byte, String> URI_PREFIX_MAP = new HashMap<Byte, String>();
static {
URI_PREFIX_MAP.put((byte) 0x00, "");
URI_PREFIX_MAP.put((byte) 0x01, "http://www.");
URI_PREFIX_MAP.put((byte) 0x02, "https://www.");
URI_PREFIX_MAP.put((byte) 0x03, "http://");
URI_PREFIX_MAP.put((byte) 0x04, "https://");
URI_PREFIX_MAP.put((byte) 0x05, "tel:");
URI_PREFIX_MAP.put((byte) 0x06, "mailto:");
URI_PREFIX_MAP.put((byte) 0x07, "ftp://anonymous:anonymous@");
URI_PREFIX_MAP.put((byte) 0x08, "ftp://ftp.");
URI_PREFIX_MAP.put((byte) 0x09, "ftps://");
URI_PREFIX_MAP.put((byte) 0x0A, "sftp://");
URI_PREFIX_MAP.put((byte) 0x0B, "smb://");
URI_PREFIX_MAP.put((byte) 0x0C, "nfs://");
URI_PREFIX_MAP.put((byte) 0x0D, "ftp://");
URI_PREFIX_MAP.put((byte) 0x0E, "dav://");
URI_PREFIX_MAP.put((byte) 0x0F, "news:");
URI_PREFIX_MAP.put((byte) 0x10, "telnet://");
URI_PREFIX_MAP.put((byte) 0x11, "imap:");
URI_PREFIX_MAP.put((byte) 0x12, "rtsp://");
URI_PREFIX_MAP.put((byte) 0x13, "urn:");
URI_PREFIX_MAP.put((byte) 0x14, "pop:");
URI_PREFIX_MAP.put((byte) 0x15, "sip:");
URI_PREFIX_MAP.put((byte) 0x16, "sips:");
URI_PREFIX_MAP.put((byte) 0x17, "tftp:");
URI_PREFIX_MAP.put((byte) 0x18, "btspp://");
URI_PREFIX_MAP.put((byte) 0x19, "btl2cap://");
URI_PREFIX_MAP.put((byte) 0x1A, "btgoep://");
URI_PREFIX_MAP.put((byte) 0x1B, "tcpobex://");
URI_PREFIX_MAP.put((byte) 0x1C, "irdaobex://");
URI_PREFIX_MAP.put((byte) 0x1D, "file://");
URI_PREFIX_MAP.put((byte) 0x1E, "urn:epc:id:");
URI_PREFIX_MAP.put((byte) 0x1F, "urn:epc:tag:");
URI_PREFIX_MAP.put((byte) 0x20, "urn:epc:pat:");
URI_PREFIX_MAP.put((byte) 0x21, "urn:epc:raw:");
URI_PREFIX_MAP.put((byte) 0x22, "urn:epc:");
URI_PREFIX_MAP.put((byte) 0x23, "urn:nfc:");
}
private final Uri mUri; private UriRecord(Uri uri) {
this.mUri = uri;
} public Uri getUri() {
return mUri;
} private static UriRecord parseAbsolute(NdefRecord ndefRecord) {
// 获得字节数据
byte[] payload = ndefRecord.getPayload();
// 把字符串转换成Uri对象。
Uri uri = Uri.parse(new String(payload, Charset.forName("UTF-8")));
return new UriRecord(uri);
} /**
* 处理已知类型的URI
* @param ndefRecord
* @return
*/
private static UriRecord parseWellKnown(NdefRecord ndefRecord) {
if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_URI))
return null;
// 获得所有字节数据
byte[] payload = ndefRecord.getPayload();
// 获得前缀(根据前面的隐射,查找对应的数据)
String prefix = URI_PREFIX_MAP.get(payload[0]);
byte[] prefixBytes = prefix.getBytes(Charset.forName("UTF-8"));
// 减 1 ,是隐射编码的长度。
byte[] fullUri = new byte[prefixBytes.length + payload.length - 1];
// 数组拷贝
System.arraycopy(prefixBytes, 0, fullUri, 0, prefixBytes.length);
System.arraycopy(payload, 1, fullUri, prefixBytes.length,
payload.length - 1);
// 生成一个URI。
Uri uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8")));
return new UriRecord(uri);
} /**
*
* @param record
* @return
*/
public static UriRecord parse(NdefRecord record) {
short tnf = record.getTnf();
if (tnf == NdefRecord.TNF_WELL_KNOWN) {
return parseWellKnown(record);
} else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {
return parseAbsolute(record);
}
throw new IllegalArgumentException("Unknown TNF " + tnf);
}
}

11、NFC技术:NDEF Uri格式解析的更多相关文章

  1. NFC(10)NDEF uri格式规范及读写示例(解析与封装ndef uri)

    只有遵守NDEF uri 格式规范的数据才能写到nfc标签上. NDEF uri 格式规范 uri 只有两部分: 第1个字节是uri协议映射值,如:0x01 表示uri以 http://www.开头. ...

  2. 9、NFC技术:NDEF文本格式解析

    NDEF文本格式规范     不管什么格式的数据本质上都是由一些字节组成的.对于NDEF文本格式来说.这些数据的第1个字节描述了数据的状态,然后若干个字节描述文本的语言编码,最后剩余字节表示文本数据. ...

  3. NFC(9)NDEF文本格式规范及读写示例(解析与封装ndef 文本)

    只有遵守NDEF文本格式规范的数据才能写到nfc标签上. NDEF文本格式规范 不管什么格式的数据本质上都是由一些字节组成的.对于NDEF文本格式来说. 1,这些数据的第1个字节描述了数据的状态, 2 ...

  4. android nfc中Ndef格式的读写

    1. 在onCreate()中获取NfcAdapter对象: NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); 2.在onNewI ...

  5. 12、NFC技术:读写NFC标签中的Uri数据

    功能实现,如下代码所示: 读写NFC标签的Uri 主Activity import cn.read.write.uri.library.UriRecord; import android.app.Ac ...

  6. 6、Android中的NFC技术

    Android对NFC技术的支持 Android2.3.1(API Level = 9)开始支持NFC技术,但Android2.x和Android3.x对NFC的支持非常有限.而从Android4.0 ...

  7. 8、NFC技术:让Android自动打开网页

    创建封装Uri的NdefRecord  public  NdefRecord  createUri(String  uriString);  public  NdefRecord  cre ...

  8. 7、NFC技术:让Android自动运行程序

    用于描述NDEF格式数据的两个重要的类 NdefMessage:描述NDEF格式的信息 NdefRecord:描述NDEF信息的一个信息段  NdefMessage和NdefRecord是Androi ...

  9. 转:YUV RGB 常见视频格式解析

    转: http://www.cnblogs.com/qinjunni/archive/2012/02/23/2364446.html YUV RGB 常见视频格式解析 I420是YUV格式的一种,而Y ...

随机推荐

  1. Hibernate的配置文件解析

    配置mybatis.xml或hibernate.cfg.xml报错: <property name="connection.url">jdbc:mysql://loca ...

  2. ehcache 缓存技术

    一 ehcache API: 1: Using the CacheManager 1.1所有ehcache的使用, 都是从 CacheManager. 开始的.有多种方法创建CacheManager实 ...

  3. 【转】Java中==、equals、hashcode的区别与重写equals以及hashcode方法实例

    原文地址:http://www.cnblogs.com/luankun0214/p/4421770.html 感谢网友的分享,记录下来只为学习. 1.重写equals方法实例   部分代码参考http ...

  4. HDU 4726 Kia's Calculation(贪心构造)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...

  5. [CFGym101061G] Repeat it(逆元)

    题目链接:http://codeforces.com/gym/101061/problem/G 题意:给一个数字n,让你重复m次,求最后这个数对1e9+7取模的结果. 思路:设数字n长度为k,重复m次 ...

  6. Ubuntu 安装vsftp软件(已测试)

    首先开启Ftp端口 使用apt-get命令安装vsftp #apt-get install vsftpd -y 添加ftp帐号和目录 先检查一下nologin的位置,通常在/usr/sbin/nolo ...

  7. hadoop2的伪分布部署

    通过我们前面的操作,已经可以编译并且打包产生适合本机的hadoop包,目录是/usr/local/hadoop-2.2.0-src/hadoop-dist/target/hadoop-2.2.0. 使 ...

  8. JOIN_TAB

    typedef struct st_join_table { st_join_table() {} /* Remove gcc warning */ TABLE *table; KEYUSE *key ...

  9. 如何在linux console中显示当前你在的branch?

    http://stackoverflow.com/questions/17333531/how-can-i-display-the-current-branch-and-folder-path-in- ...

  10. 51nod1627 瞬间移动

    打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... //打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... #include<cstd ...