NFC与Xamarin.Forms没有任何反应

时间:2017-05-12 20:12:20

标签: android xamarin.android tags xamarin.forms nfc

我第一次希望实现NFC跨平台Xamarin.Forms,用于WinPhone和Android项目。

我在Android上测试我的应用程序,但没有真正发生。 作为一种工具,我使用了Visa Pay Wave卡,我在Android程序reTag中进行了测试,并且扫描成功。 我使用了来自这个link的GitHub的解决方案 我有0个错误,也有应用程序"工作"但是当我将Visa卡添加到我的手机背面时,我什么都没得到。

我的第一个问题是:哪种协议使用Visa卡? (TAG_DISCOVERED,TECH_DISCOVERED或NDEF_DISCOVERED)。我认为这是我的计划在闲置"状态。

我的第二个问题是:你知道为什么我不能从程序中获得任何事件吗? (开始只获取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文件夹,如果需要,我会发布它。

内容页面与我提供的链接中的GitHub相同。

1 个答案:

答案 0 :(得分:3)

Visa payWave卡基于EMV标准。就RF通信协议而言,这些卡使用ISO / IEC 7816-4而不是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意图过滤器无法使用EMV卡,因为它们不包含任何NDEF结构化数据。

相关问题