2

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?

Danii-Sh
  • 447
  • 1
  • 4
  • 18
user2690527
  • 1,729
  • 1
  • 22
  • 38

1 Answers1

0

There simply is no request message. Android Beam (which is the only way to use NFC peer-to-peer mode on Android) is based on SNEP (over LLCP). The method createNdefMessage() is called to build the NDEF message served by the SNEP client on your device. Upon bringing two devices together (and after tapping the Beam UI), Android tries to locate the SNEP server on the other device and pushes the NDEF message to its inbox.

So the only "request" that plays a role in Android Beam is the PUT request that's used by your device to send the message. The process is started by NFC discovery and there is no specific request received from the other side.

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • OK. If I understand correctly, the message being returned by `createNdefMessage()` is dispatched as soon as the smartphone discovers the other device. So it is much of a "fire first, then ask" approach. But how am I supposed to build a multi-round communication that is "reactive", i.e. where the next message depends on past messages? More formally, let a1, a2, ... be the messages from Alice, b1, b2, ... be messages from Bob, let Alice start and let f be the next-message function. Then b1 = f(a1), a2 = f(a1,b1), b2 = f(a1,b2,a2) and so on. How would I realize something like that with NFC? – user2690527 Dec 05 '17 at 12:04
  • @user2690527 You are **not**. At least not with peer-to-peer mode on Android. There is ways to overcome this though: (1) If you really want to use P2P mode, you could design the other end in a way that it disconnects (switch off RF field) ad reconnects to the Android device. Usability is rather limited though. (2) You could let the Android device act as contactless card (HCE) and use reader mode on the other end. (3) You could let Android act as reader and the other end act as card (if the NFC device that you connect to your Linux system supports this). – Michael Roland Dec 05 '17 at 14:33