2016-07-25 33 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