1. 程式人生 > >NDEF文本格式解析

NDEF文本格式解析

ner 編碼 spa equal ima 文本格式 except lang ati

技術分享

技術分享

============》》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 }

NDEF文本格式解析