I am trying to create a simple message exchange between a Nexus 3 phone and a raspberry Pi with an adafruit NFC breakboard (PN532 chip). I have compiled the latest libnfc1.71 library and ran the examples:
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
The error is thrown when I remove the phone from the NFC board, otherwise it just does nothing.
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
The code from the android device:
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
never gets called, neither onNdefPushComplete
or createNdefMessage
The peer-to-peer examples that you are using are transmitting data at the NFCIP-1 (NFC-DEP) layer. An Android device supports only exchange of NDEF messages through SNEP/NPP, so you would need to implement the missing layers between NFC-DEP and SNEP in order to get P2P communication with an Android device running. The layers are like this (you can get all the specifications from the 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) |
+------------------------------------------------+
So you have to add the LLCP, SNEP and NDEF layers.