简体   繁体   中英

Android NFC Tag Read. intent always null

I am trying to read an NFC tag when the app is opened (opened automatically when I scan the tag), but I can't seem to read the payload. In fact, I can't seem to pick the tag up at all. I have used another app to read the tag's mime type and payload - everything seems fine.

Apologies if this is an obvious one - I have read the documentation and checked various other sources before posting, to no avail.

There are no errors I can see and code compiles fine. Code below. Logcat shows:

08-30 20:15:50.248: E/Activity...(3703): Hello world. Intent Type: null 08-30 20:15:50.248: E/Activity...(3703): Hello world. Intent Type: null :

package com.spotsofmagic.spotsofmagic;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener {
    private static final String TAG = "Activity...";
    private NfcAdapter mAdapter;
    private TextView mTextView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        // grab our NFC Adapter
        mAdapter = NfcAdapter.getDefaultAdapter(this);

        // TextView that we'll use to output messages to screen
        mTextView = (TextView)findViewById(R.id.text_view);

        displayMessage("Loading payload...");
        // see if app was started from a tag and show game console
        Intent intent = getIntent();


        Log.e(TAG, "Hello world.  Intent Type: "+ intent.getType());

Manifest file;

 <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".MainActivity" 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=".CardActivity" android:label="@string/app_name" > <!-- Handle a collectable card NDEF record --> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <data android:mimeType="application/vnd.spotsofmagic.bluetoothon"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> </application> 

The NFC intent filter is for CardActivity, not for MainActivity. So I would expect that the problem is in CardActivity.

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