getCachedNdefMessage返回null

时间:2018-04-17 06:40:30

标签: android nfc ndef

我正在尝试从NFC卡读取数据。我使用以下函数来获取和读取NDEF消息,但是我得到了空消息。

我可以使用getMaxSize()获得正确的数据长度,getType()返回org.nfcforum.ndef.type2getCachedNdefMessage()函数返回null。更重要的是,我将android.nfc.action.TAG_DISCOVERED视为ACTION_NDEF_DISCOVERED的意图操作。{/ p>

@Override
protected void onResume() {
    super.onResume();
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    IntentFilter[] nfcIntentFilter = new IntentFilter[]{techDetected,tagDetected,ndefDetected};
    String [][] techListsArray = new String[][]{new String[]{
            Ndef.class.getName()
    }};

    PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    if(mNfcAdapter!= null) {
        mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);
    }
    else {
        Log.d(TAG, "NFC not supported.");
    }
}
@Override
protected void onPause() {
    super.onPause();
    if (mNfcAdapter != null) {
        mNfcAdapter.disableForegroundDispatch(this);
    }
}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d(TAG, "onNewIntent: "+intent.getAction());

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    printTagId(intent);

    if(tag != null) {
        Log.d(TAG, "TAG detected.");

        String [] tagList =  tag.getTechList();
        if(tagList != null)
        {
            for (String str : tagList) {
                Log.d(TAG, "Tech " + str);
            }
        }

        Ndef ndef = Ndef.get(tag);
        readNDef(ndef);
    }
    else {
        Log.d(TAG, "TAG is null.");
    }
}

private void readNDef(Ndef ndef) {

    try {
        ndef.connect();
        Log.d(TAG, "ndef type: " + ndef.getType());
        Log.d(TAG, "ndef maxSize: " + ndef.getMaxSize());
        Log.d(TAG, "ndef isWritable: " + ndef.isWritable());

        NdefMessage ndefMessage = ndef.getCachedNdefMessage();
        if(ndefMessage != null && ndefMessage.getRecords().length > 0) {
            String message = new String(ndefMessage.getRecords()[0].getPayload());
            Log.d(TAG, "readNDef: " + message);
        }
        else{
            Log.d(TAG, "readNDef: NO Records.");
        }
        ndef.close();

    } catch (Exception e) {
        Log.d(TAG, "readNDef exception!");
        e.printStackTrace();
    }
    try{ if(ndef != null) ndef.close(); } catch (Exception ex){}
}

输出如下:

onNewIntent: android.nfc.action.TAG_DISCOVERED
Tech android.nfc.tech.MifareUltralight
Tech android.nfc.tech.NfcA
Tech android.nfc.tech.Ndef
ndef type: org.nfcforum.ndef.type2
ndef maxSize: 1908
ndef isWritable: true
readNDef: NO Records.

当我通过另一个免费应用程序读取NDEF数据时,我确信该卡具有NDEF类型数据。我无法理解为什么我没有ACTION_NDEF_DISCOVERED而不是TAG_DISCOVERED。如何以NDEF格式获取数据?

1 个答案:

答案 0 :(得分:0)

如果标签处于 NFC论坛定义的 INITIALIZED 状态,此方法可能返回 null ,因为此状态下标签为格式化为支持NDEF但尚未包含消息。请参阅文档Here

enter image description here