2017-05-12 90 views
2

第一次,我想實現NFC跨平臺Xamarin.Forms項目WinPhone和Android。NFC與Xamarin.Forms沒有任何反應

我在Android上測試了我的應用程序,但沒有發生任何事情。 作爲一種工具,我使用了Visa Pay Wave卡,這是我在Android的程序reTag中測試的,並且它是成功的掃描。 我使用這個 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); 
    } 
} 
} 

我已經加入到資源文件夾, xml文件夾與nfc.xml文件,如果需要我會發布它。

內容頁面與我在提供的鏈接上的GitHub上的頁面相同。

+0

您是否嘗試過使用可讀寫的標準NFC標籤。這可能會縮小Visa卡使用的連接範圍。 –

+0

還有這個網站https://www.patrickvankleef.com/2017/01/08/xamarin-near-field-communication/最近在Android平臺上使用NFC代碼的例子 –

+0

@AaronThompson謝謝,我會試試Android平臺,但我需要xamarin.forms。 – Stefan0309

回答

3

Visa payWave卡基於EMV標準。在射頻通信協議方面,這些卡使用ISO-DEP(ISO/IEC 14443-4)的ISO/IEC 7816-4。

在Android上,您可以使用TECH_DISCOVERED intent過濾器和相應的過濾器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結構化數據。

+1

這應該被標記爲正確的答案。我的回答是誤導,因爲我知道op想要模擬卡片,對不起。 – originx