通信Nexus 4 PN532 NFC

时间:2014-02-18 18:51:23

标签: java android nfc

我目前正在筹划一个需要通过NFC从Nexus 4智能手机到PN532 NFC芯片进行通信的项目。之前我曾使用InListPassiveTarget设法读取Mifare Classic 1k卡的内容。我采取的下一步是阅读编程的RFID标签,我能读到的唯一内容是UID,ATQA和SAK。我无法读取我在标签上编程的数据,这些数据必须存储在更深的地方。

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

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


首先,至关重要的是,数据从Nexus 4发送到PN532。我使用的Android应用程序是AndroidBeamDemo的稍微修改版本(代码是abobe)。我还设法阅读此应用程序提供的NDEF消息,其中一个NFC阅读器应用程序安装在从Google Play商店下载的另一部手机上。 我从这个tutorial重新创建了场景。这里描述(4a),在初始化之后,正在使用InJumpForDEP命令。这就是我被困住的地方。 我用普通模式初始化了PN532(SAM Configuarion,0xD4 0x14 0x01)。以前的测试显示,例如,读取Mifare卡的UID(使用InListPassiveTarget命令)没有问题。从RFID标签中读出UID也是可能的。 在执行来自turial(4a)的命令时,应用程序显示“Tap-to-Beam”屏幕并调用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());
    }
}

2 个答案:

答案 0 :(得分:0)

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

第二个答案:是的,这绝对是可能的。我建议使用附有NFC屏蔽的板。然后,您可以使用NFC进行P2P通信,或者您可以尝试在无源卡仿真模式下配置PN532,以便Nexus 看到正常的RFID标签。 NFC屏蔽提供的代码很适合第一次拍摄。

在评估问题之前,有一些事情要提前知道:

  • InJumpForDEP命令是您用于数据交换的唯一命令(当然是在配置之后)?
  • 如何配置PN532(作为目标或发起人)?
  • 您是否实施了SNEP protocolNFC LinkLayer protocol LLCP
  • 您是否使用NDEF格式的代码测试了您的Android代码?因为这就是:NDEF消息的交换。不幸的是,目前提供的Android框架没有直接的P2P NFC数据传输,或者我可能只是不知道它
  • 您是否实施了推拉或推/拉行为?
  • 您是否与其他NFC应用交叉检查?

通常,更容易实现从PN532到Nexus的推送(例如,URL)。如果PN532配置正确,Android梁应该跳转到前台。因此,在您继续使用自己的应用之前,您可以先检查PN532是否正常工作。

答案 1 :(得分:0)

好的,这是我收到的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会不时变化。