Java Code Examples for android.nfc.NdefRecord#RTD_TEXT
The following examples show how to use
android.nfc.NdefRecord#RTD_TEXT .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WriteTextActivity.java From android-nfc with MIT License | 6 votes |
/** * 创建NDEF文本数据 * * @param text * @return */ public static NdefRecord createTextRecord(String text) { byte[] langBytes = Locale.CHINA.getLanguage().getBytes(Charset.forName("US-ASCII")); Charset utfEncoding = Charset.forName("UTF-8"); //将文本转换为UTF-8格式 byte[] textBytes = text.getBytes(utfEncoding); //设置状态字节编码最高位数为0 int utfBit = 0; //定义状态字节 char status = (char) (utfBit + langBytes.length); byte[] data = new byte[1 + langBytes.length + textBytes.length]; //设置第一个状态字节,先将状态码转换成字节 data[0] = (byte) status; //设置语言编码,使用数组拷贝方法,从0开始拷贝到data中,拷贝到data的1到langBytes.length的位置 System.arraycopy(langBytes, 0, data, 1, langBytes.length); //设置文本字节,使用数组拷贝方法,从0开始拷贝到data中,拷贝到data的1 + langBytes.length //到textBytes.length的位置 System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length); //通过字节传入NdefRecord对象 //NdefRecord.RTD_TEXT:传入类型 读写 NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); return ndefRecord; }
Example 2
Source File: PNfc.java From PHONK with GNU General Public License v3.0 | 6 votes |
/** * Write text to a tag * * @param textToWrite the text to write */ public void write(String textToWrite) { Locale locale = Locale.US; final byte[] langBytes = locale.getLanguage().getBytes(StandardCharsets.UTF_8); final byte[] textBytes = textToWrite.getBytes(StandardCharsets.UTF_8); final int utfBit = 0; final char status = (char) (utfBit + langBytes.length); final 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 record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); NdefRecord[] records = {record}; messageToWrite = new NdefMessage(records); }
Example 3
Source File: ForegroundNdefPush.java From codeexamples-android with Eclipse Public License 1.0 | 6 votes |
public static NdefRecord newTextRecord(String text, Locale locale, boolean encodeInUtf8) { byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII")); Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16"); byte[] textBytes = text.getBytes(utfEncoding); int utfBit = encodeInUtf8 ? 0 : (1 << 7); char status = (char) (utfBit + langBytes.length); 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); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); }
Example 4
Source File: GingerbreadUtil.java From appinventor-extensions with Apache License 2.0 | 5 votes |
public static NdefRecord createTextRecord(String payload, boolean encodeInUtf8) { byte[] langBytes = Locale.getDefault().getLanguage().getBytes(Charset.forName("US-ASCII")); Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16"); byte[] textBytes = payload.getBytes(utfEncoding); int utfBit = encodeInUtf8 ? 0 : (1 << 7); char status = (char) (utfBit + langBytes.length); 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 record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); return record; }
Example 5
Source File: NdefRecordUtil.java From commcare-android with Apache License 2.0 | 5 votes |
public static NdefRecord createTextRecordManually(String payload, Locale locale) { byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII")); byte[] textBytes = payload.getBytes(UTF8_CHARSET); int utfBit = 0; char status = (char)(utfBit + langBytes.length); 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); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data); }
Example 6
Source File: TextRecord.java From effective_android_sample with Apache License 2.0 | 5 votes |
@Override public NdefRecord getNdefRecord() { if(!hasLocale()) { throw new IllegalArgumentException("Expected locale"); } if(!hasEncoding()) { throw new IllegalArgumentException("Expected encoding"); } if(!hasText()) { throw new IllegalArgumentException("Expected text"); } byte[] languageData = (locale.getLanguage() + (locale.getCountry() == null || locale.getCountry().length() == 0 ? "" : ("-" + locale.getCountry()))).getBytes(); if (languageData.length > TextRecord.LANGUAGE_CODE_MASK) { throw new IllegalArgumentException("Expected language code length <= 32 bytes, not " + languageData.length + " bytes"); } byte[] textData = text.getBytes(encoding); byte[] payload = new byte[1 + languageData.length + textData.length]; byte status = (byte)(languageData.length | (TextRecord.UTF16.equals(encoding) ? 0x80 : 0x00)); payload[0] = status; System.arraycopy(languageData, 0, payload, 1, languageData.length); System.arraycopy(textData, 0, payload, 1 + languageData.length, textData.length); return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, id != null ? id : EMPTY, payload); }