简体   繁体   English

Android NFC标签读取。 意向始终为null

[英]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. 我正在尝试打开应用程序(扫描标签时自动打开)时读取NFC标签,但似乎无法读取有效载荷。 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. 我使用了另一个应用程序来读取标签的mime类型和有效载荷-一切似乎都很好。

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: Logcat显示:

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 : 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. NFC意图过滤器用于CardActivity,而不用于MainActivity。 So I would expect that the problem is in CardActivity. 因此,我希望问题出在CardActivity中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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