2016-07-25 1 views
1

저는 C#으로 Xamarin으로 코딩하고 NFC에서 MIFARE Classic 1K 카드를 스캔하려고합니다.포어 그라운드 디스패치로 NFC Action_Tech_Discovered가 Mifare 1k 카드를 수신하지 못합니다.

m1card_test의 인 텐트 필터가 정상적으로 작동합니다. 그러나 어떤 활동을 시작할지 선택하고 싶지 않습니다. 그래서 전경 파견을 사용하려고합니다.

  • 에서 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); 
    
  • : 여기

    내 코드 (C#을)의 일부입니다 OnResume

    NfcManager manager = (NfcManager)GetSystemService(NfcService); 
    manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,intentF,techLists); 
    

불행하게도, 전경 파견이 작동하지 않습니다 (즉, 태그를 집어 올리지 않습니다).

나는

manager.DefaultAdapter.EnableForegroundDispatch(this,mPendingIntent,null,null); 

전경 파견 잘 작동에 EnableForegroundDispatch()로 통화를 변경하는 경우. 그러나 MIFARE Classic뿐만 아니라 모든 태그를 집어 들고 Action_Tech_Discovered가 아닌 Intellent Action_Tag_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 유형) associ 그것으로 ated. 따라서 당신은 내가 typeof(Android.Nfc.Tech.MifareClassic).FullName 태그 기술의 정확한 이름 (전체 Java 클래스 이름)로 확인하면 아주 확실하지 않다, 또한 라인

ndefDetected.AddDataType("*/*"); 

을 제거해야합니다. MifareClassic 태그 기술은 항상 NfcA을 의미하기 때문에

techLists = new string[][] { new string[] { 
    "android.nfc.tech.NfcA", 
    "android.nfc.tech.MifareClassic" 
}}; 

마지막으로, 안전하게 기술 - 필터를 줄일 수 있습니다 : 따라서, 당신은 문자열 (당신이 기술 필터 XML 파일에서와 마찬가지로) 아마 하드 코딩을해야 ~

techLists = new string[][] { new string[] { 
    "android.nfc.tech.MifareClassic" 
}}; 
+0

수정 해 주셔서 감사합니다. 데이터 유형을 제거하고 하드 코드를 사용하면 작동합니다! 저는이 부분에 익숙하지 않으므로 귀하의 관용과 도움에 다시 한 번 감사드립니다. – wuken

관련 문제