Xamarin.Forms 的 NFC 没有任何反应
NFC with Xamarin.Forms nothing happens
第一次想实现NFC跨平台Xamarin.Forms项目WinPhone和Android。
我正在 Android 我的应用程序上进行测试,但实际上没有任何反应。
作为工具,我使用了 Visa Pay Wave 卡,我在 Android 的程序 reTag 中对其进行了测试,并且扫描成功。
我使用了来自这个 link 的 GitHub 的解决方案
我有 0 个错误,还有应用程序 "works",但是当我将我的 Visa 卡添加到背面的 phone 时,我什么也没得到。
我的第一个问题是:哪个协议使用Visa卡? (TAG_DISCOVERED、TECH_DISCOVERED 或 NDEF_DISCOVERED)。我认为这是我的程序处于 "idle" 状态的原因。
我的第二个问题是:你知道为什么我无法从程序中获取任何事件吗? (开始只是获取 UID 号码..)
这是我的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.NFC" />
<application android:label="NFCTest002.Android"></application>
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application>
<activity
android:name="MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</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" />
</activity>
</application>
</manifest>
我的MainActivity.cs:
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Nfc;
using Android.OS;
using Poz1.NFCForms.Abstract;
using Poz1.NFCForms.Droid;
using System;
namespace NFCTest002.Droid
{
[Activity(Label = "NFCTest002", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { NfcAdapter.ActionTechDiscovered })]
[MetaData(NfcAdapter.ActionTechDiscovered, Resource = "@xml/nfc")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public NfcAdapter NFCdevice;
public NfcForms x;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
NfcManager NfcManager = (NfcManager)Android.App.Application.Context.GetSystemService(Context.NfcService);
NFCdevice = NfcManager.DefaultAdapter;
Xamarin.Forms.DependencyService.Register<INfcForms, NfcForms>();
x = Xamarin.Forms.DependencyService.Get<INfcForms>() as NfcForms;
LoadApplication(new NFCTest002.App());
}
protected override void OnResume()
{
base.OnResume();
if (NFCdevice != null)
{
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
NFCdevice.EnableForegroundDispatch
(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
new String[][] {new string[] {
NFCTechs.Ndef,
},
new string[] {
NFCTechs.MifareClassic,
},
}
);
}
}
protected override void OnPause()
{
base.OnPause();
NFCdevice.DisableForegroundDispatch(this);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
x.OnNewIntent(this, intent);
}
}
}
我已经将 nfc.xml 文件添加到 Resource 文件夹和 xml 文件夹,如果需要,我会 post 它。
内容页与我提供的 link 中的 GitHub 相同。
Visa payWave 卡基于 EMV 标准。在 RF 通信协议方面,这些卡使用 ISO/IEC 7816-4 over ISO-DEP (ISO/IEC 14443-4)。
在 Android 上,您可以使用 TECH_DISCOVERED 意图过滤器结合适当的过滤器 XML 文件来挑选这些卡片。对于这些卡片,XML 文件需要如下所示:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
</resources>
与上述基于清单的意图过滤器的方法类似,您可以使用以下方法启用基于前台调度的标签发现:
NFCdevice.EnableForegroundDispatch(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
new String[][] {
new string[] {
NFCTechs.IsoDep,
},
}
);
NDEF_DISCOVERED intent 过滤器不适用于 EMV 卡,因为它们不包含任何 NDEF 结构化数据。
第一次想实现NFC跨平台Xamarin.Forms项目WinPhone和Android。
我正在 Android 我的应用程序上进行测试,但实际上没有任何反应。 作为工具,我使用了 Visa Pay Wave 卡,我在 Android 的程序 reTag 中对其进行了测试,并且扫描成功。 我使用了来自这个 link 的 GitHub 的解决方案 我有 0 个错误,还有应用程序 "works",但是当我将我的 Visa 卡添加到背面的 phone 时,我什么也没得到。
我的第一个问题是:哪个协议使用Visa卡? (TAG_DISCOVERED、TECH_DISCOVERED 或 NDEF_DISCOVERED)。我认为这是我的程序处于 "idle" 状态的原因。
我的第二个问题是:你知道为什么我无法从程序中获取任何事件吗? (开始只是获取 UID 号码..)
这是我的 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.NFC" />
<application android:label="NFCTest002.Android"></application>
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application>
<activity
android:name="MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</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" />
</activity>
</application>
</manifest>
我的MainActivity.cs:
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Nfc;
using Android.OS;
using Poz1.NFCForms.Abstract;
using Poz1.NFCForms.Droid;
using System;
namespace NFCTest002.Droid
{
[Activity(Label = "NFCTest002", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[IntentFilter(new[] { NfcAdapter.ActionTechDiscovered })]
[MetaData(NfcAdapter.ActionTechDiscovered, Resource = "@xml/nfc")]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public NfcAdapter NFCdevice;
public NfcForms x;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
NfcManager NfcManager = (NfcManager)Android.App.Application.Context.GetSystemService(Context.NfcService);
NFCdevice = NfcManager.DefaultAdapter;
Xamarin.Forms.DependencyService.Register<INfcForms, NfcForms>();
x = Xamarin.Forms.DependencyService.Get<INfcForms>() as NfcForms;
LoadApplication(new NFCTest002.App());
}
protected override void OnResume()
{
base.OnResume();
if (NFCdevice != null)
{
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
NFCdevice.EnableForegroundDispatch
(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
new String[][] {new string[] {
NFCTechs.Ndef,
},
new string[] {
NFCTechs.MifareClassic,
},
}
);
}
}
protected override void OnPause()
{
base.OnPause();
NFCdevice.DisableForegroundDispatch(this);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
x.OnNewIntent(this, intent);
}
}
}
我已经将 nfc.xml 文件添加到 Resource 文件夹和 xml 文件夹,如果需要,我会 post 它。
内容页与我提供的 link 中的 GitHub 相同。
Visa payWave 卡基于 EMV 标准。在 RF 通信协议方面,这些卡使用 ISO/IEC 7816-4 over ISO-DEP (ISO/IEC 14443-4)。
在 Android 上,您可以使用 TECH_DISCOVERED 意图过滤器结合适当的过滤器 XML 文件来挑选这些卡片。对于这些卡片,XML 文件需要如下所示:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
</resources>
与上述基于清单的意图过滤器的方法类似,您可以使用以下方法启用基于前台调度的标签发现:
NFCdevice.EnableForegroundDispatch(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionTechDiscovered) },
new String[][] {
new string[] {
NFCTechs.IsoDep,
},
}
);
NDEF_DISCOVERED intent 过滤器不适用于 EMV 卡,因为它们不包含任何 NDEF 结构化数据。