Android NFC标签阅读。 intent始终为null

时间:2012-08-30 19:25:38

标签: java android android-intent nfc

我正在尝试在打开应用程序时读取NFC标签(当我扫描标签时自动打开),但我似乎无法读取有效负载。事实上,我似乎无法选择标签。我用另一个应用程序来读取标签的mime类型和有效负载 - 一切似乎都很好。

如果这是显而易见的道歉 - 我已经阅读了文档并在发布之前检查了各种其他来源,但无济于事。

我可以看到没有错误,代码编译得很好。 代码如下。 Logcat显示:

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());

清单文件;

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

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

1 个答案:

答案 0 :(得分:1)

NFC意图过滤器适用于CardActivity,不适用于MainActivity。所以我希望问题出在CardActivity上。