Android NFC passing single parameter when starting application - android

I would like to start an application using an NFC tag. I got that part working using an android application record (AAR) as described in Start Android application from NFC-tag with extra data or by using NDEF_DISCOVERED / TECH_DISCOVERED intent filters. But how do I pass data from the NFC tag (e.g. some text) to my activity upon starting it through the NFC event?
I've read through NFC Basics, but as far as I understand that it seems to want to implement a mechanism for reading the tag, when I really do not want to re-read the tag once the app is opened by the tag, but instead I just want the data passed in at the same time.
Moreover, these mechanisms seem to allow the app to read the tag after it has been started by the tag. In other words, I am worried that if someone hits the tag later when the app is already opened that tag will be read again (which is what I do not want).
Second, how do I create such NDEF message?

Android will automatically read the NDEF message of an NFC tag and process it in order to
start registered activities based on the first NDEF record, and
start apps based on Android Application Records (AAR) anywhere in the NDEF message.
In order to get your activity started and have Android pass the pre-read NDEF message, you could use the NDEF_DISCOVERED intent filter:
<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>
Then from within your activity, you could process that NDEF message:
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
}
}
}
Note that onResume() is run whenever your activity becomes the foreground activity. Hence, it might be run multiple times for the same tag. THerefore, you could either use another life-cycle method or take some precautions that you do not parse the message multiple times.
If you want to drop all further NFC events, once your activity is open, you could follow the approach that I described in response to Android app enable NFC only for one Activity. Hence, you would register for the foreground dispatch (which gives your activity priority in receiving NFC events, and you can then simply drop those events.
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
}
}
Finally, to create the NDEF message for your NFC tag, you would do something like this:
byte[] payload = ... // generate your data payload
NdefMessage msg = new NdefMessage(
NdefRecord.createExternal("example.com", "mycustomtype", payload)
)
If you want to make sure that only your app is started by this tag (or if not installed Play Store is opened for your app), you could also add an AAR:
NdefMessage msg = new NdefMessage(
NdefRecord.createExternal("example.com", "mycustomtype", payload),
NdefRecord.createApplicationRecord("com.example.your.app.package")
)

Related

How to proactive read NFC tag - without Intent

I want to scan an NFC tag without using Intent. In other words I want to force the scan. I have already read the:
http://developer.android.com/guide/topics/connectivity/nfc/index.html
https://code.google.com/p/ndef-tools-for-android/
but both use Intents.
P.S.: My case is that the NFC tag is permanently attached to the device, so I cannot use intents.
Use foreground dispatch:
http://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null || !nfcAdapter.isEnabled()) {
Log.e(TAG, "No NFC Adapter found.");
//finish();
}
Intent intent = new Intent(this, getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
// Handles all MIME based dispatches. You should specify only the ones that you need.
ndef.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
throw new RuntimeException("failed to add MIME type", e);
}
//intentFiltersArray = new IntentFilter[]{ndef,};
//Use no intent filters to accept all MIME types
intentFiltersArray = new IntentFilter[]{};
// The tech list array can be set to null to accept all types of tag
techListsArray = new String[][]{new String[]{
IsoDep.class.getName(),
NfcA.class.getName(),
NdefFormatable.class.getName()
}};
}
public void onPause() {
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
public void onResume() {
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);
super.onResume();
}
public void onNewIntent(Intent intent) {
Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//do something with tagFromIntent
}
Easy.
When you hit your "flexibility" time... and you actually change. Use INTENt, read the tag, and save the information on your sdcard, shared preferences, cloud wherever.
And use this information each time you want to read the TAG. Instead read the file which was created las ttime when tag was attached.
Next time when you will remove tag and add another one, your file will be recreated.
Don't read tag, read file created by tag attaching to device.
You cannot do what you want. Reading a tag without an intent is not possible due to the way the Android NFC subsystem is built.
Also it is a very bad idea to glue a tag onto the backside of a phone. NFC will - as long as no tag has been detected - periodically check for the existence of a tag. Once a tag has been detected it will get powered over the air until the tag does not answer anymore.
If the tag is always in range of the phone it will drain battery life like crazy.

Sending URL from Android to Windows Phone via NFC gives Play Store link

I am trying to use NFC to send a URL from an Android app to a WP8 phone.
When beaming to an Android device, the URL is sent correctly. However, when beaming to WP8, IE loads a link to the Play Store instead of the one I want to send (e.g. "http://www.stackoverflow.com").
The Play Store link is: "https://play.google.com/store/apps/details?id=com.example.conductrnfc&feature=beam". Where "com.example.conductrnfc" is the package name in the project.
The code I used to generate the NFC message is given below. Is there something I'm doing wrong here that breaks compatibility with WP8?
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
nfc.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
#Override
public NdefMessage createNdefMessage(NfcEvent event)
{
NdefRecord uriRecord = NdefRecord.createUri(urlString);
return new NdefMessage(new NdefRecord[] { uriRecord });
}
},
this);
Can you try this:
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
nfc.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() {
#Override
public NdefMessage createNdefMessage(NfcEvent event)
{
byte[] payload = urlString.getBytes();
NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);
return new NdefMessage(new NdefRecord[] { uriRecord });
}
},
this);
Eventhough I still miss some more debugging results from the OP, I thought I'd give it a shot:
As the discussion in the commands revealed that the createNdefMessage callback is not called when interacting with a WP8 phone, it would be interesting why this appens and how to prevent this. Unfortunately I have no details about the actual lifecycle of the activity, so I can only guess what might go wrong.
One reason why a registered createNdefMessage callback may not be called is that the activity that registered the callback is no longer in the foreground. So there may be a difference between an Android device and a WP8 device that causes the current activity to be paused.
Another reason would be that the WP8 device interupts communication before the Android NFC stack had the time to call the createNdefMessage callback method. However, this should be detectable as the Beam UI would typically disappear before the user is able to click it.
One cause for reason 1 may be that the WP8 device itself sends an NDEF message that causes intent processing on the Android device. If that's the case, a method to overcome this problem may be to register for the foreground dispatch system. This would prevent regular intent processing and would directly send all incoming NDEF messages to the current activity:
#Override
public void onResume() {
super.onResume();
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
adapter.enableForegroundDispatch(this, pi, null, null);
}
#Override
public void onNewIntent(Intent intent) {
if (intent != null) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Log.d("NdefTest", "This problem was actually caused by an incoming NDEF message.");
}
}
}

Android NFC activity being relaunched even though its on the top of the stack

I'm guessing most of you face palmed when you saw this title, and thought we've seen this before well I agree, but I've tried everything to solve this issue and it wont remedy. So here goes.
My app can be launched from the home screen(icon) and everything works OK it scans a NFC tag and sends the relevant data to the server.
However what I also want is for the App to be launched from NFC tag, which it does no problem.
However! when the app is launched via the NFC tag you then remove the phone and re-present it to the tag it should then read the tag and send the data to the server. Exactly the same behaviour as launching the app via the the icon, but when you scan the NFC tag this time a new instance of the app is launched which ultimately kicks the current instance down the stack, the version of the instance that was busy reading the tag so I get a ANR.
in a nutshell:
from home screen -> scan a tag -> app launches.
you then rescan the tag->
The current instance of the app disappears to be replaced by a new instance.
I want the previous instance to stay where it was and work.
A few guesses from me at this point would be that:
-The NFC is not getting properly registered to app, hence the the OS doing default behaviour.
-There is a flag not being set correctly when launched via a NFC tag telling the OS the activity does not need to relaunched.
anyway here's the code:
<uses-permission android:name="android.permission.NFC"/>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"android:host="#string/URL_for_intent_filtering" android:pathPrefix="/id/" />
//Procedure of NFC startup called from OnCreate
private void startNFC()
{
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(! mNfcAdapter.isEnabled())//check the NFC is switched on
{
mMainActivityHandler.sendEmptyMessage(NFC_DISABLED);
}
//its ok to carry on and instantiate the NFC even though its not enabled.
if(mNfcAdapter != null)
{
int requestID = (int) System.currentTimeMillis();
mPendingIntent = PendingIntent.getActivity(this, requestID, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_UPDATE_CURRENT);
IntentFilter intentF = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
intentF.addDataScheme("http");
intentfilter = new IntentFilter[] {intentF};
if(DEBUG) Log.i("DEBUG","+++++++++++++++++++++++++++++++++++++++++++++++ NFC HAS BEEN STARTED");
}else
{
//we have a problem with the NFC adapter probably because the device doesn't have one.
if(DEBUG) Log.i("DEBUG","+++++++++++++++++++++++++++++++++++++++++++++++ NFC HAS FAILED");
mMainActivityHandler.sendEmptyMessage(NFC_FAILED);
}
}
#Override
protected void onPause()
{
if(DEBUG) Log.i("MA","+++++++++++++++++++++++++++++++++++++++ ON PAUSE");
try{
mNfcAdapter.disableForegroundDispatch(this);
}catch(Exception e)
{
}
super.onPause();
}
//and here we should reinstate the NFC adapter
#Override
protected void onResume()
{
if(DEBUG) Log.i("MA","+++++++++++++++++++++++++++++++++++++++ ON RESUME");
//setupDataListener(true);
setupServerComms(getApplicationContext());
//mNfcAdapter.enableForegroundDispatch(this,mPendingIntent,intentfilter,null);
mNfcAdapter.enableForegroundDispatch(this,mPendingIntent,null,null);
super.onResume();
}
Just add this line in your AndroidManifest.xml into your Activity and you are probably done.
android:launchMode="singleTask"
So what you want is for the app to catch the intent and then read the information and do something with it correct? Well you have a pending intent that is used to do this but your setup seems a little different from what I usually do. Here is an example of how I use a pending intent to catch the tag data (from there you can do whatever you want like send it to a server):
Class example{
NfcAdapter adapter;
Tag motor;
String FileName;
PendingIntent detectTag;
IntentFilter NFC;
IntentFilter [] Catcher;
String [][] TechList;
TextView readToField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
// Catch NFC detects
adapter = NfcAdapter.getDefaultAdapter(this);
detectTag = PendingIntent.getActivity(this,0,new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
NFC = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
Catcher = new IntentFilter[] {NFC,};
TechList = new String [][]{new String[]{android.nfc.tech.NfcV.class.getName()}};
}
public void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
motor = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Bridge bridge = (Bridge) getApplication();
bridge.setCurrentTag(motor);
}
public void onPause()
{
super.onPause();
adapter.disableForegroundDispatch(this);
}
public void onResume()
{
super.onResume();
adapter.enableForegroundDispatch(this,detectTag ,Catcher , TechList);
}
}
The Bridge object is just a class I use to save the tag information so I can send information to tags from other (behind the scenes) classes.
Finally, I have always been told that for onResume and onPause you should call the super.method first.

Android NFC App not pushing Ndef messages

I am working on creating an Android App that uses NFC to register touches from device to device. I am using two Nexus 7s for testing.
Ideal use case is to have the App active on one device, not active on the other. The active device pushes an NdefMessage with a record containing some data for the passive device app to deal with. The passive device passes a recording containing some data back to the active app.
I have the following intent filters set up in my manifest:
<activity android:name=".MainActivity" android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<data android:mimeType="application/com.killerapprejji.MainActivity"/>
<data android:mimeType="application/com.*"/>
<data android:mimeType="application/com.killerapprejji.*"/>
<data android:mimeType="application/*"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
In my MainActivity I have the following in onCreate to set up the NFC adapter:
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mNfcPendingIntent = pendingIntent;
// Intent filters for exchanging over p2p.
if(mNfcAdapter != null){
IntentFilter ndefDetected = new
IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("application/*");
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
this.intentFiltersArray = new IntentFilter[] {ndef, };
Log.d(this.toString(), "mNfcPendingIntent: ");
mNdefExchangeFilters = new IntentFilter[] { ndefDetected,defendDetected };
}
I have the following for onNewIntent :
protected void onNewIntent(Intent intent) {
// NDEF exchange mode
Log.d("onNewIntent", intent.getAction());
if (NfcAdapter.ACTION_NDEF_DISCOVERED == (intent.getAction())) {
NdefMessage[] msgs = getNdefMessages(intent);
for(int i = 0; i < msgs.length; i++){
Log.d("onNewIntent", "found new NdefMessage");
}
}
finish();
}
Right now, I run this call:
public void setIdleMessage(){
InteractionHistory intHist = InteractionHistory.getInstance();
NdefMessage attackNdefMessage = null;
NdefRecord[] ndefRecords = new NdefRecord[10];
ndefRecords[0] = NdefRecord.createMime("application/com.killerapprejji.NfcHandle", new String("attack,attacker:"
+ intHist.getDisplayName()
+ ",attackerid:" + "1").getBytes());
attackNdefMessage = new NdefMessage(ndefRecords[0]);
// need to come up with a way to end if the above try/catch fails
mNfcAdapter.setNdefPushMessage(attackNdefMessage, this);
}
Expecting it to set the NdefPushMessage. Whenever I touch put the two devices in range of NFC, I still only get the optional "Touch to beam" interface.
Any ideas as to how I can pick up these intents, or if my NdefMessage is even getting sent as I expect?
"Touch to Beam" message, is the one defined by Google to push that NDEF message that you are expecting to share on your setNdefPushMessage, so until you push that screen, nothing will be sent to the other device.
In fact, that API you are using from Android is called Android BEAM
In the other way, if one device is pushing NDEF messages, it cannot receive messaged until you stop pushing, so you have to implement the NDefPushCallback to catch the succesfull sent of your NDEF message, to stop pushing in the first device, and then it will be able to receive a new intent (NDEF message received) from the other device
I recommend you make a test app first using URI mime type, or plain text (something easy) to make sure that your "Share Logic" is OK. When you confirm that, you can rollback to your own mime types

How to read from detected NFC tag (NDEF message). Android NFC

Similar question - How to read detected NFC tag (NDEF content) details in android?
I want my android app to be able to read and parse a detected NDEF message.
I have already edited the AndroidManifest.xml to detect nfc tags and I have added the intent filter it looks like this
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
I believe this is fine as when I use the NFCDemo sample app that comes with the SDK to create MockNDEFtags, when the list of applications I can choose to handle these generated tags my app appears.
I then click on my app and it opens up without problem, I just need a way to read the data that was passed to it in the NDEF message. The code:
Tag myTag = (Tag) nfcintent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// get NDEF tag details
Ndef ndefTag = Ndef.get(myTag);
...
NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
was suggested in a similar question and throughout the web I find many similar answers.
My problem is with the line of code
"Tag myTag = (Tag) nfcintent.getParcelableExtra(NfcAdapter.EXTRA_TAG);"
I get the error "nfcintent cannot be resolved"
I realize that the author of the code likely put nfcintent as a placeholder for an intent specific to my app however im not sure what im supposed to put in its place.
My mainactivity that starts my app looks like this
public class TabsActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Graph
TabSpec graphspec = tabHost.newTabSpec("Graph");
// setting Title and Icon for the Tab
graphspec.setIndicator("Graph");
Intent graphIntent = new Intent(this, GraphActivity.class);
graphspec.setContent(graphIntent);
// Tab for Intro
TabSpec introspec = tabHost.newTabSpec("Intro");
introspec.setIndicator("Intro");
Intent introIntent = new Intent(this, IntroActivity.class);
introspec.setContent(introIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(introspec); // Adding intro tab
tabHost.addTab(graphspec); // Adding graph tab
}
}
I assume as this starts the app it is where the NFC tag must be dealt with. If I can just access the NDEFMessage from the tag I already have the ability to parse it with the NdefMessageParser from the android sample app. I want to parse the information from the NDEFmessage and ultimately have that information accessible by each tab in the app.
Try this Snippet to extract message from Tag :
Parcelable[] rawMsgs = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
extractMessage(msg);
private void extractMessage(NdefMessage msg) {
byte[] array = null;
array = msg.getRecords()[0].getPayload();
}
Also Check out this Sample for NFC Reader/Writer
To get the intent that passes the NFC tag to start the activity in my case "nfcintent" just use
Intent nfcintent = getIntent();

Categories

Resources