【问题标题】:Writing to a Mifare tag with Android NFC?使用 Android NFC 写入 Mifare 标签?
【发布时间】:2013-02-27 16:46:16
【问题描述】:

我正在尝试将一个简单的“Hello World”字符串写入 Mifare 1K 标签上扇区 0 的第一个块。该标签是全新的,具有默认配置。扇区尾部(块 3)在读取时为:00 00 00 00 00 00 00 00 ff 07 80 69 ff ff ff ff ff ff ff ff。因此,访问条件是ff 07 80 69,这意味着我可以使用密钥A对每个块进行读写。

不过,我还是无法在标签上写任何东西。这是我的代码:

try {
    mfc.connect();
    boolean auth = false;

    auth = mfc.authenticateSectorWithKeyA(0,MifareClassic.KEY_DEFAULT);

    if (auth) {

        String text = "Hello, World!";
        byte[] value  = text.getBytes();
        byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];        

        for (int i=0; i<MifareClassic.BLOCK_SIZE; i++) {
              if (i < value.length) toWrite[i] = value[i];
              else toWrite[i] = 0;
        }           

        mfc.writeBlock(0, toWrite);
   }    

我收到以下异常:Transceived failed

我做错了什么?

这是堆栈跟踪:

07-09 00:19:44.836: W/System.err(13167):    at android.nfc.TransceiveResult.getResponseOrThrow(TransceiveResult.java:52)
07-09 00:19:44.843: W/System.err(13167):    at android.nfc.tech.BasicTagTechnology.transceive(BasicTagTechnology.java:151)
07-09 00:19:44.843: W/System.err(13167):    at android.nfc.tech.MifareClassic.writeBlock(MifareClassic.java:453)
07-09 00:19:44.843: W/System.err(13167):    at com.example.andorid.apis.mifare.MainActivity.resolveIntent(MainActivity.java:128)
07-09 00:19:44.843: W/System.err(13167):    at com.example.andorid.apis.mifare.MainActivity.onNewIntent(MainActivity.java:275)
07-09 00:19:44.843: W/System.err(13167):    at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1123)
07-09 00:19:44.843: W/System.err(13167):    at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2041)
07-09 00:19:44.843: W/System.err(13167):    at android.app.ActivityThread.performNewIntents(ActivityThread.java:2054)
07-09 00:19:44.843: W/System.err(13167):    at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2063)
07-09 00:19:44.843: W/System.err(13167):    at android.app.ActivityThread.access$1400(ActivityThread.java:122)
07-09 00:19:44.843: W/System.err(13167):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1193)
07-09 00:19:44.851: W/System.err(13167):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 00:19:44.851: W/System.err(13167):    at android.os.Looper.loop(Looper.java:137)
07-09 00:19:44.851: W/System.err(13167):    at android.app.ActivityThread.main(ActivityThread.java:4340)
07-09 00:19:44.851: W/System.err(13167):    at java.lang.reflect.Method.invokeNative(Native Method)
07-09 00:19:44.851: W/System.err(13167):    at java.lang.reflect.Method.invoke(Method.java:511)
07-09 00:19:44.851: W/System.err(13167):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 00:19:44.851: W/System.err(13167):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 00:19:44.851: W/System.err(13167):    at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 你能显示整个堆栈跟踪吗?
  • 我收到同样的错误收发失败。我确实认为这是 Android ICS4.0.3 和 ICS 4.0.4 的错误。我在 S3 i9300 和 Galaxy Note E160K 上测试过,都失败了。我的 MiFare 1k 卡可以通过 NDEF 被其他应用程序读写。

标签: android nfc mifare


【解决方案1】:

您正在尝试写入块 0,这是不可能的。块 0 始终是只读的,即使访问条件表明它是可写的。块 0 包含 UID 和一些其他制造商数据。尝试写入块 1 或 2,看看您是否仍然遇到同样的问题。

【讨论】:

    【解决方案2】:

    我在尝试从不再连接到阅读器的标签中读取数据时看到了这个特殊异常。

    我知道,对于某些读卡器/读卡器组合,很难保持良好的连接。

    例如,我的 Nexus S 无法与某些标签保持连接。 Nexus S 没有最强的领域。我认为 Galaxy Note 在这方面是相似的。

    尝试一些其他品牌的标签以及其他阅读器。

    另外,我会尝试通过 for 循环运行您的读取,以查看它是否保持连接,如果这有意义的话。

    【讨论】:

    • 不...我不认为这是问题所在。我可以使用 NXP 标签写入器轻松写入数据。我有一个galaxy nexus i9250。
    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多