NDEF文本格式解析

 

============》》Record Type Definition Technical Specificaltions

 

 

 

 

 1 public class TextRecord {
 2     private final String mText;
 3 
 4     private TextRecord(String text) {
 5         // TODO Auto-generated constructor stub
 6 
 7         mText = text;
 8     }
 9 
10     public String getText() {
11         return mText;
12     }
13 
14     public static TextRecord parse(NdefRecord ndefRecord) {
15         //
16         if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) {
17             return null;
18         }
19 
20         if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
21             return null;
22         }
23 
24         try {
25 
26             byte[] palyload = ndefRecord.getPayload();
27             // 根据最高位判断字符编码
28             String textEncoding = ((palyload[0] & 0x80) == 0) ? "UTF-8"
29                     : "UTF-16";
30             // 根据第六位获得语言编码长度
31             int languageCodeLength = palyload[0] & 0x3f;
32             // 获得语言编码
33             String languageCod = new String(palyload, 1, languageCodeLength,
34                     "US-ASCII");
35 
36             String text = new String(palyload, languageCodeLength + 1,
37                     palyload.length - languageCodeLength - 1, textEncoding);
38 
39             return new TextRecord(text);
40 
41         } catch (Exception e) {
42             // TODO: handle exception
43             throw new IllegalArgumentException();
44         }
45 
46     }
47 }

 

posted @ 2017-05-26 21:43  张兮兮  阅读(2043)  评论(3编辑  收藏  举报