NFC - Foreground Dispatch System

raphaelcno

Active Member
Licensed User
Longtime User
I have seen in the NFC NDEF tutorial that it is possible to use the Intent Dispatch System by adding a few lines in the Manifest Editor, for instance:
B4X:
AddActivityText(main, <intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>)
Then when I scan a NFC tag with the "text/plain" mime, my application is shown in a list of applications able to handle that kind of NFC tags.

For my application I would like to use the Foreground Dispatch System. When the application is open and I scan a NCF tag, I want the application to handle the message immediately without getting a list of applications.
I have seen some documentation on these pages (look at "Foreground Dispatch System"):
- Advanced NFC | Android Developers
- TechSoftComputing

The purpose of the Foreground Dispatch System is to give priority to the active application, otherwise the Android device shows a list of applications that are able to read NFC tags ("The Foreground Dispatch System allows an activity to intercept an intent and claim priority over other activities that handle the same intent.")

As I understand I have to implement that kind of code:

B4X:
onCreate():
  // Create a PendingIntent object so the Android system can populate it with the details of the tag when it is scanned.
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
  // Declare intent filters to handle the intents that the developer wants to intercept.
  IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
  ndef.addDataType("text/plain");
  intentFiltersArray = new IntentFilter[] {ndef};

public void onResume() {
  super.onResume();
  // Enable the foreground dispatch when the Activity regains focus.
  NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);
}

public void onPause() {
  super.onPause();
  // Disable the foreground dispatch when the Activity loses focus.
  NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}

How can I implement this code in Basic4android? Is it necessary to update a library with new objects/properties?

Here is some documentation I have found that may be useful to update libraries:

PendingIntent

PendingIntent.getActivity
  • Retrieve a PendingIntent that will start a new activity. Returns an existing or new PendingIntent matching the given parameters.
  • public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)

IntentFilter
  • IntentFilter | Android Developers
  • Structured description of Intent values to be matched.
  • java.lang.Object
  • android.content.IntentFilter
  • Public Constructor
  • public IntentFilter(String action)

IntentFilter.addDataType
  • Add a new Intent data type to match against.
  • Public Method
  • public final void addDataType(String type)

NfcAdapter

NfcAdapter.ACTION_NDEF_DISCOVERED
  • Intent to start an activity when a tag with NDEF payload is discovered. Constant Value: "android.nfc.action.NDEF_DISCOVERED"
  • Constant
  • public static final String ACTION_NDEF_DISCOVERED

NfcAdapter.getDefaultAdapter
  • Helper to get the default NFC Adapter.
  • Public Method
  • public static NfcAdapter getDefaultAdapter(Context context)

NfcAdapter.enableForegroundDispatch
  • Enable foreground dispatch to the given Activity.
  • Public Method
  • public void enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)

NfcAdapter.disableForegroundDispatch
  • Disable foreground dispatch to the given activity.
  • Public Method
  • public void disableForegroundDispatch(Activity activity)

NfcAdapter.isEnabled()
  • Return true if this NFC Adapter has any features enabled.
  • Public Method
  • public boolean isEnabled()

Or is there another possibility to get this functionality with existing libraries?

Thank you for your help :)
 
G

GCOINC

Guest
This library was built by a real life hero! Thanks - I would love to stay in the mix on all new NFC libs or mini collaborative efforts on B4A. Its a great technology and should be taken advantage of ---

o_O
 
Upvote 0
Top