【发布时间】:2015-07-16 09:30:39
【问题描述】:
我有一组当前运行良好的代码,即:
我想创建一个没有 newIntent 和其他东西的新项目,这是我的代码:
经过一些调试,我发现 currentTag 包含的内容是:
据我了解,currentTag 是 nfc 芯片的详细信息。 我正在尝试对我的新项目做同样的事情,但没有任何成功。 不胜感激任何帮助或指示,因为我是 NFC 新手。谢谢!
编辑:这是 NFCTagHelper 类:
public class NfcTagHelper {
private static final int PAGE_FOR_WRITE_CHECK = 4;
private static final int PAGE_FOR_READ_RESULT = 6;
private static final byte FAILURE_RESULT = 0x22;
public static final byte TAG_LOST_RESULT = 0x33;
public static final byte UNKNOW_ERROR_RESULT=0x44;
private static final String TAG = NfcTagHelper.class.getSimpleName();
public boolean writeToTag(Tag tag,byte[] lockID) {
boolean result=false;
MifareUltralight mifareUltralight = MifareUltralight.get(tag);
if (mifareUltralight==null) {
Log.e("NfcTagHelper", "This tag don't support to wirte page");
return false;
}else {
try {
mifareUltralight.connect();
mifareUltralight.writePage(PAGE_FOR_WRITE_CHECK,lockID);
result =true;
}catch (Exception exception) {
result=false;
Log.e("NfcTagHelper", "write Error:", exception);
}finally {
try {
mifareUltralight.close();
}catch (Exception exception) {
result=false;
Log.e("NfcTagHelper", "Exception:", exception);
}
}
}
return result;
}
public byte readFromTag(Tag tag) {
byte result=0x00;
MifareUltralight mifareUltralight = MifareUltralight.get(tag);
if (mifareUltralight==null) {
Log.e("NfcTagHelper", "This tag don't support to wirte page");
}else {
try {
mifareUltralight.connect();
byte[]payload = mifareUltralight.readPages(PAGE_FOR_READ_RESULT);
result= payload[0];
}catch (TagLostException tagLostException) {
result = TAG_LOST_RESULT;
}
catch (Exception exception) {
Log.e("NfcTagHelper", "Read Error:", exception);
result=UNKNOW_ERROR_RESULT;
}finally {
try {
mifareUltralight.close();
}catch (Exception exception) {
Log.e("NfcTagHelper", "Read Exception:", exception);
}
}
}
return result;
}
【问题讨论】:
-
NfcTagHelper是什么? -
@Blackbelt 更新了!谢谢!
标签: java android android-intent tags nfc