0

I'm developing a NFC application to turn WiFi on and connect to some specific network when NFC tag is tapped. So far, I've understood that NFC activity should be on foreground to get NFC data from Android system. But, I doubt whether my understanding is right or not, since I've seen the operation of NFC Tasks(https://play.google.com/store/apps/details?id=com.wakdev.nfctasks). Although the application is not activated on receiving NFC data, It is launching the corresponding task and turning WiFi on successfully.

I'd like to know how it is possible. Is there anyone who can explain its operation?

Thanks in advance.

Logcat on tapping NFC Tag made by 'NFC Tools' : I/NfcDispatcher(822): matched AAR to NDEF : E/SoundPool(822): Don`t play soundpool when NOT normal mode : E/NfcService(822): applyAlternativeHandler::Unknown message received : I/ActivityManager(553): START {act=android.nfc.action.NDEF_DISCOVERED typ=w1/33 pkg=com.wakdev.nfctasks cmp=com.wakdev.nfctasks/.NFCActivity (has extras) u=0} from pid 822

Logcat on tapping NFT Tag made by my application : I/NfcDispatcher(822): matched AAR to application launch : E/SoundPool(822): Don`t play soundpool when NOT normal mode : E/NfcService(822): applyAlternativeHandler::Unknown message received : I/ActivityManager(553): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.Farraf.DigitalSignage cmp=com.Farraf.DigitalSignage/.main.DigitalSignageActivity u=0} from pid 822 : I/ActivityManager(553): Start proc com.Farraf.DigitalSignage for activity com.Farraf.DigitalSignage/.main.DigitalSignageActivity: pid=10160 uid=10008 gids={3003, 1015, 1028}

Michael Roland
  • 39,663
  • 10
  • 99
  • 206

1 Answers1

0

Besides receiving NFC tag events trhough the foreground dispatch system (see Foreground Dispatch System), you can register your activities to be launched upon and receive NFC events through the AndroidManifest.xml of your app (see Filtering for NFC Intents). E.g. if your NFC tag contains the URI http://www.example.com/, you could create an intent filter like this:

<activity ...>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="http"
              android:host="www.example.com" />
    </intent-filter>
</activity>
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • The problem is that on tapping NFC tag written by my application, intent.action is MAIN, not NDEF_DISCOVERED. Do you think the intent-filter setting can fix my problem? – Raphael Joh Nov 14 '14 at 13:15
  • @RaphaelJoh That seems to be a different problem than what you describe in your question above: A tag (specifically one with an AAR) will only cause intent action MAIN to fire if you did not properly register for an NDEF intent. See this [answer](http://stackoverflow.com/a/22842449/2425802) and this [answer](http://stackoverflow.com/a/21186762/2425802) for a more detailed explanation. – Michael Roland Nov 14 '14 at 13:53
  • The data to be set to NFC Tag is like .... record 0 : text //// record 1 : external To invoke my application with text data, The below intent filter is added in AndroidManifest.xml Now I can get the WiFi information at the same time with my application started by NFC tag.
    – Raphael Joh Nov 17 '14 at 01:59
  • @RaphaelJoh Correct, if your first record is a Text record (or a text/plain MIME type record), you would need to adapt the intent filter to match for the text/plain MIME type (instead of the URI that I showed above). Note that the data structure you showed in your comment does not look like human-readable text and would therefore better fit a custom record type, e.g. an NFC Forum external type. – Michael Roland Nov 22 '14 at 07:55