简体   繁体   中英

Read a RFID/NFC Tag ID

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>

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).

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM