Easy Tag Write(2)
package skyseraph.android.util.nfc; import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap; import skyseraph.android.util.LogUtil;
import skyseraph.android.util.MyConstant; import android.annotation.SuppressLint;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord; import java.nio.charset.Charset;
import java.util.Locale; /**
* @Title :BobNdefMessage.java
* @Package :skyseraph.android.util.nfc
* @ClassName : BobNdefMessage
* @Description :Custom Class For NdefMessage
* @author : skyseraph00@163.com
* @date : 2013-5-13 上午11:14:51
* @version : V1.0 《Android NFC 开发实战详解》
*/
public class BobNdefMessage {
private static final String TAG_ASSIST = "[BobNdefMessage]-"; /**
* @About:create a TNF_WELL_KNOW NDEF record as RTD_URI
* @param uriFiledStr , The rest of the URI, or the entire URI (if
* identifier code is 0x00).
* @param identifierCode = prefixes(URI identifier code), 0x01=http://www.
* ,0x02=https://www. , 0x03=http://
* @param flagAddAAR, true means add AAR
* @return NdefMessage
* @Ref: NFCForum-TS-RTD_URI_1.0
* @By SkySeraph-2013
*/
public static NdefMessage getNdefMsg_from_RTD_URI(String uriFiledStr, byte identifierCode,
boolean flagAddAAR) {
LogUtil.i(MyConstant.TAG, TAG_ASSIST + "into getNdefMsg_from_RTD_URI");
byte[] uriField = uriFiledStr.getBytes(Charset.forName("US-ASCII"));
byte[] payLoad = new byte[uriField.length + 1]; // add 1 for the URI
// Prefix
payLoad[0] = identifierCode; // 0x01 = prefixes http://www. to the URI
// appends URI to payload
System.arraycopy(uriField, 0, payLoad, 1, uriField.length); // Method1:
NdefRecord rtdUriRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI,
new byte[0], payLoad); // Method2:only in API 14
String prefix = URI_PREFIX_MAP.get(identifierCode);
NdefRecord rtdUriRecord2 = NdefRecord.createUri(prefix + uriFiledStr); // Method3:only in API 14
NdefRecord rtdUriRecord3 = NdefRecord.createUri(Uri.parse(prefix + uriFiledStr)); if (flagAddAAR) {
// note: returns AAR for different app (nfcreadtag)
return new NdefMessage(new NdefRecord[] {
rtdUriRecord1, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
}); // packageName
} else {
return new NdefMessage(new NdefRecord[] {
rtdUriRecord1
});
}
} /**
* @About:create a TNF_WELL_KNOW NDEF record as RTD_TEXT
* @param text , the really text data
* @param encodeInUtf8 , false means TEXT encoded by UTF-8
* @param flagAddAAR , true means add AAR
* @return NdefMessage
* @Ref: NFCForum-TS-RTD_Text_1.0
* @By SkySeraph-2013
*/
public static NdefMessage getNdefMsg_from_RTD_TEXT(String text, boolean encodeInUtf8,
boolean flagAddAAR) {
LogUtil.i(MyConstant.TAG, TAG_ASSIST + "into getNdefMsg_from_RTD_TEXT"); Locale locale = new Locale("en", "US"); // a new Locale is created with
// US English.
byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
// boolean encodeInUtf8 = false;
Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char)(utfBit + langBytes.length);
// String text = "This is an RTD_TEXT exp";
byte[] textBytes = text.getBytes(utfEncoding);
byte[] data = new byte[1 + langBytes.length + textBytes.length];
data[0] = (byte)status;
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT,
new byte[0], data); if (flagAddAAR) {
// note: returns AAR for different app (nfcreadtag)
return new NdefMessage(new NdefRecord[] {
textRecord, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
});
} else {
return new NdefMessage(new NdefRecord[] {
textRecord
});
}
} /**
* @About: create a TNF_ABSOLUTE_URI NDEF record
* @param absoluteUri ,the absolute Uri
* @param flagAddAAR , true means add AAR
* @return NdefMessage
* @Note: TNF_ABSOLUTE_URI indicates the absolute form of a URI that follows
* the absolute-URI rule defined by RFC 3986
* @Note: Recommend that you use the RTD_URI type instead of
* TNF_ABSOLUTE_URI, because it is more efficient
* @By SkySeraph-2013
*/
public static NdefMessage getNdefMsg_from_ABSOLUTE_URI(String absoluteUri, boolean flagAddAAR) {
LogUtil.i(MyConstant.TAG, TAG_ASSIST + "into getNdefMsg_from_ABSOLUTE_URI");
// String absoluteUri = "http://developer.android.com/index.html";
byte[] absoluteUriBytes = absoluteUri.getBytes(Charset.forName("US-ASCII"));
NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, absoluteUriBytes,
new byte[0], new byte[0]);
if (flagAddAAR) {
// note: returns AAR for different app (nfcreadtag)
return new NdefMessage(new NdefRecord[] {
uriRecord, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
});
} else {
return new NdefMessage(new NdefRecord[] {
uriRecord
});
}
} /**
* @About:create a TNF_MIME_MEDIA NDEF record
* @param payLoad,the MIME data
* @param mimeType,the MIME Type
* @param flagAddAAR, true means add AAR
* @return NdefMessage
* @By SkySeraph-2013
*/
@SuppressLint("NewApi")
public static NdefMessage getNdefMsg_from_MIME_MEDIA(String payLoad, String mimeType,
boolean flagAddAAR) {
LogUtil.i(MyConstant.TAG, TAG_ASSIST + "into getNdefMsg_from_MIME_MEDIA");
byte[] payLoadBytes = payLoad.getBytes(Charset.forName("US-ASCII"));
// String mimeType = "application/skyseraph.nfc_demo"; // method1:Creating the NdefRecord manually
NdefRecord mimeRecord1 = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
mimeType.getBytes(Charset.forName("US-ASCII")), new byte[0], payLoadBytes);
// the identfier of the record is given as 0, since it will be the first
// record in the NdefMessage // method2:Using the createMime() method, in API-16
NdefRecord mimeRecord2 = NdefRecord.createMime(mimeType, payLoadBytes); if (flagAddAAR) {
// note: returns AAR for different app (nfcreadtag)
return new NdefMessage(new NdefRecord[] {
mimeRecord1, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
});
} else {
return new NdefMessage(new NdefRecord[] {
mimeRecord1
});
}
} /**
* @About:create a TNF_EXTERNAL_TYPE NDEF record
* @param payLoad,the EXTERNAL data
* @param flagAddAAR, true means add AAR
* @return NdefMessage
* @By SkySeraph-2013
*/
@SuppressLint("NewApi")
public static NdefMessage getNdefMsg_from_EXTERNAL_TYPE(String payLoad, boolean flagAddAAR) {
LogUtil.i(MyConstant.TAG, TAG_ASSIST + "into getNdefMsg_from_EXTERNAL_TYPE");
byte[] payLoadBytes = payLoad.getBytes();
String domain = "skyseraph.nfc_demo"; // usually your app's package name
String type = "externalType";
String externalType = domain + ":" + type; // method1:Creating the NdefRecord manually
NdefRecord exteralRecord1 = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE,
externalType.getBytes(), new byte[0], payLoadBytes); // method2:Using the createExternal() method, in API-16
NdefRecord exteralRecord2 = NdefRecord.createExternal(domain, type, payLoadBytes); if (flagAddAAR) {
// note: returns AAR for different app (nfcreadtag)
return new NdefMessage(new NdefRecord[] {
exteralRecord1, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
});
} else {
return new NdefMessage(new NdefRecord[] {
exteralRecord1
});
}
} /**
* checkSystemVersion()
*/
private boolean flagVersion = false; private void checkSystemVersion() {
// TODO Auto-generated method stub
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) LogUtil.i(MyConstant.TAG, TAG_ASSIST + "Product Model=" + android.os.Build.MODEL + ", "
+ android.os.Build.VERSION.SDK + ", " + android.os.Build.VERSION.RELEASE);
String systemModel;
String releaseVersion;
String sdkVersion;
systemModel = android.os.Build.MODEL;
sdkVersion = android.os.Build.VERSION.SDK;
releaseVersion = android.os.Build.VERSION.RELEASE;
if (Integer.parseInt(sdkVersion) > 15) {
flagVersion = true;
} else {
flagVersion = false;
LogUtil.e(MyConstant.TAG, TAG_ASSIST + "Your android system version is low to API-16");
}
} /**
* NFC Forum "URI Record Type Definition" This is a mapping of
* "URI Identifier Codes" to URI string prefixes, per section 3.2.2 of the
* NFC Forum URI Record Type Definition document.
*/
private static final BiMap<Byte, String> URI_PREFIX_MAP = ImmutableBiMap
.<Byte, String> builder().put((byte)0x00, "").put((byte)0x01, "http://www.")
.put((byte)0x02, "https://www.").put((byte)0x03, "http://").put((byte)0x04, "https://")
.put((byte)0x05, "tel:").put((byte)0x06, "mailto:")
.put((byte)0x07, "ftp://anonymous:anonymous@").put((byte)0x08, "ftp://ftp.")
.put((byte)0x09, "ftps://").put((byte)0x0A, "sftp://").put((byte)0x0B, "smb://")
.put((byte)0x0C, "nfs://").put((byte)0x0D, "ftp://").put((byte)0x0E, "dav://")
.put((byte)0x0F, "news:").put((byte)0x10, "telnet://").put((byte)0x11, "imap:")
.put((byte)0x12, "rtsp://").put((byte)0x13, "urn:").put((byte)0x14, "pop:")
.put((byte)0x15, "sip:").put((byte)0x16, "sips:").put((byte)0x17, "tftp:")
.put((byte)0x18, "btspp://").put((byte)0x19, "btl2cap://").put((byte)0x1A, "btgoep://")
.put((byte)0x1B, "tcpobex://").put((byte)0x1C, "irdaobex://")
.put((byte)0x1D, "file://").put((byte)0x1E, "urn:epc:id:")
.put((byte)0x1F, "urn:epc:tag:").put((byte)0x20, "urn:epc:pat:")
.put((byte)0x21, "urn:epc:raw:").put((byte)0x22, "urn:epc:")
.put((byte)0x23, "urn:nfc:").build();
}
Easy Tag Write(2)的更多相关文章
- Easy Tag Write(3.3)
package skyseraph.android.util; /** * @Title : Constant.java * @Package : tcl.nfc.tv.util * @ClassNa ...
- Easy Tag Write(3.2)
package skyseraph.android.util; /** * @Title : LogUtil.java * @Package : tcl.nfc.phone.util * @Class ...
- Easy Tag Write(1)
package skyseraph.easytagwrite; import skyseraph.android.util.CustomDialog; import skyseraph.android ...
- Easy Tag Write(3.1)
package skyseraph.android.util; import skyseraph.easytagwrite.R; import android.app.Dialog; import a ...
- [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode] 101. Symmetric Tree_ Easy tag: BFS
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- JUnit5学习之五:标签(Tag)和自定义注解
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
随机推荐
- Tomcat配置HTTPS方式(单向)
简要记录主要步骤备忘 1.进入到jdk下的bin目录 2.输入如下指令 keytool -v -genkey -alias tomcat -keyalg RSA -keystore d:/tomcat ...
- css3折叠效果
在开发过程中,经常会遇到一些交互效果,今天所联系的便是一个类似折纸的折叠效果,查看效果. 说到折纸,我们先看下图 这是我第一时间想到的大体思路,如果能让这6个面连续的变化角度到0不就可以了吗,运用cs ...
- Android -- FlipViewController的使用(jar文件)
1. 此控件的作用 类似于翻书一样,可以上下翻页的控件, 2. 效果图
- Linux下redis的安装
第一部分:安装redis 希望将redis安装到此目录 /usr/local/redis 希望将安装包下载到此目录 /usr/local/src 那么安装过程指令如下: $ mkdir /usr/lo ...
- <三>JDBC_面向对象思想的体现
JDBCTools.java import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;i ...
- apache配置rewrite及.htaccess文件(转载)
今天看到一个哥们的帖子发了个rewrite的帖子,以前也写过一个,配置挺简单的,但当时没注意这个问题,当时没有用到.htaccess文件,在机子上测试了一下,发现确实没法用,于是开始找问题的所在. 自 ...
- Django入门实践(3)
Django简单应用 前面简单示例说明了views和Template的工作过程,但是Django最核心的是App,涉及到App则会和Model(数据库)打交道.下面举的例子是创建一个简单应用wiki ...
- HTML基础篇之HTML基本结构
课堂知识总结 第一接触和学习HTML知识在学习过程中对所属的标签的自己认为的理解和解释. HTML元素:文档里面的标签和内容. 比如:<h1>大家好</h1> 左边的是开始标 ...
- 安卓中級教程(4):ScrollView與ListView之間的高度問題
在scrollView中加插ListView是一個大難題.其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 . <LinearLayout xmlns: ...
- final阶段成员贡献分
项目名:连连看 组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 final阶段各组员的贡献分分配如下: 姓名 个人工作量 组长评价 个人评价 团队贡献总分 张政 11 7 6 6.00 ...