gpt4 book ai didi

c# - NFC Action_Tech_Discovered with foreground dispatch 不会捕获 Mifare 1k 卡

转载 作者:行者123 更新时间:2023-11-30 01:09:18 25 4
gpt4 key购买 nike

我正在使用 Xamarin 编写 C# 代码,并尝试通过 NFC 扫描 MIFARE Classic 1K 卡。

m1card_test 的 intent-filter 工作正常。但我不想选择要启动的 Activity 。所以我正在尝试使用前台调度。

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

  • 创建时

    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}
    };
  • 暂停

    NfcManager manager = (NfcManager)GetSystemService(NfcService);
    manager.DefaultAdapter.DisableForegroundDispatch(this);
  • 恢复时

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

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

如果我将对 EnableForegroundDispatch() 的调用更改为

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

前台调度工作正常。但它会拾取所有标签,而不仅仅是 MIFARE Classic,我得到一个 Intent Action_Tag_Discovered 而不是 Action_Tech_Discovered。

Action_Tech_Discovered如何配合前台调度系统使用?

我错过了什么吗?


技术列表.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";
}
}
}

最佳答案

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

ndefDetected.AddDataType("*/*");

此外,我不太确定 typeof(Android.Nfc.Tech.MifareClassic).FullName 是否解析为标签技术的正确名称(完整的 Java 类名称)。因此,您应该对该字符串进行硬编码(就像您在 tech-filter 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"
}};

关于c# - NFC Action_Tech_Discovered with foreground dispatch 不会捕获 Mifare 1k 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38573882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com