Page 378 - 완) I MDP 프로젝트 작품 보고서(전체과 1학년)1.6
P. 378
*
* @param msg
* @param type
* @return
*/
private NdefMessage createTagMessage(String msg, int type) {
NdefRecord[] records = new NdefRecord[1];
if (type == TYPE_TEXT) {
records[0] = createTextRecord(msg, Locale.KOREAN, true);
} else if (type == TYPE_URI) {
records[0] = createUriRecord(msg.getBytes());
}
NdefMessage mMessage = new NdefMessage(records);
return mMessage;
}
private NdefRecord createTextRecord(String text, Locale locale,
boolean encodeInUtf8) {
final byte[] langBytes = locale.getLanguage().getBytes(
Charsets.US_ASCII);
final Charset utfEncoding = encodeInUtf8 ? Charsets.UTF_8 : Charset
.forName("UTF-16");
final byte[] textBytes = text.getBytes(utfEncoding);
final int utfBit = encodeInUtf8 ? 0 : (1 << 7);
final char status = (char) (utfBit + langBytes.length);
final byte[] data = Bytes.concat(new byte[] { (byte) status },
langBytes, textBytes);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT,
new byte[0], data);
}
private NdefRecord createUriRecord(byte[] data) {
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI,
new byte[0], data);
}
}
- 371 -