RPi上的PN532与Nexus手机点对点连接



我试图在Nexus 3手机和带有adafruit NFC breakboard (PN532芯片)的树莓派之间创建一个简单的消息交换。我已经编译了最新的libfc1.71库并运行了示例:

pi@raspberrypi ~/libnfc1.7/libnfc-1.7.1/examples $ ./nfc-dep-initiator
NFC device: pn532_uart:/dev/ttyAMA0
 openedD.E.P. (212 kbpspassive mode) target:
       NFCID3: 01  fe  ec  d8  f7  03  c5  2d  00  00
           BS: 00
           BR: 00
           TO: 08
           PP: 32
General Bytes: 46  66  6d  01  01  11  03  02  00  13  04  01  96
Sending: Hello World!
nfc_initiator_transceive_bytes: RF Transmission Error

当我将手机从NFC板上移除时抛出错误,否则它什么也不做。

pi@raspberrypi ~/libnfc1.7/libnfc-1.7.1/examples $ ./nfc-dep-target
NFC device: pn532_uart:/dev/ttyAMA0 opened
NFC device will now act as: D.E.P. (undefined baud ratepassive mode) target:
       NFCID3: 12  34  56  78  9a  bc  de  ff  00  00
           BS: 00
           BR: 00
           TO: 00
           PP: 01
General Bytes: 12  34  56  78
Waiting for initiator request...
Initiator request received. Waiting for data...
nfc_target_receive_bytes: Target Released

来自android设备的代码:

MainActivity extends ActionBarActivity   implements NfcAdapter.CreateNdefMessageCallback, NfcAdapter.OnNdefPushCompleteCallback{
    private NfcAdapter mNfcAdapter = null;
    @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;
        }

        mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback()
        {
            @Override
            public NdefMessage createNdefMessage(NfcEvent event)
            {
                String text = ("Beam me up, Android!nn" +
                        "Beam Time: " + System.currentTimeMillis());
                NdefMessage msg = new NdefMessage(
                        new NdefRecord[] { createMime(
                                "application/vnd.com.example.android.beam", text.getBytes())
                        });
                return msg;
            }
        }, this, this);

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

@Override
public void onResume() {
    super.onResume();
    // Check to see that the Activity started due to an Android Beam
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processIntent(getIntent());
    }
}
@Override
public void onNewIntent(Intent intent) {
    // onResume gets called after this to handle the intent
    setIntent(intent);
}
/**
 * Parses the NDEF Message from the intent and prints to the TextView
 */
void processIntent(Intent intent) {
    EditText textView = (EditText) findViewById(R.id.editText);
    Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
            NfcAdapter.EXTRA_NDEF_MESSAGES);
    // only one message sent during the beam
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    // record 0 contains the MIME type, record 1 is the AAR, if present
    textView.setText(new String(msg.getRecords()[0].getPayload()));
}
public void CloseApp(View view) {
    this.finish();
}
@Override
public void onNdefPushComplete(NfcEvent nfcEvent) {
    EditText editText = (EditText) findViewById(R.id.editText);
    editText.setText("onNdefPushComplete");
}

processIntent永远不会被调用,onNdefPushCompletecreateNdefMessage也不会被调用

您正在使用的点对点示例是在NFCIP-1 (NFC-DEP)层传输数据。Android设备只支持通过SNEP/NPP交换NDEF消息,因此您需要在NFC-DEP和SNEP之间实现缺失的层,以便与正在运行的Android设备进行P2P通信。这些层是这样的(你可以从NFC论坛的网站上获得所有的规格:

)
+------------------------------------------------+
|        NFC Data Exchange Format (NDEF)         |
+------------------------------------------------+
|     Simple NDEF Exchange Protocol (SNEP)       |
+------------------------------------------------+
|    NFC Logical Link Control Protocol (LLCP)    |
+------------------------------------------------+
|  NFC Data Exchange Protocol (NFC-DEP/NFCIP-1)  |
+------------------------------------------------+

所以你必须添加LLCP, SNEP和NDEF层

最新更新