Two actions on one NFC tag

后端 未结 2 962
暗喜
暗喜 2020-12-20 08:16

I want to know if it is possible to do the following;

I want to have a nfc tag that works in conjunction with an Android application that I am developing.

If

相关标签:
2条回答
  • 2020-12-20 08:53

    If avoiding Play Store is a requirement, you can store an NDEF message containing two records on the tag: The first record contains the URL to your app's download page. The second record contains the plain text data. (In fact, you could even encode the data into the URL and, therefore use only one URI record.)

    +----------------------------------------------+------------------+
    | URI Record: http://www.zapnologica.com/myapp | Text Record: nnn |
    +----------------------------------------------+------------------+
    

    If your application is not installed, the app's download page (here: http://www.zapnologica.com/myapp) will be opened in the user's web browser.

    In order to automatically launch your app upon tag scanning, your app needs to define an intent filter for the download URL:

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="http"
              android:host="www.zapnologica.com"
              android:pathPrefix="/myapp" />
    </intent-filter>
    

    Then, within your app, you can read the whole NDEF message from the tag (actually the NDEF message will be provided to your app in the intent extra EXTRA_NDEF_MESSAGES automatically):

    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        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];
            }
        }
    }
    

    Thus, msg[0].getRecords() will return two records -- the URI record and the Text record.

    0 讨论(0)
  • 2020-12-20 08:59

    I think that this is what you are looking for:

    Tutorial NFC.

    If a user without our app touches the tag, it’ll use a built in mechanism to take them to the download page for the app in the Android Play Store. Once installed any subsequent taps will launch the app and show the game card on screen.

    0 讨论(0)
提交回复
热议问题