1

Ok so I've been bangin away at my first Android app and the NFC has been very hit and miss. I'm able to successfully pickup on plain text type records, but when I switch over to try to pickup on uri records the phone's browser keeps opening rather than my app. I'm clueless at this point so here's what I got...

<activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:scheme="http" android:host="google.com"/>
        </intent-filter>
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <data android:mimeType="text/plain"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

When I read the tag, I get a new intent but it's action type is "MAIN". Is it just being relaunched? And if so why doesn't the text record do the same? I've tried multiple uri records and every time I get this behavior. Here is part of the java src.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent launchIntent = getIntent();
    String action = launchIntent.getAction();
    if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
        Log.i(tag, "MATCH!");
    }


    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    tagDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

    tagDetected.addDataScheme("http");
    tagDetected.addDataAuthority("google.com", null);
    filters = new IntentFilter[] { tagDetected };

    techArray = new String[][] {
            new String[] {
                    IsoDep.class.getName()
            },
            new String[] {
                    NfcB.class.getName()
            },
            new String[] {
                    Ndef.class.getName()
            }
    };

}

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



    nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, filters, techArray);

    Intent launchIntent = getIntent();
    String action = launchIntent.getAction();
    if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
        Log.i(tag, "MATCH!");
    } else if(action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
        Log.i(tag, "TECH DISCOVERED");

    } else if(action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        Log.i(tag, "TAG DISCOVERED");
    }

    Parcelable[] msg = launchIntent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    //byte[] payloadData = msg.getRecords()[0].getPayload();
    //Log.i(tag, "NUM records = " + Integer.toString(msg.getRecords().length));


}


public void onPause() {
    super.onPause();

    nfcAdapter.disableForegroundDispatch(this);
}

Another interesting note is that when I don't include the list of technologies in the enableForegroundDispatch() call, then the app doesn't pickup any intents resulting from NFC at all (when trying to read uri records). Any ideas oh wise internet?!

  • What is the URI on the tag? The hostname should match exactly the one in the intent filter. – NFC guy Aug 27 '12 at 21:57
  • The uri consists of 0x01 to encode `http://www.` followed by google.com. I think the issue is related to the issue discussed [here](http://stackoverflow.com/questions/2708392/why-is-my-searchable-activitys-intent-getaction-null?rq=1). – user1628779 Aug 27 '12 at 22:44

1 Answers1

0

With ForegroundDispatch enabled, the intent will arrive through onNewIntent(), so you need to override that method in your activity to receive the NFC intent.

You also need to make sure that the hostname matches exactly. If the tag contains "www.google.com", your intent filter should contain the same name: <data android:scheme="http" android:host="www.google.com"/> and tagDetected.addDataAuthority("www.google.com", null).

NFC guy
  • 10,151
  • 3
  • 27
  • 58