我正在尝试使用adafruit NFC分板(PN532芯片)在Nexus 3手机和覆盆子Pi之间创建简单的消息交换。 我编译了最新的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
永远不会被调用,onNdefPushComplete
或createNdefMessage
答案 0 :(得分:2)
您正在使用的对等示例是在NFCIP-1(NFC-DEP)层传输数据。 Android设备仅支持通过SNEP / NPP交换NDEF消息,因此您需要在NFC-DEP和SNEP之间实现缺少的层,以便与运行的Android设备进行P2P通信。图层是这样的(您可以从NFC Forum's website获得所有规格:
+------------------------------------------------+
| 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层。