NFC Action_Tech_Discovered with foreground dispatch won';

本文关键字:won dispatch with Action Tech Discovered NFC foreground | 更新日期: 2023-09-27 17:58:30

我正在用Xamarin用C#进行编码,并试图通过NFC扫描MIFARE Classic 1K卡。

m1card_test的intent过滤器工作正常。但我不想选择要启动的"活动"。所以我尝试使用前台调度。

这是我的部分代码(C#):

  • OnCreate

    Intent Myintent = new Intent(this, GetType());
    Myintent.AddFlags(ActivityFlags.SingleTop);
    mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0);
    ndefDetected = new IntentFilter(NfcAdapter.ActionTechDiscovered);
    ndefDetected.AddDataType("*/*");
    intentF = new IntentFilter[] { ndefDetected };
    techLists = new string[][] {new string[] {
        typeof(Android.Nfc.Tech.NfcA).FullName,
        typeof(Android.Nfc.Tech.MifareClassic).FullName}
    };
    
  • OnPause

    NfcManager manager = (NfcManager)GetSystemService(NfcService);
    manager.DefaultAdapter.DisableForegroundDispatch(this);
    
  • OnResume

    NfcManager manager = (NfcManager)GetSystemService(NfcService);
    manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,intentF,techLists);
    

不幸的是,前台调度不起作用(即,它没有拾取标签)。

如果我将呼叫从EnableForegroundDispatch()更改为

manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,null,null);

前台调度工作正常。但它收集了所有标签,而不仅仅是MIFARE Classic,我得到了一个意向Action_Tag_Discovered,而不是Action_Tech_Discovere。

如何在前台调度系统中使用Action_Tech_Discovered?

我错过什么了吗?


techn_list.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
  <tech-list>
    <tech>android.nfc.tech.NfcA</tech>
    <tech>android.nfc.tech.MifareClassic</tech>
  </tech-list>  
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="m1card_test.m1card_test" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="16" />
  <application android:label="m1card_test"></application>
  <uses-permission android:name="android.permission.NFC" />
</manifest>

我的C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Nfc;
namespace m1card_test
{
    [Activity(Label = "m1_read",  Icon = "@drawable/icon", LaunchMode = Android.Content.PM.LaunchMode.SingleTask)]
    [IntentFilter(
    new[] {NfcAdapter.ActionTechDiscovered}, 
    Categories = new[] {Intent.CategoryDefault,})]
    [MetaData("android.nfc.action.TECH_DISCOVERED", Resource = "@xml/tech_list")]
    public class m1_read : Activity
    {
        TextView mTV;
        PendingIntent mPendingIntent;
        IntentFilter ndefDetected;
        IntentFilter[] intentF;
        String[][] techLists;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.m1_read);
            Intent Myintent = new Intent(this, GetType());
            Myintent.AddFlags(ActivityFlags.SingleTop);
            mPendingIntent = PendingIntent.GetActivity(this, 0, Myintent, 0);
            ndefDetected = new IntentFilter(NfcAdapter.ActionTechDiscovered);
            ndefDetected.AddDataType("*/*");
            intentF = new IntentFilter[] { ndefDetected };
            techLists = new string[][] {new string[] {
                typeof(Android.Nfc.Tech.NfcA).FullName,
                typeof(Android.Nfc.Tech.MifareClassic).FullName}
            };
            Button button = FindViewById<Button>(Resource.Id.Back_Button);
            mTV = FindViewById<TextView>(Resource.Id.textview);
            button.Click += delegate
            {
                Intent main_intent = new Intent(this, typeof(MainActivity));
                this.StartActivity(main_intent);
                Finish();
            };
        }
        protected override void OnPause()
        {
            base.OnPause();
            NfcManager manager = (NfcManager)GetSystemService(NfcService);
            manager.DefaultAdapter.DisableForegroundDispatch(this);
        }
        protected override void OnResume()
        {
            base.OnResume();
            NfcManager manager = (NfcManager)GetSystemService(NfcService);
            manager.DefaultAdapter.EnableForegroundDispatch(this, mPendingIntent,intentF,techLists);
        }
        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            mTV.Text = "OnNewIntent";
        }
    }
}

NFC Action_Tech_Discovered with foreground dispatch won';

TECH_DISCOVERED意向过滤器没有关联的数据类型(MIME类型)。因此,您需要删除行

ndefDetected.AddDataType("*/*");

此外,我不太确定typeof(Android.Nfc.Tech.MifareClassic).FullName是否解析为标记技术的正确名称(完整的Java类名)。因此,您可能应该对该字符串进行硬编码(就像在技术过滤器XML文件中所做的那样):

techLists = new string[][] { new string[] {
    "android.nfc.tech.NfcA",
    "android.nfc.tech.MifareClassic"
}};

最后,由于MifareClassic标签技术总是暗示NfcA,您可以安全地将技术过滤器减少到仅

techLists = new string[][] { new string[] {
    "android.nfc.tech.MifareClassic"
}};