17

Trying to get NFC to work on Android in Embarcadero XE5. Started with the following: https://forums.embarcadero.com/thread.jspa?threadID=97574 which seem to be working. Now would like to register callback for the NFC Intent

Java approach:

1. Register current activity as a listener
...
2. Receive Intent
@Override
protected void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        NdefMessage[] msgs = NfcUtils.getNdefMessages(intent);
    }
}

Source: http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/

Delphi approach (as I would imagine):

1. Define methods available in Java interface

Source: https://forums.embarcadero.com/thread.jspa?messageID=634212

Question:
How do I register a listener for NFC intent messages and 
how do I eventually get messages?

My guess would be to call enableForegroundDispatch method. Define it like:

procedure enableForegroundDispatch; cddcl;

to call it from Android API

But since I have never done this before, I do not know how to proceed

0

1 Answer 1

0

Edit : Seems I have missed a tag and the OP does not ask for Java code. Leaving this anyway for future reference

Your guess is correct, although it might be possible to define the intents you want to listen for in the AndroidManifest.xml, foreground dispatch really puts your app in the front, giving you the ability to capture all launched NFC intents.

The way it is described in the docs gives you a clue.

I assume you're familiar with the lifecycle of the Android Activities, Intent Dispatching and such.


Structure

Using the following structure, you'd have 4 fields :

private PendingIntent pendingIntent;
private IntentFilter[] mIntentFilters;
private String[][] mTechLists;
private NfcAdapter mNfcAdapter;

In the onCreate you'd get :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    mIntentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)};
    mTechLists = new String[][]{new String[]{Ndef.class.getName()},
                new String[]{NdefFormatable.class.getName()}};
}

This does not actually enable the Foreground Dispatch yet, it is just the preparation. The application would receive the Ndef and NdefFormatable technologies Why do we subscribe for ACTION_NDEF_DISCOVERED ?

The order in which Android tries to handle the intent is as such :

  1. ACTION_NDEF_DISCOVERED
  2. ACTION_TECH_DISCOVERED
  3. ACTION_TAG_DISCOVERED

So we make sure our app is the first to be looked at by Android.


Enable FGD

Put the following line of code in the onResume method :

if (mNfcAdapter != null) {
        mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists);
    }

Why is this in the onResume? As the docs state : enableForegroundDispatch() must be called from the main thread and only when the activity is in the foreground (calling in onResume() guarantees this)

This should enable your app to receive the intent, of course, when actually running. If you want to receive intents while not running, you'd have to go to the AndroidManifest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.