繁体   English   中英

通讯Nexus 4 PN532 NFC

[英]Communication Nexus 4 PN532 NFC

我目前正在计划一个项目,该项目需要Nexus 4智能手机通过NFC与PN532 NFC芯片进行通信。 我以前通过使用InListPassiveTarget读取了Mifare Classic 1k卡的内容。 我的下一步是读取已编程的RFID标签,唯一可以读取的内容是UID,ATQA和SAK。 我无法读取我在标签上编程的数据,该数据必须存储在更深的地方。

如何读取编程的数据,必须使用哪个命令? 我使用InListPassiveTarged命令接收UID,ATQA和SAK。

我的第二个问题是:我可以编写一个将数据传输到PN532的Android应用程序吗? 如果是这样,我如何设法实现这一目标?


首先至关重要的是,数据必须从Nexus 4发送到PN532。 我使用的Android应用是AndroidBeamDemo的略微修改版本(代码为abobe)。 我还设法通过在从Google Play商店下载的另一部手机上安装的NFC阅读器应用程序读取了此应用程序提供的NDEF消息。 我根据本教程重新创建了场景。 这里描述(4a),在初始化之后,将使用InJumpForDEP命令。 这就是我坚持的重点。 我以正常模式(SAM Configuarion,0xD4 0x14 0x01)初始化了PN532。 先前的测试表明,例如,读取Mifare卡的UID没问题(使用InListPassiveTarget命令)。 也可以从RFID标签读取UID。 在执行来自Turial(4a)的命令时,该应用程序显示“轻击光束”屏幕,并调用了createNdefMessage函数,但从未调用过onNdefPushComplete函数,而是LogCat显示了上面概述的输出。 希望有了这些信息,我的情况现在更加清楚了,谢谢您的帮助。


关于第二个问题,我现在几乎设法在PN532中断板和Nexus 4之间建立通信。在PN532初始化之后,我使用了InJumpForDEP命令。 之后,我没有从PN532收到任何响应,并且使用Logcat跟踪了一些日志。 希望您能帮助我了解跟踪的日志,并告诉我如何处理错误。

我在InJumpForDEP命令中发送的数据:

0xD4、0x56、0x01、0x02、0x04、0x46、0x66、0x6D,0x01、0x01、0x10、0x03、0x02、0x00、0x01、0x04、0x01、0x96

LogCat:

02-22 17:19:08.418: D/NfceeAccess(834):                  Granted NFCEE access to com.google.android.apps.walletnfcrel (wildcard)
02-22 17:19:08.648: D/NfcService(834):                   LLCP Activation message
02-22 17:19:08.648: I/NfcP2pLinkManager(834):            LLCP activated
02-22 17:19:08.648: D/NfcP2pLinkManager(834):            onP2pInRange()
02-22 17:19:08.648: D/audio_hw_primary(176):             select_devices: out_snd_device(2: speaker) in_snd_device(0: )
02-22 17:19:08.648: D/ACDB-LOADER(176):                  ACDB -> send_afe_cal
02-22 17:19:08.668: I/NFC(5006):                         Create android.nfc.NfcEvent@42025c60
02-22 17:19:08.668: D/NfcP2pLinkManager(834):            onP2pSendConfirmationRequested()
02-22 17:19:08.728: D/dalvikvm(834):                     GC_FOR_ALLOC freed 4322K, 27% free 12775K/17320K, paused 22ms, total 22ms
02-22 17:19:08.748: I/dalvikvm-heap(834):                Grow heap (frag case) to 15.822MB for 3483664-byte allocation
02-22 17:19:08.758: D/dalvikvm(834):                     GC_CONCURRENT freed 11K, 22% free 16166K/20724K, paused 3ms+2ms, total 17ms
02-22 17:19:08.758: D/dalvikvm(834):                     WAIT_FOR_CONCURRENT_GC blocked 7ms
02-22 17:19:08.768: D/PhoneStatusBar(683):               disable: < EXPAND* icons alerts ticker system_info back home recent clock search >
02-22 17:19:08.798: W/InputMethodManagerService(599):    Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4254ddf8 (uid=10128 pid=5006)
02-22 17:19:09.449: D/NfcP2pLinkManager(834):            onP2pSendConfirmed()
02-22 17:19:09.639: E/BrcmNfcNfa(834):                   nfa_p2p_proc_llcp_disconnect_ind (): Link deactivated
02-22 17:19:09.639: E/BrcmNfcJni(834):                   PeerToPeer::disconnectConnOriented: can't find connection handle: 22
02-22 17:19:09.639: D/NdefPushClient(834):               about to create socket
02-22 17:19:09.639: D/NdefPushClient(834):               about to connect to service com.android.npp
02-22 17:19:09.649: D/NfcService(834):                   LLCP Link Deactivated message. Restart polling loop.
02-22 17:19:09.649: I/NfcP2pLinkManager(834):            LLCP deactivated.
02-22 17:19:09.649: E/NfcP2pLinkManager(834):            onP2pSendDebounce()
02-22 17:19:09.649: E/BrcmNfcNfa(834):                   NFA_P2pConnectByName ():   MIU(128) must be between 128 and 2175 or LLCP link is not activated
02-22 17:19:09.649: E/BrcmNfcJni(834):                   PeerToPeer::createDataLinkConn: fail; error=0x3
02-22 17:19:09.649: E/BrcmNfcJni(834):                   PeerToPeer::disconnectConnOriented: can't find connection handle: 23
02-22 17:19:14.655: D/NfcP2pLinkManager(834):            Debounce timeout
02-22 17:19:14.655: D/NfcP2pLinkManager(834):            onP2pOutOfRange()

Android应用程序的源代码:

package com.example.nfc_demo_ndef;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback
{
    private NfcAdapter mNfcAdapter;

    private static final int MESSAGE_SENT = 1;

    /** This handler receives a message from onNdefPushComplete */
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
            case MESSAGE_SENT:
                Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) 
        {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        // Register callback
        mNfcAdapter.setNdefPushMessageCallback(this, this);
        mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) 
    {
        String text = ("aaaaaaaa");
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { NdefRecord.createMime(
                        "text/plain", text.getBytes())
         /**
          * The Android Application Record (AAR) is commented out. When a device
          * receives a push with an AAR in it, the application specified in the AAR
          * is guaranteed to run. The AAR overrides the tag dispatch system.
          * You can add it back in to guarantee that this
          * activity starts when receiving a beamed message. For now, this code
          * uses the tag dispatch system.
          */
          //,NdefRecord.createApplicationRecord("com.example.android.beam")
        });

        Log.i("NFC", "Create " + event.toString());

        return msg;
    }

    @Override
    public void onNdefPushComplete(NfcEvent event)
    {
        mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();     
        Log.i("NFC", "Complete " + event.toString());
    }
}

第一个答案 :您需要使用InListPassiveTarget来获取MIFARE标记的UID。 接下来,您必须使用InDataExchange命令(使用身份验证过程)访问标签中的安全数据。

第二个回答 :是的,这绝对是可能的。 我建议使用带有NFC防护罩的板。 然后,您可以使用通过NFC进行的P2P通信,也可以尝试在无源卡仿真模式下配置PN532,以使Nexus 看到正常的RFID标签。 NFC防护罩提供的代码适合初次使用。

在评估问题之前,需要提前了解一些事情:

  • InJumpForDEP命令是您用于数据交换的唯一命令(当然是在配置之后)吗?
  • 您如何配置PN532(作为目标或发起者)?
  • 您是否实现了SNEP协议NFC LinkLayer协议LLCP
  • 您是否使用NDEF格式的标签测试了Android代码? 因为这就是关于:NDEF消息的交换。 不幸的是,当前提供的Android框架无法实现直接的P2P NFC数据传输,或者我可能对此一无所知
  • 您是否实现了推或拉或推/拉行为?
  • 您是否与其他NFC应用程序进行了交叉核对?

通常,更容易实现从PN532到Nexus的推送(例如URL)。 如果PN532配置正确,Android光束应跳入前景。 因此,您可以先检查PN532是否正常工作,然后再继续使用自己的应用程序。

好的,这是我收到的InJumpForDEP的响应:

0xD5 0x57 0x00 0x01 0x01 0xFE 0x95 0xCA 0x46 0x61 0xC5 0x38 0x00 0x00 0x00 0x00 0x00 0x07 0x32 0x46 0x66 0x6D 0x01 0x01 0x55 0x00 0x00 0x00 0x00 0xFF 0x03 0xFD 0xD4 0x14 0x01 0x17 0x00 0x00 0xFF 0xEE 0xD4 0x56 0x01 0x02 0x04 0x46 0x66 0x6D 0x01 0x01 0x10 0x03 0x02 0x00 0x01 0x04 0x01 0x96 0x03

如您所见,最后一个字节0x46 0x66 0x6D 0x01 0x01 0x10 0x03 0x02 0x00 0x01 0x04 0x01 0x96是正确的,因为它们是用作参数的常规字节。 但是对于其余部分,仍然存在巨大差异。

部分0x95 0xCA 0x46 0x61 0xC5 0x38不时变化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM