Eventually, I need to implement a cryptographic protocol between an Android device and a Linux host that has a NFC adapter using libfc.
I have never used NFC before with Android. At the moment my idea is to use NFC in Peer-to-Peer mode in the passive variant, e.g. the smartcard reader at the linux host plays the role of the initiator and provides the HF field while the smartphone is the target.
As far as I understand the callback createNdefMessage
can be used to react to an NFC request and send back a reply message. However, I do not understand how I obtain the request message in my Android program.
At the moment -- as a toy example -- I try to achieve the following: The linux host sends a random number via NFC, the android device draws a random number and replies with the sum.
I have
package edu.kit.iti.crypto.nfctest1;
import android.app.Activity;
import android.content.Intent;
import android.nfc.*;
import android.os.Bundle;
import android.provider.Settings;
import java.nio.ByteBuffer;
public class MainActivity extends Activity implements NfcAdapter.CreateNdefMessageCallback {
protected NfcAdapter nfcAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
nfcAdapter = NfcAdapter.getDefaultAdapter( getApplicationContext() );
nfcAdapter.setNdefPushMessageCallback( this, this );
}
protected void onResume() {
super.onResume();
if( !nfcAdapter.isEnabled() ) {
startActivity( new Intent( Settings.ACTION_NFC_SETTINGS) );
} else if( !nfcAdapter.isNdefPushEnabled() ) {
startActivity( new Intent( Settings.ACTION_NFCSHARING_SETTINGS) );
}
}
public NdefMessage createNdefMessage( NfcEvent event ) {
int randomNumber = 42;
// How to obtain the request message here?
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt( randomNumber );
NdefRecord replyRecord = NdefRecord.createExternal( "edu.kit.iti.crypto.nfctest1", "app-randomness", buffer.array() );
return new NdefMessage( replyRecord );
}
}
My very basic question is the comment in the method at the bottom: How do I get the request message?