Android NFC passes one parameter at application launch

I would like to run the application using the NFC tag. I got this part using the Android Application Record (AAR) as described in Launch Android Application from NFC tag with additional data or using NDEF_DISCOVERED/ TECH_DISCOVEREDtarget filters. But how to transfer data from an NFC tag (for example, some text) to its activity after it is launched through an NFC event?

I read the NFC Fundamentals , but as far as I understand, it seems like he wants to implement a mechanism for reading a tag when I really don't want to reread the tag when the application opens with the tag, but instead I just want the data to be transferred to the same thing time.

In addition, these mechanisms seem to allow the application to read the tag after it has been launched by the tag. In other words, I am worried that if someone hits the tag later when the application is already open, that tag will be read again (this is what I am not doing ).

Secondly, how do I create such an NDEF message?

+4
source share
1 answer

Android will automatically read the NDEF message of the NFC tag and process it to

  • start logged actions based on the first NDEF record and
  • run applications based on Android Application Records (AAR) anywhere in the NDEF message.

Android NDEF, NDEF_DISCOVERED:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="vnd.android.nfc"
        android:host="ext"
        android:pathPrefix="/example.com:mycustomtype"/>
</intent-filter>

NDEF:

public void onResume() {
    super.onResume();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        NdefMessage[] msgs = null;
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; ++i) {
                msgs[i] = (NdefMessage)rawMsgs[i];
            }
        }

        if ((msgs != null) && (msgs.length > 0)) {
            NdefRecord[] records = msgs[0].getRecords();
            NdefRecord firstRecord = records[0];
            byte[] payloadData = firstRecord.getPayload();

            // do something with the payload (data passed through your NDEF record)
            // or process remaining NDEF message

        }
    }
}

, onResume() , . , . , , .

NFC, , , Android-, NFC . , ( NFC, .

public void onResume() {
    super.onResume();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}

public void onPause() {
    super.onPause();
    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.disableForegroundDispatch(this);
}

public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        // drop NFC events
    }
}

, NDEF- NFC, - :

byte[] payload = ...  // generate your data payload
NdefMessage msg = new NdefMessage(
    NdefRecord.createExternal("example.com", "mycustomtype", payload)
)

, ( , Play Store ), AAR:

NdefMessage msg = new NdefMessage(
    NdefRecord.createExternal("example.com", "mycustomtype", payload),
    NdefRecord.createApplicationRecord("com.example.your.app.package")
)
+6

All Articles