Reputation: 1986
i know there are many codes for reading or writing on NFC tags using android devices , but is this doable : if there is WiFi connection open this link , otherwise open another link? without building a custom reader , and use the Built in reader ?
Or do i need to use this device http://www.amazon.com/uFR-RFID-Reader-Writer-Programmer/dp/9896740054/ref=sr_1_8?ie=UTF8&qid=1395842599&sr=8-8&keywords=ACR122U, to apply this ?
Upvotes: 1
Views: 923
Reputation: 40849
If you scan an NFC tag that contains a URL, Android's default behavior (unless an app registered for that URL) is to open the URL in the web browser. Moreover, Android's automatic dispatching system will only read the first NDEF record from a tag (except if there is an AAR available). Therefore, Android would -- by default -- only handle the first URL on the tag.
The only option you have - if you want to implement your desired behavior - is to create your own app (which is good, btw., otherwise your question would likely be off-topic on stackoverflow).
So what could be the design of your app?
You would create an NFC tag that contains two NDEF records of the well-known type URI containing your two URLs. The first one of them should point to the prefered URL in case you app is not installed on a user's device (in that case the first URL would automatically be launched in the web browser). Also note, that the first URL should belong to you (i.e. it should use a domain name that belongs to you). Otherwise, your app could interfere with other apps that use that same URL. E.g.
+-------------------------------------------------+
| WKT:URI | http://www.mroland.at/example/noWiFi |
+-------------------------------------------------+
| WKT:URI | http://www.mroland.at/example/hasWiFi |
+-------------------------------------------------+
You would register your app to be automatically started if the URL from the first record (in our case http://www.mroland.at/example/noWiFi
) is detected. You can do this with an intent filter like this in your manifest:
<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.mroland.at"
android:pathPrefix="/example/noWiFi" />
</intent-filter>
</activity>
One your app is started by the NDEF_DISCOVERED
intent, you could detect the current network state (i.e. if WiFi is available). If WiFi is available, you would read the second NDEF record from the NFC tag and dispatch an intent to start that URL in a web browser. Otherwise, you would take the URL from the first record and dispatch an intent to start this URL in a web browser.
Upvotes: 1