Android无法编写mifare超轻标记 - 不是Ndef标记吗?

时间:2015-08-07 23:05:31

标签: android nfc mifare

我有一个写有nfc标签的Android应用程序,我可以写一个" NXP MIFARE Ultralight(Utralight C) - NTAG216"没问题。但是,相同的代码似乎不适用于" NXP MIFARE Ultralight(Ultralight)"

技术是一样的,iso号是相同的,但是ndef.java吐出的不是ndef标签。

以下是执行编写的代码位:

public void enableForegroundDispatch() {
    Intent intent = new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    IntentFilter[] intentFilter = new IntentFilter[]{tagDetected,techDetected};
    nfcAdapter.enableForegroundDispatch(activity, pendingIntent, intentFilter, null);
}
...
public boolean writeNdefMessage(Intent intent, NdefMessage ndefMessage) {
    Tag tag = getTagFromIntent(intent);
    return writeNdefMessage(tag, ndefMessage);
}
...
private boolean writeNdefMessage(Tag tag, NdefMessage ndefMessage) {
    boolean result = false;
    try {
        if (tag != null) {
            Ndef ndef = Ndef.get(tag);
            if (ndef == null) {
                result = formatTag(tag, ndefMessage);
            } else {
                ndef.connect();
                if (ndef.isWritable()) {
                    ndef.writeNdefMessage(ndefMessage);
                    result = true;
                }
                ndef.close();
            }
        }
    } catch (Exception e) {
        Log.e("writeNdefMessage", e.toString());
        e.printStackTrace();
    }
    return result;
}
...
private boolean formatTag(Tag tag, NdefMessage ndefMessage) {
    try {
        NdefFormatable ndefFormat = NdefFormatable.get(tag);
        if (ndefFormat != null) {
            ndefFormat.connect();
            ndefFormat.format(ndefMessage);
            ndefFormat.close();
            return true;
        }
    } catch (Exception e) {
        Log.e("formatTag", e.toString());
        e.printStackTrace();
    }
    return false;
}

这是堆栈跟踪:

 08-07 17:44:26.961  13634-13634/redacted E/writeNdefMessage﹕ java.io.IOException
 08-07 17:44:26.961  13634-13634/redacted W/System.err﹕ java.io.IOException
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.nfc.tech.Ndef.writeNdefMessage(Ndef.java:320)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at redacted.helpers.NfcHelper.writeNdefMessage(NfcHelper.java:113)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at redacted.helpers.NfcHelper.writeNdefMessage(NfcHelper.java:100)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at redacted.activities.MainActivity.onNewIntent(MainActivity.java:138)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1211)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1223)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2459)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.performNewIntents(ActivityThread.java:2471)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2480)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.access$1600(ActivityThread.java:151)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1375)
 08-07 17:44:26.962  13634-13634/redacted W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
 08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
 08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5257)
 08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
 08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
 08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
 08-07 17:44:26.963  13634-13634/redacted W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

为了更好的衡量,ndef.java函数:

 public void writeNdefMessage(NdefMessage msg) throws IOException, FormatException {
    checkConnected();

    try {
        INfcTag tagService = mTag.getTagService();
        if (tagService == null) {
            throw new IOException("Mock tags don't support this operation.");
        }
        int serviceHandle = mTag.getServiceHandle();
        if (tagService.isNdef(serviceHandle)) {
            int errorCode = tagService.ndefWrite(serviceHandle, msg);
            switch (errorCode) {
                case ErrorCodes.SUCCESS:
                    break;
                case ErrorCodes.ERROR_IO:
                    throw new IOException();
                case ErrorCodes.ERROR_INVALID_PARAM:
                    throw new FormatException();
                default:
                    // Should not happen
                    throw new IOException();
            }
        }
        else {
            throw new IOException("Tag is not ndef");
        }
    } catch (RemoteException e) {
        Log.e(TAG, "NFC service dead", e);
    }
}

知道这里有什么问题吗? NFC工具和TagWriter在向这些卡写入Ndef消息时没有问题,ndef库是否可能不支持它?我需要使用自己的代码来编写这些标签吗?

0 个答案:

没有答案