The method NdefRecord.createTextRecord()
was introduced in API level 21. Consequently, it is not available on platforms below that API level. However, you could easily assemble the Text record on your own. The payload of a Text record consists of a status byte, a language code field, and a text field:
+-------------+---------------+--------------------------+
| Status byte | Language code | Text |
| (1 byte) | (n byte) | (m byte) |
+-------------+---------------+--------------------------+
- The status byte indicates the character encoding of the text field (
0
= UTF-8, 1
= UTF-16) in bit 7 and the length n
of the language code in bits 5..0. Bit 6 must always be zero.
- The language code filed contains an IANA language code encoded in US-ASCII (e.g. "en").
You could create the Text record using this method:
public static NdefRecord createTextRecord(String language, String text) {
byte[] languageBytes;
byte[] textBytes;
try {
languageBytes = language.getBytes("US-ASCII");
textBytes = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
byte[] recordPayload = new byte[1 + (languageBytes.length & 0x03F) + textBytes.length];
recordPayload[0] = (byte)(languageBytes.length & 0x03F);
System.arraycopy(languageBytes, 0, recordPayload, 1, languageBytes.length & 0x03F);
System.arraycopy(textBytes, 0, recordPayload, 1 + (languageBytes.length & 0x03F), textBytes.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, null, recordPayload);
}
NdefRecord r = createTextRecord("en", content);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…