2016-07-25 62 views
1

我在C#中用Xamarin編碼,並試圖通過NFC掃描MIFARE Classic 1K卡。NFC Action_Tech_Discovered與前臺調度不會捕獲Mifare 1k卡

m1card_test的意圖過濾器工作正常。但我不想選擇要開始的活動。所以我試圖使用前臺調度。

這裏是我的代碼(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_Discovered。

如何使用Action_Tech_Discovered與前臺調度系統?

我錯過了什麼嗎?


tech_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"; 
     } 
    } 
} 

回答

1

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" 
}}; 
+0

感謝您的編輯第一。在我刪除數據類型並使用硬編碼後,它可以工作!我是新來的,所以我非常感謝你的容忍和幫助。 – wuken