发现nfc标签后,应用无法启动

时间:2018-06-11 00:47:55

标签: java android nfc

我正在尝试开发一款可以读取nfc标签ID的应用。 我开始使用NFC Basics: https://developer.android.com/guide/topics/connectivity/nfc/nfc

它说,如果发现nfc标签,我的应用程序将启动 但是当我用我的设备(Xperia C3)扫描nfc标签时,它只显示我的吐司通知" NFC读取错误。再试一次"。 我尝试使用已打开和关闭的应用程序扫描nfc标记,但具有相同的结果

有我的AndroidManifest:



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company">
    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>


            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED"/>
            </intent-filter>
            <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />


            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
&#13;
&#13;
&#13;

nfc_tech_filter.xml:

&#13;
&#13;
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.NfcF</tech>
        <tech>android.nfc.tech.NfcV</tech>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>
&#13;
&#13;
&#13;

在MainActivity.java中,除了获取有关nfc标记扫描的信息外,我不想做任何事情。

&#13;
&#13;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

    NfcAdapter mNfcAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
        } else if (!mNfcAdapter.isEnabled()) {
            Toast.makeText(this, "NFC is not enabled", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "NFC is OK", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())
                || NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())
                || NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        System.out.println(intent.getAction());
        setIntent(intent);
    }

    private void processIntent(Intent intent) {
        Toast.makeText(this, "NFC tag scanned", Toast.LENGTH_LONG).show();
    }
}
&#13;
&#13;
&#13;

那么,问题是什么?

抱歉,如果我没有提供足够的信息,请问我任何事情

0 个答案:

没有答案