2016-07-25 1 views
1

Je code en C# avec Xamarin et essaye de scanner une carte MIFARE Classic 1K par NFC.NFC Action_Tech_Discovered avec le dispatch de premier plan n'attrapera pas la carte Mifare 1k

Le filtre d'intention de m1card_test fonctionne correctement. Mais je ne veux pas sélectionner quelle activité je veux commencer. J'essaie donc d'utiliser la répartition de premier plan.

Voici une partie de mon code (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); 
    

Malheureusement, l'envoi de premier plan ne fonctionne pas (à savoir il ne ramasse pas l'étiquette).

Si je change l'appel à EnableForegroundDispatch() à

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

l'avant-plan expédition beau travail. Mais il prend toutes les balises et pas seulement MIFARE Classic et j'obtiens une intention Action_Tag_Discovered au lieu de Action_Tech_Discovered. Comment utiliser Action_Tech_Discovered avec le système de dispatch de premier plan?

Ai-je raté quelque chose?


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> 

Mon code 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"; 
     } 
    } 
} 

Répondre

1

Le filtre intention TECH_DISCOVERED ne dispose pas d'un type de données (Type MIME) avec lui. Ainsi, vous devez supprimer la ligne

ndefDetected.AddDataType("*/*"); 

De plus, je ne suis pas tout à fait sûr si typeof(Android.Nfc.Tech.MifareClassic).FullName décide au nom correct (nom de classe Java complet) de la technologie de l'étiquette. Ainsi, vous devriez probablement coder en dur cette chaîne (comme vous le faites dans la technologie filtre fichier XML): la technologie

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

Enfin, puisque la balise MifareClassic implique toujours NFCA, vous pouvez sans risque réduire la tech-filtre à juste

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

Merci d'avoir d'abord édité. Après avoir supprimé le type de données et utilisé le code dur, cela fonctionne! Je suis nouveau à ce sujet, je suis très reconnaissant pour votre tolérance et votre aide à nouveau. – wuken