user635489
user635489

Reputation: 11

Android Application with NFC

I am trying to build an Android application in which I need to have an activity that does the following: It loads up and then waits until a NFC tag is detected. I do not really care about the way the tag is parsed (whether it is a smart poster or URI etc..). The only thing i am interested in is the ID of that tag. Once a tag and its ID are detected, I want to perform some computations, and then go back to the waiting state (the state where the application is waiting to detect an NFC tag).

My problem is that I cannot figure out how to make all my code be triggered by the detection of a tag. (Please note that the application is running so it is not a problem of application priority. Instead, I want my code to be triggered by the detection of a tag, and then go back to waiting state).

Thank you very much

Upvotes: 1

Views: 7097

Answers (1)

Sven Haiges
Sven Haiges

Reputation: 2636

Here we go, code below. The trick is to register the foreground tag dispatch so your activity gets all the new tags. Also specify the flag SINGLE_TOP so the one instance active of the activity will be called with onNewIntent. Will post the ForegroundUtil, too.

public class DashboardActivity extends Activity {

NFCForegroundUtil nfcForegroundUtil = null;

private TextView info;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    info = (TextView)findViewById(R.id.info);

    nfcForegroundUtil = new NFCForegroundUtil(this);


}

public void onPause() {
    super.onPause();
    nfcForegroundUtil.disableForeground();
}   

public void onResume() {
    super.onResume();
    nfcForegroundUtil.enableForeground();

    if (!nfcForegroundUtil.getNfc().isEnabled())
    {
        Toast.makeText(getApplicationContext(), "Please activate NFC and press Back to return to the application!", Toast.LENGTH_LONG).show();
        startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
    }

}

public void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    info.setText(NFCUtil.printTagDetails(tag));    

}


}

Foreground-Util (you shoudl modify the intent filter to fit your needs)

public class NFCForegroundUtil {

private NfcAdapter nfc;


private Activity activity;
private IntentFilter intentFiltersArray[];
private PendingIntent intent;
private String techListsArray[][];

public NFCForegroundUtil(Activity activity) {
    super();
    this.activity = activity; 
    nfc = NfcAdapter.getDefaultAdapter(activity.getApplicationContext());

    intent = PendingIntent.getActivity(activity, 0, new Intent(activity,
            activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

    try {
        ndef.addDataType("*/*");
    } catch (MalformedMimeTypeException e) {
        throw new RuntimeException("Unable to speciy */* Mime Type", e);
    }
    intentFiltersArray = new IntentFilter[] { ndef };

    techListsArray = new String[][] { new String[] { NfcA.class.getName() } };
    //techListsArray = new String[][] { new String[] { NfcA.class.getName(), NfcB.class.getName() }, new String[] {NfcV.class.getName()} };
}

public void enableForeground()
{
    Log.d("demo", "Foreground NFC dispatch enabled");
    nfc.enableForegroundDispatch(activity, intent, intentFiltersArray, techListsArray);     
}

public void disableForeground()
{
    Log.d("demo", "Foreground NFC dispatch disabled");
    nfc.disableForegroundDispatch(activity);
}

public NfcAdapter getNfc() {
    return nfc;
}   

}

Upvotes: 8

Related Questions