开发者

NFC read and write

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" andr开发者_开发百科oid: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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜