简体   繁体   English

Android NFC意向未向设备注册

[英]Android NFC intent not registering with device

I am building an application which scans NFC/Mifare cards and gets their unique identifier. 我正在构建一个扫描NFC / Mifare卡并获取其唯一标识符的应用程序。 Unfortunately, despite following the Android NFC api tutorial, the intent as declared both in the manifest and as a foreground dispatcher is not being registered with the device and so Android chooses to open up the default NFC application instead. 不幸的是,尽管遵循了Android NFC api教程,但清单中和前台调度程序中声明的意图并未在设备中注册,因此Android选择打开默认NFC应用程序。 My manifest is as follows: 我的清单如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myname.application">
    <uses-permission android:name="android.permission.INTERNET"/>
    <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=".Scanner">
            <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"/>
                <action android:name="android.nfc.action.TECH_DISCOVERED"/>
                <action android:name="android.nfc.action.TAG_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
            <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter"></meta-data>
</activity>
    </application>

</manifest>

and my foreground dispatcher is as follows: 我的前台调度程序如下:

   @Override
    protected void onResume() {
        super.onResume();

        setupForegroundDispatch(this, mNfcAdapter);
    }

    @Override
    protected void onPause() {
        stopForegroundDispatch(this, mNfcAdapter);

        super.onPause();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }


    public static void setupForegroundDispatch(final Activity activity, NfcAdapter adapter) {
        final Intent intent = new Intent(activity.getApplicationContext(), activity.getClass());
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);

        IntentFilter[] filters = new IntentFilter[1];
        String[][] techList = new String[][]{};
        filters[0] = new IntentFilter();
        filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
        filters[0].addCategory(Intent.CATEGORY_DEFAULT);
        try {
            filters[0].addDataType(MIMETYPE_TEXT_PLAIN);
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("Check your mime type.");
        }

        adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);
    }


    public static void stopForegroundDispatch(final Activity activity, NfcAdapter adapter) {
        adapter.disableForegroundDispatch(activity);
    }

As I'm not sure if it makes a difference, the platform that the application is currently being run on is a Huawei P10 Plus. 我不确定是否会有所作为,因此该应用程序当前正在运行的平台是Huawei P10 Plus。 I've had a few compatibility issues with it in the past so this may be relevant. 过去我遇到过一些兼容性问题,因此这可能是相关的。

I do this successfully but without declare anything about NFC in the manifest, only the NFC permissions. 我成功完成了此操作,但未在清单中声明任何有关NFC的内容,仅声明了NFC权限。 I don't pass to the foregroundDispath the filters and the techList and I verify that the intent that I received is a nfc intent. 我没有将过滤器和techList传递给foregroundDispath,并且我确认我收到的意图是nfc意图。

@Override
protected void onPause() {
 super.onPause();
 mAdapter = NfcAdapter.getDefaultAdapter(context);
 if(mAdapter != null){
     mAdapter.disableForegroundDispatch(activity);
 }
}

@Override
protected void onResume() {
 super.onResume();
 mAdapter = NfcAdapter.getDefaultAdapter(context);
 if(mAdapter != null) {
    mPendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    mAdapter.enableForegroundDispatch(activity, mPendingIntent, null, null);
 }
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if ( NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) ) {
     readNFC(intent);
  }

}

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

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