0

I am trying to get a piece of code to be able to scan a Tag and display that tag in a TextView...

I have been getting really swamped down in this, any advice would be appreciated...

When I scan a tag, the noise for a tag being discovered is played... However the TextView is not updated... So the app can currently scan a tag, but it is not putting said tag ID in the TextView...

Java Main Class

package com.security.nfc;

    import android.app.Activity;
    import android.content.Intent;
    import android.nfc.NfcAdapter;
    import android.nfc.Tag;
    import android.os.Bundle;
    import android.widget.TextView;

public class main extends Activity 
{



@Override
protected void onCreate(Bundle b)
{
    super.onCreate(b);
    setContentView(R.layout.main);

}
    public void onNewIntent(Intent intent) 
    {
        Tag myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        TextView tagID = (TextView) findViewById(R.id.result);
        tagID.setText("TagID: " + myTag.getId());
    }

}

Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.security.nfc">

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">

    <activity
        android:name="com.security.nfc.main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
            <action android:name="android.nfc.action.TECH_DISCOVERED"/>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        </intent-filter>
    </activity>
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
</application>

<uses-permission android:name="android.permission.NFC" />

</manifest>
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Rory Price
  • 45
  • 1
  • 1
  • 6
  • what is your issue? All you have told us is that you are trying to read a NFC tag, and that you have this code - no mention of any errors, or unexpected behaviour. – panini Mar 17 '14 at 23:34
  • Updated OP, there are no errors when compiling or running the app, The app simply does not put the scanned ID into the TextView... However the successful Tag Scan noise is played when i scan a tag – Rory Price Mar 17 '14 at 23:38
  • is anything getting put into the TextView? are you getting "TagID: "? or is it just empty? – panini Mar 17 '14 at 23:39
  • There is no change to the TextView at all... The basic idea is to scan a tag and it put the ID into the TextView – Rory Price Mar 17 '14 at 23:41

2 Answers2

0

If you want to receive NFC tag discovery events while your activity is already visible in the foreground, you should register with the NFC foreground dispatch system. See Advanced NFC: Using the NFC Foreground Dispatch System. You would typically register using the NfcAdapter's enableForegroundDispatch() method. After that you can get the intents in your activity's onNewIntent() method (or as a pending intent result depending on how you register).

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
0

void onNewIntent (Intent intent)

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

https://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)

MOSDEV
  • 139
  • 4