Question

I am writing an Android application to read and write to a tag using Nexus S. I have two different activities, one for read and another to write, Both are to be called during their respective contexts. My manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="bri.nfc.NfcTagReaderV1"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.NFC"/>
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".NFCTagReaderV1Activity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.android.nfc.TagViewer">
            <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <data android:mimeType="text/*" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <data android:scheme="http" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    <activity android:name="bri.nfc.writetag.WritetagActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <data android:mimeType="text/*" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <data android:scheme="http" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
</application>
</manifest>

At my execution, the activities are called at random.. one time the tagviewer activity is called and another time writetag activity is called.

I require that my application should call "tagviewer activity" on read and "writetag activity" on write.

Was it helpful?

Solution

You don't need to declare an intent filter for your "writetag activity". Instead you need to use the foreground dispatch system.

The following link sends you to a Android Dev page, which describes how to implement a foreground dispatch system. The implementation is very straight forward.

http://developer.android.com/guide/topics/nfc/index.html#foreground-dispatch

When the newIntent-event is fired, then you're able to write your data on the NFC tag. For writing data on a NFC tag follow this link http://developer.android.com/guide/topics/nfc/index.html#write

Thats the way I'm doing this within my applications and it works great.

OTHER TIPS

When your Activity Resumes you should use foreground dispatch in OnResume Method() of your ActivityClass.For Reading the Tag follow this link Reading NFC Tag With ForeGround Dispatch.And While Writing you should Again Set the Foreground Dispatch for the WritingActivity. Dont forget to stop Foreground Dispatch in OnPause Method of your Activity.So that at a time only 1 activity will register for foreground dispatch

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top