【问题标题】:how to write informations into mifare ultra light EV1 nfc card with android?如何使用android将信息写入mifare ultralight EV1 nfc卡?
【发布时间】:2021-08-31 11:29:13
【问题描述】:

我已成功将信息写入我的 nfc 卡,但现在它不再工作了,这是我的代码:

  public void writeTag(MifareUltralight mifareUlTag) {
    try {
        mifareUlTag.connect();
        String val=getRandomString();

       // mifareUlTag.writePage(4, "rfa".getBytes(Charset.forName("US-ASCII")));
       // mifareUlTag.writePage(5, "nfc".getBytes(Charset.forName("US-ASCII")));
        mifareUlTag.writePage(10, val.getBytes(Charset.forName("US-ASCII")));
        //mifareUlTag.writePage(7, " now".getBytes(Charset.forName("US-ASCII")));
    } catch (IOException e) {
        Log.e(TAG, "IOException while writing MifareUltralight...", e);
    } finally {
        try {
            mifareUlTag.close();
        } catch (IOException e) {
            Log.e(TAG, "IOException while closing MifareUltralight...", e);
        }
    }
}

发生的错误是:android.nfc.TagLostException: Tag was lost getRandomString 方法是一个返回 3 个字符的随机字符串的函数。 我的卡又可以用了,因为我可以读取他的号码。

【问题讨论】:

  • 您使用什么方法来获得有关标签enableForegroundDispatchenableReaderMode 的通知?
  • enableForegroundDispatch

标签: android nfc mifare


【解决方案1】:

我发现,对于真正的用户来说,除了读取 Ndef 数据之外,尤其是尝试写入数据时,很容易得到 TagLostExceptionIOExceptionenableForegroundDispatch

原因很简单,enableForegroundDispatch 是你无法控制通知声音的。

用户过早听到并从射频场移除标签,enableForegroundDispatch 当系统 NFC 应用检测到标签时会产生声音,然后它必须制作Intent,暂停和恢复您的应用为了能够提供Intent,您的应用必须处理该Intent,然后开始写入标签。

我发现使用enableReaderMode 更可靠,您可以关闭系统应用通知声音并在完成写入标签后发出自己的声音。 enableReaderMode 也不会暂停和恢复您的应用程序,而是在新线程中处理标签读取,这通常开销较小且速度更快,因此会降低出现 TagLostException 等错误的可能性

当然,这些错误仍然可以生成,您的代码必须处理这些错误,并且使用enableReaderMode,您可以生成“失败”声音来提示用户表示标签,或者就像我对任何错误保持沉默,只是再次出现标签时重试写入。

enableReaderMode 只是为用户提供了比enableForegroundDispatch 更健壮和更少混乱的交互

更新:

您是否尝试过写入 4 个字符,因为数据发送必须是 4 个字节,您可能没有发送足够的数据。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多