本文整理匯總了Java中android.nfc.NdefRecord.TNF_WELL_KNOWN屬性的典型用法代碼示例。如果您正苦於以下問題:Java NdefRecord.TNF_WELL_KNOWN屬性的具體用法?Java NdefRecord.TNF_WELL_KNOWN怎麽用?Java NdefRecord.TNF_WELL_KNOWN使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類android.nfc.NdefRecord
的用法示例。
在下文中一共展示了NdefRecord.TNF_WELL_KNOWN屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createTextRecord
/**
* 創建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;
}
示例2: createTextRecord
private NdefRecord createTextRecord (String message)
{
try
{
byte[] language;
language = Locale.getDefault().getLanguage().getBytes("UTF-8");
final byte[] text = message.getBytes("UTF-8");
final int languageSize = language.length;
final int textLength = text.length;
final ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + languageSize + textLength);
payload.write((byte) (languageSize & 0x1F));
payload.write(language, 0, languageSize);
payload.write(text, 0, textLength);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload.toByteArray());
}
catch (UnsupportedEncodingException e)
{
Log.e("createTextRecord", e.getMessage());
}
return null;
}
示例3: createUriRecord
/**
* 將Uri轉成NdefRecord
*
* @param uriStr
* @return
*/
public static NdefRecord createUriRecord(String uriStr) {
byte prefix = 0;
for (Byte b : UriPrefix.URI_PREFIX_MAP.keySet()) {
String prefixStr = UriPrefix.URI_PREFIX_MAP.get(b).toLowerCase();
if ("".equals(prefixStr))
continue;
if (uriStr.toLowerCase().startsWith(prefixStr)) {
prefix = b;
uriStr = uriStr.substring(prefixStr.length());
break;
}
}
byte[] data = new byte[1 + uriStr.length()];
data[0] = prefix;
System.arraycopy(uriStr.getBytes(), 0, data, 1, uriStr.length());
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], data);
return record;
}
示例4: parseTextRecord
private String parseTextRecord(NdefRecord record){
String response=null;
// 驗證TNF是否為TNF_WELL_KNOWN
if(record.getTnf()!=NdefRecord.TNF_WELL_KNOWN) {
System.out.println("不是TNF_WELL_KNOW");
return null;
}
byte[] payload=record.getPayload();
Byte statusByte=payload[0];
String textCoding=((statusByte&0x80)==0)?"utf-8":"utf-16";
int codeLength=statusByte&0x3f;
try{
response=new String(payload,codeLength+1,payload.length-codeLength-1,textCoding);
}
catch(UnsupportedEncodingException e){
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
return response;
}
示例5: write
/**
* 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(Charset.forName("UTF-8"));
final byte[] textBytes = textToWrite.getBytes(Charset.forName("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);
}
示例6: createTextRecord
private NdefRecord createTextRecord(String content) {
try {
byte[] language;
language = Locale.getDefault().getLanguage().getBytes("UTF-8");
final byte[] text = content.getBytes("UTF-8");
final int languageSize = language.length;
final int textLength = text.length;
final ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + languageSize + textLength);
payload.write((byte) (languageSize & 0x1F));
payload.write(language, 0, languageSize);
payload.write(text, 0, textLength);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload.toByteArray());
} catch (UnsupportedEncodingException e) {
Log.e("createTextRecord", e.getMessage());
}
return null;
}
示例7: parseNdefRecord
private static String parseNdefRecord(Context ctx, NdefRecord record) {
if(record.getTnf() == NdefRecord.TNF_WELL_KNOWN) {
if(Arrays.equals(record.getType(),NdefRecord.RTD_URI)) {
return parseWellKnownUriRecord(ctx,record);
}
else if (Arrays.equals(record.getType(),NdefRecord.RTD_TEXT)) {
return parseWellKnownTextRecord(ctx, record);
}
else {
return parseGenericNdefRecord(ctx,record);
}
}
else {
return parseGenericNdefRecord(ctx,record);
}
}
示例8: parseTnf
private static String parseTnf(Context ctx, NdefRecord record) {
short tnf = record.getTnf();
switch(tnf) {
case NdefRecord.TNF_ABSOLUTE_URI:
return ctx.getString(R.string.tnf_abs_uri);
case NdefRecord.TNF_EMPTY:
return ctx.getString(R.string.tnf_empty);
case NdefRecord.TNF_EXTERNAL_TYPE:
return ctx.getString(R.string.tnf_external);
case NdefRecord.TNF_MIME_MEDIA:
return ctx.getString(R.string.tnf_mime_media);
case NdefRecord.TNF_UNCHANGED:
return ctx.getString(R.string.tnf_unchanged);
case NdefRecord.TNF_WELL_KNOWN:
return ctx.getString(R.string.tnf_well_known);
default:
return ctx.getString(R.string.tnf_unknown);
}
}
示例9: nfcRead
public String nfcRead(Tag t)
{
Tag tag = t;
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records)
{
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
{
try {return readText(ndefRecord);} catch (UnsupportedEncodingException e) {}
}
}
return null;
}
示例10: getNdefMessageFromText
public NdefMessage getNdefMessageFromText(String messageText)
{
try
{
// Get UTF-8 byte
byte[] lang = Locale.getDefault().getLanguage().getBytes("UTF-8");
byte[] text = messageText.getBytes("UTF-8"); // Content in UTF-8
int langSize = lang.length;
int textLength = text.length;
ByteArrayOutputStream payload = new ByteArrayOutputStream(1 + langSize + textLength);
payload.write((byte) (langSize & 0x1F));
payload.write(lang, 0, langSize);
payload.write(text, 0, textLength);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], payload.toByteArray());
return new NdefMessage(new NdefRecord[]{record});
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例11: getTagText
public String getTagText() throws Exception
{
if (tag == null)
{
throw new Exception("Please call handle new read as tag is null");
}
Ndef ndef = Ndef.get(tag);
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records)
{
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT))
{
try
{
return readText(ndefRecord);
}
catch (UnsupportedEncodingException e)
{
Toast.makeText(activity, "Unsupported encoding", Toast.LENGTH_SHORT).show();
}
}
}
return null;
}
示例12: doInBackground
@Override
protected ArrayList<String> doInBackground(Tag... params) {
Tag tag = params[0];
Ndef ndef = Ndef.get(tag);
if (ndef == null) {
// NDEF is not supported by this Tag.
return null;
}
ArrayList<String> results = new ArrayList<>();
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
NdefRecord[] records = ndefMessage.getRecords();
for (NdefRecord ndefRecord : records) {
if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
try {
results.add(readText(ndefRecord));
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported Encoding", e);
}
}
}
return results;
}
示例13: toNfcRecord
/**
* Converts android.nfc.NdefRecord to mojo NfcRecord
*/
private static NfcRecord toNfcRecord(NdefRecord ndefRecord)
throws UnsupportedEncodingException {
switch (ndefRecord.getTnf()) {
case NdefRecord.TNF_EMPTY:
return createEmptyRecord();
case NdefRecord.TNF_MIME_MEDIA:
return createMIMERecord(
new String(ndefRecord.getType(), "UTF-8"), ndefRecord.getPayload());
case NdefRecord.TNF_ABSOLUTE_URI:
return createURLRecord(ndefRecord.toUri());
case NdefRecord.TNF_WELL_KNOWN:
return createWellKnownRecord(ndefRecord);
}
return null;
}
示例14: readTag
private void readTag(Tag t) {
byte[] id = t.getId();
// get NDEF tag details
Ndef ndefTag = Ndef.get(t);
// get NDEF message details
NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
if (ndefMesg == null) {
return;
}
NdefRecord[] ndefRecords = ndefMesg.getRecords();
if (ndefRecords == null) {
return;
}
for (NdefRecord record : ndefRecords) {
short tnf = record.getTnf();
String type = new String(record.getType());
if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
String url = new String(record.getPayload());
recordBadge(url);
}
}
}
示例15: buildNdefRecord
/**
* 構建NdefRecord
*
* @param text
* @param locale
* @param encodeInUtf8
* @return
*/
private NdefRecord buildNdefRecord(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);
}