【问题标题】:Convert java.lang.String to android.nfc.Tag将 java.lang.String 转换为 android.nfc.Tag
【发布时间】: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


【解决方案1】:

Intent 是 Android 的主要进程间通信方式。 Android NFC 堆栈(NFC 系统服务进程)通过发送 NFC 发现意图(NDEF_DISCOVEREDTECH_DISCOVEREDTAG_DSICOVERED)或通过阅读器模式回调(活动生命周期回调)。

因此,如果不处理 NFC 发现意图(例如,通过 onNewIntent 接收)或 ReaderCallback (onTagDiscovered),就无法获得 Tag 对象。

请注意,NFC 发现事件基本上是由用户触发的(即用户点击标签来触发此类事件)。如果您想在按下按钮时处理标签,您有第二个用户触发的事件,您不应该期望彼此同步发生。此外,您不应该期望用户长时间触摸标签(不仅因为这对用户来说难以处理,而且手机和标签通过无线接口进行通信,通信可能会中断)。

最好的方法是,在用户单击按钮后,切换到您接受 NFC 发现事件的状态(例如设置标志)。然后,在完成标签 IO 后,您可以清除该标志以切换回您不再接受新标签的状态。

【讨论】:

    猜你喜欢
    • 2012-09-30
    • 2020-11-14
    • 2019-02-05
    • 1970-01-01
    • 2021-08-22
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    相关资源
    最近更新 更多