0

我正在尝试在 Nexus 3 手机和带有 adafruit NFC 断板(PN532 芯片)的树莓派之间创建一个简单的消息交换。我已经编译了最新的 libnfc1.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!\n\n" +
                        "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

4

1 回答 1

2

您使用的点对点示例是在 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 层。

于 2014-05-06T14:08:50.887 回答